Hello, I am trying to convert a small C file to nasm.
Here is my C file:
http://pastebin.com/KZ6L2TK4It works fine when compiling with the following command:
gcc -lglfw -lGL -lm -m32 -o main main.c
I'll be using "-m32" to generate 32 bit code all along for simplicity.
Now the conversion part.
Using gcc's "-S -masm=intel" argument I first generate an assembly file with intel syntax:
gcc -m32 -S -masm=intel -Os -o main.intel-gcc.s main.c
Here's the result:
http://pastebin.com/ZNwr9S9aThis is gcc's intel syntax, which differs a bit from nasm's syntax. So I try to adapt it to nasm syntax and remove unncessary parts. I wrote the following sed script to do the trick:
http://pastebin.com/vW53pZDV. That does most of the work but the result isn't parseable by Nasm yet.
Here's the result:
http://pastebin.com/Dk1rm4FgThe rest of the work must be done manually, as I don't know enought sed to automate that.
Next, we must take care of segments. Just try to comile with nasm and examine the output. I use the following command:
nasm -f elf32 -o main.o main.nasm
I put .text segment at top - just rename ".text" to "section .text". Next, move the offending ".section .rodata etc" segments to the end of the file. Also delete repeating ".text" and ".section .text.startup etc" lines. Then, rename the ".data" line to ".bss" and in that segment rename all dd instructions to resd, and all db instructions to resb. Also remove .align lines from there. Next, insert a line "section .data" at top of the all .rodata segments we moved to end of the file eariler, and delete those ".section .rodata etc" lines.
Here's the result:
http://pastebin.com/XrtMm6LFNext, Nasm needs to know about external library functions used, such as strcpy and exit. I manually copied their names and put them at top of the file with extern declaration. Finally, Nasm needs a "global _start" line.
Here's the final result:
http://pastebin.com/EbUyEfcbFor some reason, I am getting many warnings like this: main.c.nasm:158: warning: dword data exceeds bounds
Is this because I assigned wrong storage size to my variables? Also what segment do I put intialized variables in (not constants)?Finally, link the resulting main.o with the following command:
ld -s -dynamic-linker\
/usr/lib32/ld-linux.so.2\
-L/usr/lib32\
-lc -lm -lglfw -lGL -m elf_i386\
-o main \
main.o
./main - and I get this line as output: zsh: killed ./main
If someone would be kind enough to look into my steps and point out my mistakes or offer suggestions I would be very thankful.
I am using Arch Linux x64
gcc version: 4.8.2
nasm version: 2.10.09
ld version: 2.23.2
As a side note, anti-spam methods on this forum are really annoying and confusing. I had to find a mouse! Then spent half an hour before realizing that after assembling the picture I should click register as there is no indicator of correctness or auto-verification.