在Linux中环境中运行行内汇编(使用GCC / G ++) [英] Running In-Line Assembly in Linux Environment (Using GCC/G++)

查看:159
本文介绍了在Linux中环境中运行行内汇编(使用GCC / G ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个行内汇编编码部分写在C(.c文件)一个非常基本的程序。我想.c文件转换成汇编输出,我知道,但不知道如何来编译code为Linux环境。

在使用gcc或g ++的.cpp文件,我得到的错误不承认汇编指令。

现在按预期在Visual Studio中,除了我改变括号内为ASM code到括号这个code工作。不过,我仍然得到错误。给变量未定义的引用一堆。

我从工作code制成正在改变括号括号,投入引号汇编指令(在网上找到的,可能是错误的)的变化。

总之,我希望code以下能够被使用命令gcc Linux环境成功编译。我不知道语法,但code ++工程,只是不为Linux /.

 的#include<&stdio.h中GT;
诠释的main(){浮NUM1,NUM2,金额,产品;
浮动金额,产品;
浮F1,F2,F3,FSUM,FMUL;的printf(请输入两个浮点数:\\ n);
scanf函数(%F%F,&安培; NUM1,&安培; NUM2);
__asm​​__

    FLD NUM1;
    FADD NUM2;
    FST FSUM;
);的printf(%f和%f的总和,是,%F \\ N,NUM1,NUM2,FSUM);
的printf(十六进制等价的数量和金额是%X%+ X =%X \\ n,NUM1,NUM2,FSUM);返回0;
}


解决方案

在网上装配GCC是直译到生成的汇编代码;因为在装配中不存在的变量,有没有办法,你已经写了可以工作。

的方法,使其工作就是使用扩展组装,其中标注装配与GCC将用来组装转换时的源代码编译的改性剂。

  __ asm__

  FLD%1 \\ n \\ t的
  FADD%2 \\ n \\ t的
  FST%0
  := F(FSUM)
  :F(NUM1),F(NUM2)
  :
);

So I have a very basic program written in C (.c file) with an in-line assembly coding part. I want to convert the .c file into assembly output which I know but don't know how to compile that code for a Linux environment.

When using gcc or g++ for .cpp files, I get errors not recognizing asm instructions.

Now this code works as intended in Visual Studio besides me changing the brackets for the asm code to parenthesis. However I still get errors. Bunch of undefined references to the variables.

The changes I made from the working code are changing brackets to parentheses, putting assembly instruction in quotation marks (found online, could be wrong).

In short, I want the code below be able to be compiled successfully in a linux environment using the command gcc. I don't know the syntax but the code works, just not for linux/.

#include <stdio.h>
int main()

{

float num1, num2, sum, product;
float sum, product;
float f1, f2, f3, fsum, fmul;

printf("Enter two floating point numbers: \n");
scanf("%f %f", &num1, &num2);


__asm__ 
(
    "FLD num1;"
    "FADD num2;"
    "FST fsum;"
);

printf("The sum of %f and %f " "is" " %f\n", num1, num2, fsum);
printf("The hex equivalent of the numbers and sum is %x + %x = %x\n", num1, num2, fsum);

return 0;
}

解决方案

In-line assembly in GCC is translated literally to the generated assembly source; since the variables don't exist in the assembly, there's no way what you have written can work.

The way to make it work is to use extended assembly, which annotates the assembly with modifiers that GCC will use to translate the assembly when the source is compiled.

__asm__
(
  "fld %1\n\t"
  "fadd %2\n\t"
  "fst %0"
  : "=f" (fsum)
  : "f" (num1), "f" (num2)
  :
);

这篇关于在Linux中环境中运行行内汇编(使用GCC / G ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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