assembly - IA32 组件 : lea instruction

标签 assembly x86

我知道我要说的这句话可能是让我很快在 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/

相关文章:

python - 一次加法需要多少 CPU 周期?

linux - 使用 as 和 ld 在 x86-64 Linux 上汇编和运行 i386 asm 程序

c++ - 访问冲突 _mm_store_si128 SSE Intrinsics

c - IA32 (x86) 机器上的数据类型定义

c - 在 MIPS 程序集中交换数组中的元素? (比较清楚的)

assembly - 如何获取 VESA BIOS 信息

performance - 现代 PC 视频硬件是否支持硬件中的 VGA 文本模式,或者 BIOS 是否模拟它(使用系统管理模式)?

ubuntu - 汇编代码故障

c - 整数加法中多余的中间存储指令

c - 气体组件 x86。将字符转换为二进制字符串