Test platform is 32 bit Linux.
Basically, I know gcc can be used to generate both Intel and At&T style assembly code, but it seems that you can not directly use nasm/tasm to compile the Intel style assembly code gcc generated.
I am conducting a project analysis asm code on both windows and Linux platform, so I am thinking if they can be both compiled by platform independent assembler like nasm\yasm, I could have a much easier time...
So my question is how to generate a nasm compilable assembly code from c source code on Linux?
Open a Linux terminal. Type whereis nasm and press ENTER. If it is already installed, then a line like, nasm: /usr/bin/nasm appears. Otherwise, you will see just nasm:, then you need to install NASM.
c is being compiled without any additional links or files, so the program will be converted into an executable by itself. Alas, for the grand finale, let's run the full shebang (no computing reference intended here), gcc main. c without any options, to preprocess, compile, assemble, and link the program all at once.
I find it's a better approach to disassemble the object files rather than use assembly code generated by gcc.
First, generate an object file from your source code:
gcc -fno-asynchronous-unwind-tables -O2 -s -c -o main.o main.c
-fno-asynchronous-unwind-tables
: do not generate unnecessary sections like .eh_frame
-O2
optimizes so the asm isn't horrible. Optionally use -Os
(size over speed) or -O3
(full optimization including auto-vectorization). Also you can tune for a CPU and and use extensions it supports with -march=native
or -march=haswell
or -march=znver1
(Zen)
-s
: make smaller executable (strip)
-c -o main.o
: compile but don't link, generate an object file called main.o
Use objconv to generate nasm
code:
objconv -fnasm main.o
The result will be stored in main.asm
.
The result will be very close to Nasm syntax. However you might need to make some minor tweaks to eliminiate warnings/errors. Simply try to compile it with Nasm
nasm -f elf32 main.asm
and fix the errors/warnings by hand. For example:
align=N
and execute
/noexecute
words from .SECTION
lines.: function
from global
declarationsdefault rel
lineLink the resulting main.o
which generated by Nasm in step 3 using gcc:
gcc main.o
You can also link it using ld but it's much harder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With