/*************************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
- 所谓的引用操作就是,把变量的内存地址作为参数传递出去。
- 指针操作符* 取,地址对应的内容。
- 尽管函数知道穿过来的是地址,但是还是要通过寄存器寻址,而不是直接立即数寻址:(