cmp je/jg它们在组装中的工作方式 [英] cmp je/jg how they work in assembly

查看:59
本文介绍了cmp je/jg它们在组装中的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解cmp和je/jg在组装中的工作方式.我在Google上看到的例子很少,但我仍然有些困惑.下面我展示了一部分汇编代码,我试图将其转换为C语言以及相应的C代码.是用正确的方式实施的,还是我对cmp的工作方式有错误的理解?

I would like to understand how cmp and je/jg work in assembly. I saw few examples on google but I am still little bit confused. Below I have shown a part of assembly code that I am trying to convert to C language and the corresponding C code. Is it implemented in the right way or do I have a wrong understanding of how cmp works?

cmp    $0x3,%eax
je     A
cmp    $0x3,%eax
jg     B
cmp    $0x1,%eax
je     C


 int func(int x){


  if(x == 3)
    goto A;

  if (x >3)
    goto B;


  if(x == 1)
     goto C;

    A:
    ......

    B:
    ......

    C:
    ......

推荐答案

您正确理解cmp和je/jg的工作方式,但是C代码中有错误.这行:

You understand correctly how cmp and je/jg work, but you have an error in your C code. This line:

if (*x == 1)

应该是

if (x == 1)

此处是x86控制流指令的相当不错的摘要.

Here is a pretty good summary of the x86 control flow instructions.

此外,没有理由针对相同的值重复cmp指令.执行完毕后,您可以用多种方式测试结果,而无需重复比较.因此,您的汇编代码应如下所示:

Also, there's no reason to repeat the cmp instruction for the same values. Once you've executed it, you can test the results multiple ways without repeating the comparison. So your assembly code should look like this:

cmp    $0x3,%eax
je     A
jg     B
cmp    $0x1,%eax
je     C

这篇关于cmp je/jg它们在组装中的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆