025 函数的引用调用

/*************************025 函数的引用调用*********************
 * C语言精彩编程百例 25*/
#include<stdio.h>
void swap(int *x,int *y);
void main()
{
    int i,j;
    i=12;
    j=36;

    printf("i and j before swapping : %d %d\n",i,j);
    sawp(&i,&j);
    printf("i and j after swapping : %d %d\n",i,j);
}

void swap(int *x,int *y)
{
    int temp;

    temp = *x;
    *x=*y;
    *y=temp;
}

对应的汇编

    .file   "025.c"
    .def    ___main;    .scl    2;  .type   32; .endef
    .text
    .align 32
LC0:
    .ascii "i and j before swapping : %d %d\12\0"
    .align 32
LC1:
    .ascii "i and j after swapping : %d %d\12\0"
    .align 2
.globl _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    andl    $-16, %esp
    movl    $0, %eax
    movl    %eax, -12(%ebp)
    movl    -12(%ebp), %eax
    call    __alloca
    call    ___main
    movl    $12, -4(%ebp)  # i=12
    movl    $36, -8(%ebp)  # j=16
    subl    $4, %esp   # 准备printf
    pushl   -8(%ebp)    # j
    pushl   -4(%ebp)    # i
    pushl   $LC0       # "i and j before swapping : %d %d\n"
    call    _printf     # 调用 printf
    addl    $16, %esp  # 平衡堆栈
    subl    $8, %esp   # 准备swap
    leal    -8(%ebp), %eax  # eax=&j
    pushl   %eax        # &j入栈
    leal    -4(%ebp), %eax  # eax=&i
    pushl   %eax        # &i 入栈
    call    _sawp       # 调用swap
    addl    $16, %esp  # 准备swap
    subl    $4, %esp
    pushl   -8(%ebp)    # j
    pushl   -4(%ebp)    # i
    pushl   $LC1       # "i and j before swapping : %d %d\n"
    call    _printf
    addl    $16, %esp
    leave
    ret

    .align 2
.globl _swap
    .def    _swap;  .scl    2;  .type   32; .endef
_swap:
    pushl   %ebp        # 保存ebp
    movl    %esp, %ebp  # 调整栈基址
    subl    $4, %esp   # 调整栈空间
    movl    8(%ebp), %eax   # eax=x
    movl    (%eax), %eax    # eax=*x
    movl    %eax, -4(%ebp)  # temp=*x
    movl    8(%ebp), %edx   # edx=x
    movl    12(%ebp), %eax  # eax=y
    movl    (%eax), %eax    # eax=*y
    movl    %eax, (%edx)    # *x=*y
    movl    12(%ebp), %edx  # edx=y
    movl    -4(%ebp), %eax  # eax=temp
    movl    %eax, (%edx)    # *y=temp
    leave
    ret
    .def    _sawp;  .scl    2;  .type   32; .endef
    .def    _printf;    .scl    2;  .type   32; .endef
  1. 所谓的引用操作就是,把变量的内存地址作为参数传递出去。
  2. 指针操作符* 取,地址对应的内容。
  3. 尽管函数知道穿过来的是地址,但是还是要通过寄存器寻址,而不是直接立即数寻址:(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值