如何从c文件中获取完整的汇编代码? [英] How do I get a full assembly code from c file?

查看:397
本文介绍了如何从c文件中获取完整的汇编代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试从相应的c源文件中产生等效的汇编代码的方法. (请原谅我不会流利的英语,我是新来的英语.)

I'm currently trying to figure out the way to produce equivalent assembly code from corresponding c source file. (pardon me for not being able to speak English fluently, I'm new to English.)

我使用C语言已经有好几年了,但是汇编语言的经验却很少.

I've been using C language for several years, but have little experience to assembly language.

我能够在gcc中使用S选项输出汇编代码.但是,生成的汇编代码包含调用指令,该调用指令又跳转到另一个函数,如_exp.这不是我想要的,我需要一个功能完整的汇编代码放在一个文件中,而不必依赖于其他代码.

I was able to output the assembly code using S option in gcc. However, the resulted assembly code contained call instruction which in turn makes a jump to another functions like _exp . This is not what I wanted, I needed a fully functional assembly code in a single file, with no dependency to other codes.

是否有可能实现我想要的? 希望得到答案,我将不胜感激!

Is it possible to achieve what I'm looking for? I would much be appreciated to get an answer!

为了更好地描述问题,我在这里向您展示我的代码:

To better describe the problem, I'm showing you my code here:

#include <math.h>
float sigmoid(float i){
    return 1/(1+exp(-i));
}

是的,它令人尴尬地简短,我应该在我第一次提出这个问题时就提供它.我还想补充一点,我目前正在使用的平台是Windows 10 64bit,我正在使用的编译器是MSbuild的cl.exe.再次感谢您!

And yes, it is embarrassingly short, I should have provided it at the first time I opened this question. I would also like to add that the platform I am currently working on is Windows 10 64bit, the compiler I'm using is cl.exe from MSbuild. Once again, thanks in advanced!

我现在看到我没有清楚描述我想要的东西.对此我感到抱歉.无论如何,我的最初目标是尽可能最低地了解计算机如何计算数学函数.我决定观察计算过程的级别是汇编代码,而我选择的数学函数是如上所述的S形.

Edit 2: I now see that I didn't clearly describe what I wanted. I'm sorry for that. Anyway, my initial objective was to see, at a lowest level possible, how computers calculate mathematical functions. The level where I decided to observe the calculation process is assembly code, and the mathematical function I've chosen was sigmoid defined as above.

推荐答案

_exp是标准的数学库函数

_exp is the standard math library function double exp(double); apparently you're on a platform that prepends a leading underscore to C symbol names.

给出一个调用某些库函数的.s,以与调用库函数的.c文件相同的方式构建它:

Given a .s that calls some library functions, build it the same way you would a .c file that calls library functions:

gcc foo.S -o foo  -lm

默认情况下,您将获得一个动态可执行文件.

You'll get a dynamic executable by default.

但是,如果您确实希望将 all 的代码放在一个没有外部依赖项的文件中,则可以将.c链接到一个静态可执行文件中,然后反汇编它. >

But if you really want all the code in one file with no external dependencies, you can link your .c into a static executable and disassemble that.

gcc -O3 -march=native foo.c -o foo -static -lm
objdump -drwC -Mintel foo > foo.s

不能保证libm.a(静态库)中的_exp实现与您在libm.solibm.dll或其他版本中获得的实现相同,因为它是一个不同的文件.对于memcpy之类的功能尤其如此,其中动态链接程序技巧通常在运行时用于选择最佳版本(针对您的CPU).

There's no guarantee that the _exp implementation in libm.a (static library) is identical to the one you'd get in libm.so or libm.dll or whatever, because it's a different file. This is especially true for a function like memcpy where dynamic-linker tricks are often used to select an optimal version (for your CPU) at run-time.

这篇关于如何从c文件中获取完整的汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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