我知道我要说的这句话可能是让我很快在 StackOverflow 上变得非常不受欢迎的最好方法。无论如何我都会说:为什么这不起作用(完全)?
我试图弄清楚lea/leal指令的作用。按照我的理解,lea/leal 找出第一个操作数的内存地址并将该地址写入第二个操作数(可能是寄存器等)。
这部分似乎有效。当我运行下面的程序时,它显示:
The memory address of var is 134518204.
但是,紧接着它会显示“内存访问错误”之类的内容。 pushl (%eax) 显然不起作用。为什么不呢?
.data
var: .long 42
str1: .string "The memory address of var is %d.\n"
str2: .string "At this memory address we find var's value: %d.\n"
.text
.global main
main:
leal var, %eax # Copy the address of var into %eax
pushl %eax
pushl $str1
call printf # Print str1
pushl (%eax)
pushl $str2
call printf # Print str2
# Some stack cleaning should be here :)
movl $1, %eax
int $0x80
我什至不确定我是否正确理解了 lea/leal 的做法。感谢帮助。 ;)
最佳答案
The way I understand it, lea/leal finds out the memory address of the first operand and writes this address into the second operand (which might be a register or so).
听起来足够准确。它用于执行地址运算;这可以像加载地址一样简单,但您也可以使用它来执行某些常量的乘法。
<小时/>
pushl (%eax)
obviously doesn't work. Why not?
您的 push
指令未引用您认为的地址:printf
返回写入的字符数,并且该值在 %eax 中返回
注册,因此%eax
不再包含var
的地址。
关于assembly - IA32 组件 : lea instruction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743569/