The Difference between LEA and MOV OFFSET

Posted in kerneltrap.org on January 24, 2010 – 12:33am

The other day a friend of mine asked me to code a decimal-to-hex-converter in MASM using DOS ISRs (Interrupt Service Routine) for I/O. Well, I just gave her the equivalent C code since I am not into proprietary software. But, anyway I googled for some materials related to MASM syntax and DOS ISRs for I/O [0]. Upon reading some examples of using DOS ISR 21h, I stumbled upon the use of LEA dx, var and MOV dx, OFFSET var. So, I decide to have a look into them and come up with the following conclusion.

First, the keyword OFFSET is not an x86 instruction. It is a keyword in MASM to take the address of a variable [1][2]. In C, the OFFSET keyword of MASM is the equivalent of & in, for example, scanf("%d", &var).

Second, for the simplest case of moving an address into a register, the following instructions have the same effect [3][27]:

mov dx, offset var
lea dx, var

Finally, in addition to its original purpose of doing pointer arithmetic, however, LEA can be harnessed to perform integer addition and multiplication in a way that is faster than using ADD and MUL [4] and in a way that allows the result to be stored in a register other than the source register [5].

Advertisements
This entry was posted in Assembly, Programming Languages and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s