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.

Download HitXP Mobile App on Google Play

Get it on Google Play
Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

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.

Online Education should be interactive and innovative – Fun to Learn

For online education to succeed for smaller classses - it should be as interesting as cartoons. Online education cant succeed unless it is more interesting, interactive and innovative than offline schooling.

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

Latest Music Notations

So Gaya Ye Jahan – Tezaab – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song So Gaya Ye Jahan from the 1988 Hindi movie Tezaab in Western and Indian Notations.

Background Theme (BGM) – Mouna Ragam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Background Theme (BGM) from the 1986 Tamil movie Mouna Ragam in Western and Indian Notations.

Dil Kya Kare Jab Kisi Se – Julie – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Dil Kya Kare Jab Kisi Se from the 1975 Hindi movie Julie in Western and Indian Notations.

Albela Sajan Aayo Re – Hum Dil De Chuke Sanam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Albela Sajan Aayo Re from the 1999 Hindi movie Hum Dil De Chuke Sanam in Western and Indian Notations.

Download HitXP Mobile App on Google Play

Get it on Google Play
Content Protection by DMCA.com