1. 前言
堆栈生长方向: 高地址在栈底,底地址在栈顶 参数入栈顺序: 从右到左依次入栈 函数调用入栈顺序: 函数调用入栈顺序
2. 测试案例
- 汇编代码
hello.asm
extern compare
section .data
enter_ch db 0x0a
num1 dd 3
num2 dd 3
section .text
global _start
global printstr
_start:
mov edx, [num2]
push edx; second num
mov edx, [num1]
push edx; first num
call compare
mov eax, 1
int 0x80
printstr:
mov eax, 4
mov ebx, 1
mov ecx, [esp+4] ; esp指向调用处的下一条地址
mov edx, [esp+8]
int 0x80
ret
main.c
extern void printstr(char* str, int len);
void compare(int num1, int num2)
{
if (num1 == num2)
printstr("Equal\n", 6);
else
printstr("Not equal\n", 10);
}
makefile
.DEFAULT_GOAL := default
main.o: main.c
gcc -m32 -c main.c -o main.o
hello.o:hello.asm
nasm -felf32 hello.asm -o hello.o
default:
make main.o
make hello.o
ld -m elf_i386 hello.o main.o -o main
|