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

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

问题描述

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:
    ......

解决方案

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

if (*x == 1)

should be

if (x == 1)

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

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天全站免登陆