代码是
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)
这个问题要求我找出缺少的测试表达式必须产生给出的汇编代码.好的,够容易的.有一个明显的比较之间正在进行的x和y.如果jge操作员要将跳跃预先形成循环体12(%ebp) > %eax.
可能的选择是
x<=y
x>=y
x>y
x<y
我的回答是x<=y,因为它12(%ebp)是一个参考y,它是目的地.但这个答案是错误的,我不知道如何.任何提示?非常感谢.
这是带注释的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,这将是最终结果.