从c到使用AT& T语法的汇编语言

时间:2018-05-03 16:02:17

标签: assembly x86 stack getchar

我写了一个非常简单的C程序,它将字符打印到标准输出

#include <stdio.h>


int c;

int main(){


 while ((c = getchar ()) != EOF) {

putchar(c);

}

return 0;
}

然后我用gcc -S -m32 -fno-pic -fno-asynchronous-unwind-tables lauf.c将它转换为汇编语言并得到了这个

.file   "lauf.c"
.comm   c,4,4                 // global variable c with 4 bytes .. 
                               The second 4 means alignment
                               but what exactly does it mean? 
.text
.globl  main
.type   main, @function

main:
leal    4(%esp), %ecx  
andl    $-16, %esp
pushl   -4(%ecx)
pushl   %ebp                   // start of stack frame
movl    %esp, %ebp             // gives stackpointer the value of the 
                                   basepointer
pushl   %ecx
subl    $4, %esp                   //end of stackframe
jmp .L2

.L3:
movl    c, %eax
subl    $12, %esp    // why does the compiler subtract 12 from esp?
pushl   %eax
call    putchar
addl    $16, %esp    // why esp +16?

.L2:
call    getchar    // calls getchar but where is c assigned to getchar?
movl    %eax, c    
movl    c, %eax    
cmpl    $-1, %eax   
jne .L3                     
movl    $0, %eax
movl    -4(%ebp), %ecx
leave
leal    -4(%ecx), %esp
ret
.size   main, .-main
.ident  "GCC: (Ubuntu 4.9.4-2ubuntu1) 4.9.4"
.section    .note.GNU-stack,"",@progbits

我在汇编文件中添加了一些问题。 有人可以解释汇编文件中究竟发生了什么吗?

0 个答案:

没有答案