找到缺少的C代码,给定汇编代码?

Ano*_*ous 6 c x86 assembly

代码是

int f(int x, int y, int z) {
  if (/* missing code here */)
    return z;
  else
    return -z;
}
Run Code Online (Sandbox Code Playgroud)

组装是

    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %eax
    cmpl 12(%ebp), %eax
    jge .L2
    movl 16(%ebp), %eax
    jmp .L3
.L2:
    movl 16(%ebp), %eax
    negl %eax
.L3:
    popl %ebp
    ret
Run Code Online (Sandbox Code Playgroud)

这个问题要求我找出缺少的测试表达式必须产生给出的汇编代码.好的,够容易的.有一个明显的比较之间正在进行的xy.如果jge操作员要将跳跃预先形成循环体12(%ebp) > %eax.

可能的选择是

x<=y x>=y x>y x<y

我的回答是x<=y,因为它12(%ebp)是一个参考y,它是目的地.但这个答案是错误的,我不知道如何.任何提示?非常感谢.

Ric*_*ner 5

这是带注释的x86程序集:

pushl %ebp ; save the old stack movl %esp, %ebp ; set up your local, new stack movl 8(%ebp), %eax ; take the first function argument and store it into eax cmpl 12(%ebp), %eax ; compare the 2nd function arg with the 1st (in eax)

在此之后,有一个jge基本上意味着"跳过大于或等于",你可以在cmp指令之后做.

这意味着如果第一个参数大于第二个参数,它会跳转,因此,x >= y.

但是,这个跳转(到L2)实际上会否定z,然后返回z.你真正想要的是跳转到L3,如果x <y,这将是最终结果.

  • @black,假设Linux,问题中的代码似乎是32位,因此参数通常在堆栈(32位i386 ABI)上传递,这与System V 64位ABI不同,其中适合的参数可以在寄存器中传递(如果需要在堆栈上) (3认同)