How to compile asm created by GCC?

I play with some asm code and something bothers me.

I will compile this:

#include <stdio.h> int main(int argc, char** argv){ printf("Hello World\n"); return 0; } 

with gcc file.c -S -o file.S this generates a nice bit of asm code:

  .cstring LC0: .ascii "Hello World\0" .text .globl _main _main: LFB3: pushq %rbp LCFI0: movq %rsp, %rbp LCFI1: subq $16, %rsp LCFI2: movl %edi, -4(%rbp) movq %rsi, -16(%rbp) leaq LC0(%rip), %rdi call _puts movl $0, %eax leave ret LFE3: .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support EH_frame1: .set L$set$0,LECIE1-LSCIE1 .long L$set$0 LSCIE1: .long 0x0 .byte 0x1 .ascii "zR\0" .byte 0x1 .byte 0x78 .byte 0x10 .byte 0x1 .byte 0x10 .byte 0xc .byte 0x7 .byte 0x8 .byte 0x90 .byte 0x1 .align 3 LECIE1: .globl _main.eh _main.eh: LSFDE1: .set L$set$1,LEFDE1-LASFDE1 .long L$set$1 LASFDE1: .long LASFDE1-EH_frame1 .quad LFB3-. .set L$set$2,LFE3-LFB3 .quad L$set$2 .byte 0x0 .byte 0x4 .set L$set$3,LCFI0-LFB3 .long L$set$3 .byte 0xe .byte 0x10 .byte 0x86 .byte 0x2 .byte 0x4 .set L$set$4,LCFI1-LCFI0 .long L$set$4 .byte 0xd .byte 0x6 .align 3 LEFDE1: .subsections_via_symbols 

My next problem is actually, how do I compile this output, and can I make GCC for this?

+46
c assembly gcc compiler-construction
Aug 25 2018-11-12T00:
source share
7 answers

Yes, you can use gcc to compile your asm code. Use -c to compile as follows:

 gcc -c file.S -o file.o 

This will give an object code file called file.o. To invoke the linker, run the following command:

 gcc file.o -o file 
+61
Aug 25 2018-11-12T00:
source share

gcc can use the assembly file as input and activate the assembler if necessary. However, there is a subtlety:

  • If the file name ends with " .s " (lowercase '), then gcc calls the assembler.
  • If the file name ends with " .s " (upper case "S"), then gcc uses the C preprocessor in the source file (that is, it recognizes directives such as #if and replaces macros) and then calls the assembler for the result.

So, on a general basis, you want to do something like this:

 gcc -S file.c -o file.s gcc -c file.s 
+35
Aug 25 '11 at 12:40
source share

You can embed assembly code in a regular C program. Here is a good introduction . Using the appropriate syntax, you can also specify the GCC with which you want to interact with variables declared in C. The program below tells gcc that:

  • eax should be foo
  • ebx should be bar
  • the value in eax should be stored in foo after the assembly code is executed

\ P

 int main(void) { int foo = 10, bar = 15; __asm__ __volatile__("addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) ); printf("foo+bar=%d\n", foo); return 0; } 
+7
Aug 25 2018-11-12T00:
source share

Yes, gcc can also compile assembly source code. Alternatively, you can call as , which is an assembler. (gcc is just a β€œdriver” program that uses heuristics to invoke the C compiler, C ++ compiler, assembler, linker, etc.)

+3
Aug 25 '11 at 12:12
source share

You can use GAS, which is a gcc assembler:

http://linux.die.net/man/1/as

+3
Aug 25 '11 at 12:14
source share

If you have a main.s. file you can generate a GCC object file as well as

 # gcc -c main.s # as main.s -o main.o 

check this link, it will help you find out some binutils from GCC http://www.thegeekstuff.com/2017/01/gnu-binutils-commands/

+1
Apr 13 '17 at 8:53 on
source share
 nasm -f bin -o 2_hello 2_hello.asm 
0
Nov 10 '14 at 11:40
source share



All Articles