I try to learn assembly language as a hobby, and I often use gcc -S to build the assembly. It is pretty simple, but I can not compile the assembly. I was curious if this could be done at all. I tried using standard assembly output and syntax syntax using -masm=intel . Both cannot be compiled with nasm and are associated with ld .
Therefore, I would like to ask if it is possible to generate assembly code, which can then be compiled.
To be more precise, I used the following C code.
>> cat csimp.c int main (void){ int i,j; for(i=1;i<21;i++) j= i + 100; return 0; }
Generated assembly with gcc -S -O0 -masm=intel csimp.c and tried to compile with nasm -f elf64 csimp.s and associate with ld -m elf_x86_64 -s -o test csimp.o . The result I got from nasm reads:
csimp.s:1: error: attempt to define a local label before any non-local labels csimp.s:1: error: parser: instruction expected csimp.s:2: error: attempt to define a local label before any non-local labels csimp.s:2: error: parser: instruction expected
This is most likely due to violation of assembly syntax. I hope I can fix this without resorting to gcc -S manual output correction
Edit
They gave me a hint that my problem was solved in another question; Unfortunately, after testing the method described there, I was unable to create the nasm assembly nasm . You can see the objconv result below. Therefore, I still need your help.
>>cat csimp.asm ; Disassembly of file: csimp.o ; Sat Jan 30 20:17:39 2016 ; Mode: 64 bits ; Syntax: YASM/NASM ; Instruction set: 8086, x64 global main: ; **the ':' should be removed !!!** SECTION .text ; section number 1, code main: ; Function begin push rbp ; 0000 _ 55 mov rbp, rsp ; 0001 _ 48: 89. E5 mov dword [rbp-4H], 1 ; 0004 _ C7. 45, FC, 00000001 jmp ?_002 ; 000B _ EB, 0D ?_001: mov eax, dword [rbp-4H] ; 000D _ 8B. 45, FC add eax, 100 ; 0010 _ 83. C0, 64 mov dword [rbp-8H], eax ; 0013 _ 89. 45, F8 add dword [rbp-4H], 1 ; 0016 _ 83. 45, FC, 01 ?_002: cmp dword [rbp-4H], 20 ; 001A _ 83. 7D, FC, 14 jle ?_001 ; 001E _ 7E, ED pop rbp ; 0020 _ 5D ret ; 0021 _ C3 ; main End of function SECTION .data ; section number 2, data SECTION .bss ; section number 3, bss
Explicit solution:
I made a mistake while cleaning objconv output. I had to run:
sed -i "s/align=1//g ; s/[az]*execute//g ; s/: *function//g; /default *rel/d" csimp.asm
All steps can be compressed in a bash script
#! /bin/bash a=$( echo $1 | sed "s/\.c//" )
Running this code. I get ld warning:
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400080
An executable file executed in this way fails with a segmentation fault message. I would be grateful for your help.