一段简单c程序的汇编语言学习(ubuntu+x86)

Posted on 2013-07-29 19:32  冰天雪域  阅读(375)  评论(0编辑  收藏  举报

c程序代码:

 

#include <stdio.h>

int main(void)
{
	int i=0, j=0;

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

	return 0;
}


汇编代码如下:

 

 

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

 

Copyright © 2024 冰天雪域
Powered by .NET 8.0 on Kubernetes