My book speaks of something called a direct-offset operand. Here's the example it provides:
.data
myArray WORD 1,2,3,4,5
.code
mov esi,OFFSET myArray + 4
It then says that ESI points to the third integer in the array. Since array indexes start at
- :
- ,[1],[2],[3],[4]
1, 2, 3, 4, 5
wouldn't myArray + 4 point to the fourth integer at index [3]? What's going on here?
Zero based index.
Quote from: hutch-- on July 14, 2015, 11:09:03 AM
Zero based index.
Okay... so then myArray + 4 does point to the integer in index [3], right?
OFFSET myArray+0
would point to the first element
OFFSET myArray+4
would point to the last element
Also remember the data SIZE, 1 byte for BYTE, 2 bytes for WORD, 4 bytes for DWORD etc .... OFFSET works in BYTES so you must set the offset to match the data type, 0,4,8,12,16 etc .... for DWORD.
0,2,4,6,8,10,12 etc .... for WORD.
my mistake, Hutch - thanks for catching that
OFFSET myArray+4 does point to the 3 value :P
Quote from: RedSkeleton007 on July 14, 2015, 10:42:55 AM
My book speaks of something called a direct-offset operand. Here's the example it provides:
.data
myArray WORD 1,2,3,4,5
.code
mov esi,OFFSET myArray + 4
It then says that ESI points to the third integer in the array. Since array indexes start at - :
- ,[1],[2],[3],[4]
1, 2, 3, 4, 5
wouldn't myArray + 4 point to the fourth integer at index [3]? What's going on here?
"OFFSET myArray + 4" is (OFFSET myArray) + 4
bytes (the cpu knows nothing of your arrays or their element sizes.)
If you want array indexing, you must multiply the index by the element size, in this case: myArray
= (OFFSET myArray) + (SIZEOF WORD*i)
So, +0 points to "1", +2 points to "2", +4 points to "3", +6 points to "4" and +8 points to "5".
mov ax, myArray[2*WORD] ; simple
mov esi, offset myArray ; offset in register
mov dx, [esi+2*WORD]
mov ecx, 2
mov bx, [esi+ecx*WORD] ; using an index register
mov ax, myArray[ecx*WORD] ; also valid