aanrv
aanrv

Reputation: 2241

Using OFFSET operator on an array in x86 Assembly?

I'm currently going through Assembly Language for x86 Processors 6th Edition by Kip R. Irvine. It's quite enjoyable, but something is confusing me.

Early in the book, the following code is shown:

list BYTE 10,20,30,40
ListSize = ($ - list)

This made sense to me. Right after declaring an array, subtract the current location in memory with the starting location of the array to get the number of bytes used by the array.

However, the book later does:

.data
arrayB BYTE 10h,20h,30h 
.code
mov esi, OFFSET arrayB
mov al,[esi]
inc esi
mov al,[esi]
inc esi
mov al,[esi]

To my understanding, OFFSET returns the location of the variable with respect to the program's segment. That address is stored in the esi register. Immediates are then used to access the value stored at the address represented in esi. Incrementing moves the address to the next byte.

So what is the difference between using OFFSET on an array and simply calling the array variable? I was previously lead to believe that simply calling the array variable would also give me its address.

Upvotes: 2

Views: 10648

Answers (1)

mcleod_ideafix
mcleod_ideafix

Reputation: 11418

.data
Number dd 3
.code
mov eax,Number
mov ebx,offset Number

EAX will read memory at a certain address and store the number 3

EBX will store that certain address.

mov ebx,offset Number

is equivalent in this case to

lea ebx,Number

Upvotes: 1

Related Questions