比较2号与装配 [英] compare 2 numbers with assembly

查看:143
本文介绍了比较2号与装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code。我想完成组装code表示如下:

I have the following code. And I want to complete the assembly code as indicated below:

int main(void)
{
    int x = 10;

    int i=0;
    label1:


    asm (.....) // code to add here: if i>=x then jump to label2

    printf("%d\n",i);

    i++;
    asm (.....) // code to add here: jump to label 1
    label2:

    printf("out\n");
}

我的机器是x86和操作系统是Ubuntu的12

My machine is x86 and the Operating system is Ubuntu 12

推荐答案

首先,让自己的x86运算codeS的名单,应该很容易在网上找到。

First, get yourself a list of x86 op codes, should be easy to find online.

ASM()函数如下顺序是:

asm ( "assembly code"
           : output operands                  /* optional */
           : input operands                   /* optional */
           : list of clobbered registers      /* optional */
);

第二,你有一个主要的问题是你不能跳到一个C标签,你需要让你的标签的集结号标签可以跳转到它。例如:

Second, one major problem you have is you can't "jump" to a C label, you need to make your label an "assembly" label to be able to jump to it. ex:

int main()
{
  asm("jmp .end");    // make a call to jmp there
  printf("Hello ");
  asm(".end:");       //make a "jumpable" label
  printf("World\n");
  return 0;
}

本程序的输出是简单的世界,我们跳过了你好。下面是相同的例子,但与比较跳跃:

Output of this program is simply "World" as we jumped over the "Hello ". Here's the same example but with a comparative jump:

int main()
{
    int x = 5, i = 0;
    asm(".start:");
    asm("cmp %0, %1;"   // compare input 1 to 2
        "jge .end;"     // if i >= x, jump to .end
        :               // no output from this code
        : "r" (x), "r" (i));  // input's are var x and i
    printf("Hello ");
    i++;
    asm("jmp .start;");    
    asm(".end:");
    printf("World\n");
    return 0;
}

这篇关于比较2号与装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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