함수 스택 처리
Language/ASSEMBLY 2017. 2. 10. 09:32============================
함수 인자 전달
============================
void f(int a)
{
int b; // 5.{ESP,4}
b = a; // 6.{MOV EAX,[EBP+8]} 7.{MOV [EBP-4],EAX}
// 8.{ADD ESP,4}
// 9.{POP EBP}
// 10.{RETN (POP EIP)}
}
int main(void)
{
f(0); // 1.{PUSH 0}, 2.{CALL f(2-1.PUSH EIP 2-2.JMP f)}, 3.{PUSH EBP 4.MOV EBP,ESP}
// 11.{ADD ESP, 4}
return 0;
}
---------------- <---- EBP-4h
지역변수 int b
---------------- <---- ESP, EBP
EBP
----------------
RTN
---------------- <---- EBP+8h
인자 int a:0
---------------- <---- ESP
MAIN
---------------- <---- EBP
============================
리턴값 전달
============================
int add(int x, int y)
{
return x+y; // 7.{MOV EAX, [EBP+C]}, 8.{ADD EAX, [EBP+10]}, 9.{MOV [EBP+8], EAX}
// 10.{POP EBP}
// 11.{RETN}
}
int main(void)
{
add(1,2); // 1.{PUSH 2}, 2.{PUSH 1}, 3.{SUB ESP,4}, 4.{CALL add}, 5.{PUSH EBP}, 6.{MOV EBP, ESP}
// 12.{MOV EAX, [EXP]}
// 13.{ADD ESP, 0C}
return 0;
}
---------------- <---- ESP, EBP
EBP
----------------
RTN
---------------- <---- EBP+8h
리턴값: 3
---------------- <---- EBP+Ch
인자 int x:1
---------------- <---- EBP+10h
인자 int y:2
---------------- <---- ESP
MAIN
---------------- <---- EBP
관련 자료