News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

OFFSET can also be applied to a direct-offset operand?

Started by RedSkeleton007, July 14, 2015, 10:42:55 AM

Previous topic - Next topic

RedSkeleton007

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?

hutch--


RedSkeleton007

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?

dedndave

OFFSET myArray+0
would point to the first element

OFFSET myArray+4
would point to the last element

hutch--

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.

dedndave

my mistake, Hutch - thanks for catching that

OFFSET myArray+4 does point to the 3 value   :P

Tedd

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".
Potato2

jj2007

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