C语言函数调用参数压栈顺序

很多人都知道,函数调用中,函数的参数是通过压栈出栈传递的。下面通过代码来验证压栈过程。

代码分两部分,参考了其他作者的代码后修改的。

1. 首先验证操作系统的栈的增长方向;

2. 汇编语言将参数压栈,然后调用C语言的函数。


代码1: 函数的临时变量从栈中分配内存,而static变量只初始化一次。利用这两个基本原理验证堆栈增长方向。

代码如下:

#include <stdio.h>


int find_stack_direction();


int main()
{
int temp;


temp = find_stack_direction();
if(0 == temp)
printf("Ubuntu's stack is increating!\n");
else
printf("Ubuntu's stack is decreasing!\n");


return 1;
}


int find_stack_direction()
{
static int* addr = NULL;
int anything;


if(NULL == addr)
{
addr = &anything;
find_stack_direction();
}
else
{
if(addr > &anything)
return 1;
else
return 0;
}
}

代码2中,汇编程序主动压栈两个参数,调用C语言函数,函数从栈中取参数运算后返回。

汇编代码如下:

extern choose


[section .data]


num1st dd 5
num2nd dd 9


[section .text]


global _start
global myprint


_start:
push dword[num1st]
push dword[num2nd]
call choose
add  esp, 8


mov  ebx, 0
mov eax, 1
int  0x80


myprint:
mov edx, [esp+8]
mov ecx, [esp+4]
mov ebx, 1
mov eax, 4
int 0x80
ret

C语言代码如下:





void myprint(char* msg, int len);




int choose(int a, int b)
{

char temp;
if (a < b)
{
myprint("the 1st one choosed!\n", 21);


}
else
{
myprint("the 2nd one choosed!\n", 21);
}
a += 48;
b += 48;
temp = (char)a;


myprint(&temp, 1);
myprint("\n",2);
temp = (char)b;
myprint(&temp, 1);
myprint("\n",2);

return 0;
}

由于我用的是64为ubuntu,在用NASM和GCC编译的时候出现编译通过,不能链接的错误。Google后找到原因:NASM编译出32位目标文件,GCC在64位机默认编译出64位目标文件,导致两个目标文件无法链接。解决方法是在GCC编译命令里添加 -m32 参数,LD链接命令参数添加 -m elf_i386

代码1的参考作者不记得了,代码2参考于Orange's;链接错误参考于http://www.cnblogs.com/chenchenluo/archive/2012/04/02/2421457.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值