Chinaunix首页 | 论坛 | 博客
  • 博客访问: 640815
  • 博文数量: 99
  • 博客积分: 4335
  • 博客等级: 中校
  • 技术积分: 931
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-26 14:11
文章分类

全部博文(99)

文章存档

2012年(14)

2011年(17)

2010年(14)

2009年(54)

分类: LINUX

2009-07-29 11:03:47

这是昨天做的一个实验,用GDB进行调试,可以看到寄存器等信息,从而理解栈在函数调用中起的作用,也再学了学AT&T汇编语言,呵呵。不多说了
 
源程序如下:
 

#include <stdio.h>
void swap(int *x, int *y);
int main(void)
{
    int a=1;
    int b=2;
    swap(&a,&b);
    printf("%d%d\n",a,b);
    return 0;
}
void swap(int *x, int *y)
{
    int t0 = *x;
    int t1 = *y;
    *x = t1;
    *y = t0;
}

 

汇编程序如下:

    .file    "swap.c"
    .section    .rodata
.LC0:
    .string    "%d%d\n"
    .text
.globl main
    .type    main,@function
main:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    andl    $-16, %esp
    movl    $0, %eax
    subl    %eax, %esp
    movl    $1, -4(%ebp)
    movl    $2, -8(%ebp)
    subl    $8, %esp
    leal    -8(%ebp), %eax
    pushl    %eax
    leal    -4(%ebp), %eax
    pushl    %eax
    call    swap
    addl    $16, %esp
    subl    $4, %esp
    pushl    -8(%ebp)
    pushl    -4(%ebp)
    pushl    $.LC0
    call    printf
    addl    $16, %esp
    movl    $0, %eax
    leave
    ret
.Lfe1:
    .size    main,.Lfe1-main
.globl swap
    .type    swap,@function
swap:
    pushl    %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    8(%ebp), %eax
    movl    (%eax), %eax
    movl    %eax, -4(%ebp)
    movl    12(%ebp), %eax
    movl    (%eax), %eax
    movl    %eax, -8(%ebp)
    movl    8(%ebp), %edx
    movl    -8(%ebp), %eax
    movl    %eax, (%edx)
    movl    12(%ebp), %edx
    movl    -4(%ebp), %eax
    movl    %eax, (%edx)
    leave
    ret
.Lfe2:
    .size    swap,.Lfe2-swap
    .ident    "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"

 

分析待续;

阅读(1620) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~