I'm currently working my way through the Irvine x86 Assembly book, and I'm on chapter four.
They've introduced the OFFSET
directive, but I'm confused on why I'd ever use it. Why wouldn't I just take the label (which is already the address of that data)? It seems like OFFSET
just adds extra noise.
I have this small program to illustrate my point. I have a label for some data called array
and I can move the elements of my array into al
. But the book is talking about using the OFFSET
directive to get the address of array
and move it to esi
. But this just seems unnecessary to me as I could just use the label.
I have two sections of code that do the same thing below. One where I'm using the label to access the elements of the array and the other where I'm using OFFSET
to move the address into esi
and then access the elements of the array.
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
array BYTE 10h, 20h, 30h, 40h, 50h
.code
main PROC
xor eax, eax ; set eax to 0
; Using Labels
mov al, array
mov al, [array + 1]
mov al, [array + 2]
mov al, [array + 3]
mov al, [array + 4]
; Using Offset
mov esi, OFFSET array
mov al, [esi]
mov al, [esi + 1]
mov al, [esi + 2]
mov al, [esi + 3]
mov al, [esi + 4]
INVOKE ExitProcess, 0
main ENDP
END main
Are they really just two ways to achieve the same thing?
Later on in the book when talking about pointers, they have this example:
.data
arrayB byte 10h, 20h, 30h, 40h
ptrB dword arrayB
And this makes sense to me. ptrB
holds the address of arrayB
. But then they say, "Optionally, you can delcare ptrB with the OFFSET
operator to make the relationship clearer:"
ptrB dword OFFSET arrayB
That doesn't make it clearer to me at all. I already know arrayB
is an address. It looks like OFFSET
is just thrown in there and it's not really doing anything. Removing OFFSET
from that last line would literally achieve the same thing. What exactly does OFFSET
do if I can just use a label to get the address anyways?