assembly language - c-language and mnemonics

1.6k views Asked by At

I wrote a simple program in c-language, the classic helloworld. I wanted to know how it looked liked when the compiler translated it to assembly code.

I use MinGW and the command:

gcc -S hellow.c

When I opened this file I expected it would, AT THE LEAST, be somewhat similar to a hello-world program written directly in assembly, that is:

jmp 115
db 'Hello world!$' (db = define bytes)
-a 115 
mov ah, 09 (09 for displaying strings ... ah = 'command register')
mov dx, 102 (adress of the string)
int 21
int 20

Instead it look like this:

   .file    "hellow.c"

.def    ___main;    
.scl    2;  
.type   32; 
.endef
.section 
.rdata,"dr"
LC0:

.ascii "Hello world!\0"

.text
.globl  _main

.def    _main;  
.scl    2;  
.type   32; 
.endef
_main:
 LFB6:

.cfi_startproc
pushl   %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8
movl    %esp, %ebp

.cfi_def_cfa_register 5
andl    $-16, %esp
subl    $16, %esp
call    ___main
movl    $LC0, (%esp)
call    _puts
movl    $0, %eax
leave

.cfi_restore 5

.cfi_def_cfa 4, 4
ret

.cfi_endproc
LFE6:

.def    _puts;  
.scl    2;  
.type   32; 
.endef

I know litte about assembly language, but i DO recognice the so called mnemonics like ADD, POP, PUSH, MOV, JMP, INT etc. Could not see much of these in the code generated by the c-compiler.

What did I missunderstand?

2

There are 2 answers

1
Jens Gustedt On

This prepares the arguments to call a function __main that probably does all initial setup that is needed for a C program

andl    $-16, %esp
subl    $16, %esp
call    ___main

This prepares the arguments and calls function _puts. LC0 is a symbol that contains the string to be printed.

movl    $LC0, (%esp)
call    _puts

This prepares the return value of main and returns

movl    $0, %eax
leave
ret
0
Devolus On

Your example code uses Intel syntax, while the standard output from gcc is AT&T syntax. You can change that by using

gcc -S hellow.c -masm=intel

The resulting output should look more familiar.

However, if the compiler generates the source then it looks rather different, then what you would write by hand.

The int would be used if you compile for DOS, but even so, these calls would be wrapped in C standard functions, like puts in this case.