Home Articles Software Difference between ADDR and OFFSET

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 the address is allocated during run-time 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 its 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 knows 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 run time 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 person 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 (person’s) address and I am the assembler.

Now if I know his/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 the person lives, but I know somebody who knows the address of that person, then I’ll give you the address of that somebody and ask you to checkout there for the address of the person 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 for global variables and ADDR for local variables. ADDR can also be used for global variables, but OFFSET cannot be used while referring to local variables.

Content Protection by DMCA.com

Download HitXP Mobile App

Get it on Google Play
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.


Please enter your comment!
Please enter your name here

RSS FeedSubscribe
Sound CloudFollow

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com