Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a full assembly code from C file?

I'm currently trying to figure out the way to produce equivalent assembly code from corresponding C source file.

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

I was able to output the assembly code using the -S option in gcc. However, the resulting assembly code contained call instructions which in turn make a jump to another function like _exp. This is not what I wanted, I needed a fully functional assembly code in a single file, with no dependency to other code.

Is it possible to achieve what I'm looking for?

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

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

The platform I am working on is Windows 10 64-bit, the compiler I'm using is cl.exe from MSbuild.

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.

like image 519
Kinnefix Kim Avatar asked May 08 '26 07:05

Kinnefix Kim


1 Answers

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

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.


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

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.

like image 170
Peter Cordes Avatar answered May 11 '26 04:05

Peter Cordes