程序集添加十六进制和多个参数,然后比较

时间:2016-09-20 00:39:21

标签: assembly x86 x86-64 att

add -0x4(%r12), %eax
cmp %eax, %r12

我在装配中给了这两行。

我的猜测是你从r12中的值中减去4,然后将其添加到eax。

r12是否仍然是原始的-4,或者该值是否保持原始值?

例如,如果r12 = 5,eax = 3,则add函数将导致eax = 4;
r12仍然是5还是1?

2 个答案:

答案 0 :(得分:1)

你可以在gdb中单独执行此操作,看看它做了什么。设置gdb以显示您在最后一步中更改的注册(例如layout reg,请参阅 tag wiki的底部。)

由于%r12需要是ADD源操作数的有效指针,因此将其放在foo.S中:

.globl _start
_start:
    mov   %rsp, %r12               # added this instruction: r12 is now a valid pointer to stack memory, since we copy the stack pointer into it

    add   -0x4(%r12), %eax
    # cmp   %eax, %r12             # operand-size mismatch is an error

    cmp   %eax, %r12d              # 32-bit compare
    cmp   %rax, %r12               # 64-bit compare.  upper 32 of RAX is zero from writing EAX in the add instruction

    # your program will segfault here because we don't make an exit() system call, and instead keep executing whatever bytes are next in memory.

gcc -g -nostdlib foo.S组装它以制作静态二进制文件。 _start is the default entry point

运行gdb ./a.out

(gdb) layout reg
(gdb) b _start
(gdb) r
(gdb) si          # step instruction, 
# repeat as necessary and watch gdb highlight changed registers.

我喜欢set disassembly-flavor intel而不是AT& T语法,但如果您愿意(或想要/需要学习AT& T语法),那么就不要这样做。

提示,CMP doesn't modify either of its operands,ADD仅使用R12作为寻址模式,从中加载4个字节的源数据。

EAX的最终值取决于记忆中的内容。

答案 1 :(得分:0)

这不会改变r12;它只是用它来计算一个地址。