In the initial days when I started writing assembly programs on my own I used to get confused as to when to use ADDR and when to use OFFSET in the program. This article is an attempt to clear the doubts of assembly programmers regarding the meaning and usage of ADDR and OFFSET.
First and foremost, the purpose of using either ADDR or OFFSET is to get the memory address of variables during program execution.
Now, we know that variables in any assembly program are of two types, i.e. local and global variables.
While global variables remain in the memory throughout the execution of the program, local variables exist only during the execution of the functions in which they are declared and will be removed from the stack memory once the function in which they are declared completes is execution.
Since the global variables exist in memory throughout the lifetime of a program’s execution, their memory address is allocated during assembly time by the assembler. The assembler knows the exact location of the global variable’s memory address during assembly time.
In case of local variables, the assembler has no idea about the address of the variable as it’s address is allocated during runtime in the stack as and when the function in which it is declared is executed.
now coming back to our assembler instructions, OFFSET will get the address of a variable which already has it’s address allocated. This in turn means, OFFSET could be used to get the address of global variables only. We cannot receive the address of a local variable by using OFFSET as the address of a local variable is not decided during assembly time.
To overcome this difficulty we have ADDR instruction. This instruction should be used if we want to retrieve the address of a local variable.
Now naturally a question arises as to how does ADDR know the address of a local variable while OFFSET cannot. Well, even ADDR will not know the actual address of a local variable as it is referred during assembly time. What ADDR actually does is a simple substitution in the code as follows, just before the function is executed.
lea eax, localvar
What really this means is that ADDR causes the address of the local variable which is generated during runtime to be returned. lea is used to refer to the stack memory. LEA means Load Effective Address! It is used to load variables from the stack.
If you still did not get it, then imagine a situation as follows.
I am standing somewhere on the street there and you come to meet me there in search of the address of a beautiful girl which you feel I know. So, now your asking me of the address could be considered as the assembly time of the program, you are the assembly program in search of the (girl’s) address and I am the assembler.
Now if I know her exact address I’ll give it to you: with perfect street address, door number, etc. This is what OFFSET does.
Now if I don’t know where she lives, but I know somebody who I know knows the address of that girl, then I’ll give you the address of that somebody and ask you to checkout there for the address of the girl you are searching for. That’s what ADDR does. So it’s clear that even ADDR doesn’t have the exact address of the variable.
Now that we clearly know when to use ADDR and OFFSET, another question arises. Can we use ADDR to load global variables????
Yes, of course! If you are referring to global variables using ADDR, then ADDR simply substitutes is as following.
mov eax, 3000h
where 3000h is the actual address of the global variable. Remember, the actual address of a global variable is known during assemble and link time.
But then, why does ADDR use LEA instead of MOV in case of local variables. Well, for the simple reason that
is an invalid CPU instruction. Note that EBP also known as base address is the register used to access stack, and it is in stack where the local variables are stored.
Hence, LEA is used by ADDR in case of local variables.
So it is clear that OFFSET is to be used to global variables and ADDR for local variables. ADDR could ALSO be used while referring to global variables, BUT OFFSET cannot be used while referring to local variables.