汇编语言 - 工作原理

drl*_*ifz -1 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
Run Code Online (Sandbox Code Playgroud)

我试图根据我对此的理解来评论它 - 但我可能完全错了.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;
}
Run Code Online (Sandbox Code Playgroud)

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

Use*_*r.1 6

首先,欢迎来到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
Run Code Online (Sandbox Code Playgroud)

现在,回到你手头的问题.他所得到的是比较指令和相关JGE指令的"意义" .

这是你需要理解的迷茫的东西,以便在这些"学术经历"中生存下来

这个cmpl %eax,%edx指令是" 比较 "指令的一种形式

当你看到语法时,尝试形成这样的想法,"...... 目标操作数相对于源操作数的值是多少? ......"

警告:我对AT&T语法完全不满意,所以欢迎任何人在此纠正我.

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

"......我认为cmpl %eax,%edx我认为:关于eax,价值edx ......"

然后用你对下一条指令的"意义"来完成你心中的那个句子,这是一个有条件的跳跃.

人脑中的范式过程可以形成这样的句子......

"...... 关于eax,值edx是大于或等于,所以我跳 ......"

所以,如果你对于a和的位置是正确的b,那么你可以做范式的大脑扰乱器并得到这样的东西......

"...... 就价值而言b,该值a大于或等于,所以我会跳 ......"

要掌握这一点,请注意,JGE如果你愿意,那就是"相反的意义" JL(即"如果小于"则跳跃)

好吧,现在碰巧return在C中ret汇编语言中的指令有关,但它不是一回事.

当C程序员说"...... 该函数返回一个int ......"时他们的意思是......

  • 汇编语言子例程将放入一个值 Eax
  • 然后子程序将固定堆栈并将其放回整齐的顺序
  • 然后子程序将执行其Ret指令

现在,还有一件混淆的物品被扔在你的脸上.

以下条件跳转适用于有符号算术比较运算......

  • JG
  • JGE
  • JNG
  • JL
  • JLE
  • JNL

它就是!陷阱等着搞砸你了!

你想做签名还是未签名比较???

顺便说一下,我从来没有见过任何人做过第一个函数,其中无符号数与有符号数进行比较.这甚至合法吗?

所以无论如何,我们将所有这些事实放在一起,我们得到:如果它小于值,a则此汇编语言例程返回值,b否则返回值b.

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

(我想我做对了;有人检查我的逻辑.我真的不喜欢汇编程序的语法.)

所以,无论如何,我有理由相信你不想让互联网上的人向你提供你特定家庭作业问题的具体答案,所以我将由你来解释这个问题.

希望我已经解释了足够的逻辑和比较的"意义"以及签名和未签名的商业,以便您可以了解这一点.

哦,再次免责声明,我总是使用英特尔语法(例如,Masm,Tasm,Nasm,等等),所以如果我在这里得到了一些东西,请随时为我纠正.