Difference Between ADDR and OFFSET

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 
push eax

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 

mov eax,ebp+2

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.

Still any doubts? Feel free to mail me to the address on the RHS below. But please do not ask the address of any beautiful girl :-) I won't give it even if I get one ever.

http://www.hitxp.com/comp/pro/asm/120403.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值