Questo è il mio programma in C usando puts()
:Qual è la differenza tra put e printf in C compilato in linguaggio Assembly
#include <stdio.h>
int main(void){
puts("testing");
}
Dopo aver utilizzato gcc -S -o sample.s sample.c
a compilato in Assemblea, questo è quello che ho ottenuto:
.file "sample.c"
.section .rodata
.LC0:
.string "testing"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $.LC0, (%esp)
call puts
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"
.section .note.GNU-stack,"",@progbits
ho fatto lo stesso modo in cui, questa volta stavo usando printf()
invece degli attuali e questo è quello che ho ottenuto:
.file "sample.c"
.section .rodata
.LC0:
.string "testing"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $.LC0, %eax //this is the difference
movl %eax, (%esp)
call printf
leave
ret
.size main, .-main
.ident "GCC: (GNU) 4.4.5 20110214 (Red Hat 4.4.5-6)"
.section .note.GNU-stack,"",@progbits
Ecco quello che non capisco, la funzione printf()
mov $.LC0
-%eax
, poi mov %eax
-(%esp)
mentre la funzione mov %.LC0
puts()
direttamente a (%esp)
. Non so perché sia così.
Probabilmente è perché 'printf' è una funzione varargs, e ottengono chiamato in modo diverso dalle funzioni che hanno un numero fisso di argomenti. – Barmar
È interessante notare che, usando clang su os x, entrambi i programmi vengono compilati nello stesso assembly. – Leandros
Anche perché GCC sta compilando il codice dipendente dalla posizione su linux? – Leandros