asm: difference between LEA and MOV

本文探讨了汇编语言中LEA与MOV指令的区别及应用。LEA用于加载地址到寄存器,而MOV用于复制数据值。文章还介绍了如何使用LEA进行指针算术和整数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The difference between LEA and MOV is roughly equivalent to the use of
the "&" operator in C. Specifically:

; eax = &A;

lea eax, A

; eax = A;

mov eax, A

Cheers,
Randy Hyde

 

 

TkW wrote:


Ok.
MOV A, EAX <- Moves the value pointed in the address


Depends on the declaration of A. If defined as follows:

A dword ?

Then the mov instruction above moves a copy of the value held in EAX
into the memory location associated with the variable A.


But what about:
MOV A, [EAX] ?


If A is a memory location, defined as above, this is an illegal
instruction. You cannot move the contents of one memory location (e.g.,
the dword at the address held in the EAX register) into another memory
location (A).


This instruction, for me, seems to be same as LEA A, EAX.


This is not a syntactically correct instruction.
LEA eax, A
is okay, which loads the address of the A variable into EAX.

My biggest doubt is with parentheses.


When brackets surround a 32-bit register, this implies *indirection*.
That is, the register's value (EAX's value in this case) is a 32-bit
address of some other object in memory and the instruction uses that
address to reference memory. If the brackets are not present, then
this tells the assembler to use the value of the register directly.
E.g.,

mov ebx, 12345678h
mov eax, ebx ;Loads eax with a copy of EBX's value (12345678h)
mov eax, [ebx] ;Loads eax with the dword at address 12345678h in
memory.

The difference between LEA and MOV is roughly equivalent to the use of
the "&" operator in C. Specifically:

; eax = &A;

lea eax, A

; eax = A;

mov eax, A

Cheers,
Randy Hyde

 

 

 

http://kerneltrap.org/node/51523

The Difference between LEA and MOV OFFSET

Submitted by Eus
on January 23, 2010 - 10: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].

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值