####
c程序代码:

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>  

int main(void)
{

int i=0, j=0;

for(i=0; i<8; i++)
j=j+1;

return 0;
}

汇编代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
.file   "test_asm.c"  
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
movl $0, -8(%ebp)
movl $0, -4(%ebp)
movl $0, -8(%ebp)
jmp .L2
.L3:
addl $1, -4(%ebp)
addl $1, -8(%ebp)
.L2:
cmpl $7, -8(%ebp)
jle .L3
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3"
.section .note.GNU-stack,"",@progbits

汇编代码讲解

  • 第8行:将栈基址指针(存在ebp寄存器)推入栈
  • 第11行:将栈指针移入基址指针(esp为基址寄存器)
  • 第13行:从ebp开始分配16字节的内存
  • 第14,15,16行:将i, j赋初值0
  • 第17行:.L2为编译器创建的标号,并跳到.L2执行
  • 第22,23行:将i的与常量7比较,成立则跳到.L3执行
  • 第18行:将j的值加1,i的值加1
  • 最后几行释放局部内存区,并跳转回调用程序(ret)

留言

Jun 10 2015