我试图理解 x86 汇编语言中
mov
和 lea
指令之间的差异。我编写了一个简单的汇编代码片段,并添加了注释来解释我对这些指令的理解。
.data
num: .int 2 # Declare an integer variable num with value 2
.text
.globl _start
_start:
movl num, %eax # num stores an address, take the value inside that address into eax
movl $num, %ebx # num stores an address, $ tells us to take the address itself into ebx
leal num, %ecx # num stores an address, take the address itself into ecx (lea)
#initialize
movl $0, %eax
movl $0, %ebx
movl $0, %ecx
movl $num, %edx # num stores an address, take the address itself into edx
movl %edx, %eax # edx is an address, take the value inside that address into eax
leal (%edx), %ecx # () tells us to go to the address inside edx, take the address itself into ecx (lea)
movl (%edx), %ebx # () tells us to go to the address inside edx, take the value inside that address into ebx
# Exit
movl $1, %eax # System call number for exit
xorl %ebx, %ebx # Exit status 0
int $0x80 # Invoke the syscall
但是,我对这些指令如何与寄存器一起工作有点困惑。如果有人能给我他的解释,我会很高兴。
您的大部分评论都很好。
其他人可能需要一些改写:
movl %edx, %eax # edx is an address, take the value inside that address into eax
“取该地址内的值”
这只是将 EDX 寄存器中内容的副本放入 EAX 寄存器中。在这种情况下,EDX 包含地址这一事实并不重要。
leal (%edx), %ecx # () tells us to go to the address inside edx, take the address itself into ecx (lea)
“前往里面的地址”
我们不会“去”某个地址。 CPU 计算最左边操作数代表的地址并将其存储到目标寄存器 ECX 中。