The MASM Forum

General => The Campus => Topic started by: RedSkeleton007 on July 14, 2015, 10:42:55 AM

Title: OFFSET can also be applied to a direct-offset operand?
Post by: 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
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: hutch-- on July 14, 2015, 11:09:03 AM
Zero based index.
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: RedSkeleton007 on July 14, 2015, 12:14:37 PM
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?
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: dedndave on July 14, 2015, 01:12:01 PM
OFFSET myArray+0
would point to the first element

OFFSET myArray+4
would point to the last element
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: hutch-- on July 14, 2015, 03:09:22 PM
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.
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: dedndave on July 14, 2015, 03:16:15 PM
my mistake, Hutch - thanks for catching that

OFFSET myArray+4 does point to the 3 value   :P
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: Tedd on July 15, 2015, 12:55:55 AM
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".
Title: Re: OFFSET can also be applied to a direct-offset operand?
Post by: jj2007 on July 15, 2015, 01:29:03 AM
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