c - 汇编语言-工作原理

标签 c assembly

我真的是学习汇编语言的新手,并且刚刚开始研究汇编语言,所以我想知道也许有些人可以帮助我解决一个问题。我有一个作业,告诉我将汇编语言指令与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;
}


我认为正确的答案很有趣3 ..但我不太确定。

最佳答案

首先,欢迎使用StackOverflow。很棒的地方,确实是。

现在开始吧,让我为您服务;很多;很多。

您有很好的评论,可以极大地帮助您和我以及其他所有人,但是它们太丑陋,以致阅读起来很痛苦。

解决方法如下:空格,大量空格,空行,以及将指令分组为彼此相关的小团体。

更重要的是,在条件跳转之后,插入一条空白行,在绝对跳转之后,插入两条空白行。 (古老的技巧,可提高可读性)

其次,排列评论,使它们排列整齐。看起来好一千倍。

这是您的资料,我整理了90秒的文字。相信我,专业人员会以这种源代码更好地尊重您一千倍。

    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 ) ?
                            //   No, Think like this: "What is the value of edx with respect to the value of eax ?"

    jge .L3                 //   edx is greater, so return the value in eax as it is

    movl %edx,%eax          //   If the term is not met here it will return b
                            //   (pssst, I think you're wrong; think it through again)

    .L3:

    movl %ebp,%esp          //   Starting to finish the function
    popl %ebp               //   Putting the base pointer in the right place
    ret                     //   return


现在,回到您遇到的问题。他得到的是比较指令和相关的JGE指令的“感觉”。

这是您需要理解的困惑知识,才能在这类“学术经历”中生存

biz,cmpl %eax,%edx指令,是“比较”指令的一种形式

当您看到以下语法时,尝试形成类似这样的想法:“ ...相对于源操作数,目标​​操作数的值是多少?...”

警告:我绝对不喜欢AT&T语法,因此欢迎任何人对此进行纠正。

无论如何,在这种特定情况下,您可以像这样在您的脑海中表达这个想法...

“ ...我看到cmpl %eax,%edx,所以我认为:关于eaxedx中的值是...”

然后,您就可以通过下一条指令的“感觉”完成该句子,这是有条件的跳转。

人脑中的范式过程形成了这样的句子。

“ ...关于eaxedx中的值大于或等于,所以我跳了……”

因此,如果您对ab的位置是正确的,则可以进行范例性的脑子争夺者并获得类似的信息...

“ ...关于b中的值,a中的值大于或等于,所以我会跳...”

要对此有所了解,请注意,JGEJL的“相反含义”(即,“小于则跳转”)

好的,现在碰巧C中的return与汇编语言中的ret指令相关,但这不是一回事。

当C程序员说“ ...那个函数返回一个整数...”时,它们的意思是...


汇编语言子例程将在Eax中放置一个值
然后,该子例程将修复堆栈并以整齐的顺序放回堆栈
然后该子例程将执行其Ret指令


现在,您的脸上又出现了一种混淆。

以下这些条件跳转适用于有符号算术比较操作...


JG
JGE
JNG
JL
JLE
JNL


那里!陷阱等着你把这一切搞砸了!

您要进行有符号比较还是无符号比较?

顺便说一句,我从未见过有人像第一个函数那样将无符号数与带符号数进行比较。那合法吗?

因此,无论如何,我们将所有这些事实放在一起,就得到了:如果该汇编语言例程小于a中的值,则返回b中的值,否则返回b中的值。

这些值被评估为有符号整数。

(我认为我没错;有人检查了我的逻辑。我真的根本不喜欢那个汇编程序的语法。)

因此,无论如何,我可以肯定地说,您不想让互联网上的人为您提供针对特定作业问题的特定答案,因此,我将由您自己决定是否从此解释中找出答案。

希望我已经对比较的逻辑,比较的“意义”以及有符号的和未签名的biz进行了充分的解释,以便您可以解决这个问题。

哦,再次免责声明,我总是使用Intel语法(例如,Masm,Tasm,Nasm等),因此,如果我在这里遇到了一些问题,请随时为我更正。

关于c - 汇编语言-工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26188068/

相关文章:

c - 如何将一行整数拆分为数组?

c++ - void* 可以用来存储函数指针吗?

c - 在 C 中每 X 分钟运行一次命令

c - 从程序集调用 printf 时字符串输出两次

c++ - “lock cmpxchg”如何在汇编中工作?

c - int * 与 char * 初始化

c - 传递参数列表

gcc - 关于 gcc 生成的汇编代码(汇编代码不按顺序排列?)

macos - Segmentation Fault 11链接OS X 32位汇编器

c - 汇编程序在调用 printf 后崩溃