【发布时间】:2014-10-03 23:25:36
【问题描述】:
我在学习汇编语言方面真的很陌生,并且刚刚开始深入研究它,所以我想知道你们中的一些人是否可以帮助我解决一个问题。我有一个家庭作业,它告诉我将汇编语言指令与 c 代码进行比较,并告诉我哪个 c 代码等同于汇编指令。所以这里是组装说明:
pushl %ebp // What i think is happening here is that we are creating more space for the function.
movl %esp,%ebp // Here i think we are moving the stack pointer to the old base pointer.
movl 8(%ebp),%edx // Here we are taking parameter int a and storing it in %edx
movl 12(%ebp),%eax // Here we are taking parameter int b and storing it in %eax
cmpl %eax,%edx // Here i think we are comparing int a and b ( b > a ) ?
jge .L3 // Jump to .L3 if b is greater than a - else continue the instructions
movl %edx,%eax // If the term is not met here it will return b
.L3:
movl %ebp,%esp // Starting to finish the function
popl %ebp // Putting the base pointer in the right place
ret // return
我试图根据我对此的理解将其评论出来 - 但我可能完全错了。假设其中之一等效的 C 函数的选项是:
int fun1(int a, int b)
{
unsigned ua = (unsigned) a;
if (ua < b)
return b;
else
return ua;
}
int fun2(int a, int b)
{
if (b < a)
return b;
else
return a;
}
int fun3(int a, int b)
{
if (a < b)
return a;
else
return b;
}
我认为正确的答案是 fun3 .. 但我不太确定。
【问题讨论】:
-
@Puciek:实际上,OP 确实有 cmets 显示对生成的指令的假定解释。
-
这就像一个 PHP 开发人员从他们的编码枪中拿出所有的钱并用百分比代替它们。
-
@MartinJames:你喝醉了。去睡觉... ;)
-
@SaniHuttunen - 与其假设我无法学习这一点并且将无法通过课程,不如获得一些帮助。
-
哇,这里有一些真正的混蛋。坚持下去,SO 通常比那更好。