Click here to Skip to main content
14,267,453 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am confused about the concept of the Offset Operator

Here is an example from my textbook (suppose bVal was located at offset 00404000):

.data
bVal Byte ?

.code
mov esi, OFFSET bVal ; ESI = 00404000

From what I understand from this example is that OFFSET simply returns the address of any variable, such as bVal.

This is ok so far, but the next example shows that OFFSET does something else, which is confusing.

Example:

.data
byteVal BYTE 10h

.code
mov esi, OFFSET byteVal
mov al, [esi] ; AL = 10h

Why is al = 10h? shouldn't AL contain the address of bVal, according to the previous example?

Also, how is it possible that esi can be moved to al, which is a smaller size than esi?

What I have tried:

I have tried to understand what the textbook is saying but the examples show contradicting information.
Posted
Updated 10-Jun-16 14:58pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The difference is between using the value directly or from memory address.

mov esi, OFFSET byteVal
Here the address of byteVal will be put into register esi (assume 00404000 as in the example)

mov al, [esi] ; AL = 10h
Here the esi is put between brackets, like [esi], meaning not to use the value directly (value is 00404000) but use it as an address and load the byte from that memory into register AL.

So the meaning changes using the brackets. An easier example would be:
Lets say that on address 1 in memory there is a value 5 stored.
Lets also say that AL is 0 (using for example XOR AL, AL)
When you would execute:
ADD AL, 1; AL would be 1 (0 + 1 = 1)

But, lets clear AL to 0 again and now do:
ADD AL, [1]; AL would now be 5. Instead of using value 1 directly it uses the value from memory address 1, which is 5.

Hopefully this will make it somewhat more clear.

Good luck!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100