Download presentation
Presentation is loading. Please wait.
Published bySilas Harvey Modified over 8 years ago
1
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions
2
2 Push instruction push REGISTER 1.Decrements (by 4) the stack pointer register (ESP) 2.Copies the value in REGISTER to the top of the stack push EDI bottom of the stack (higher address) ESP = ESP - 4 content of the stack and ESP after push
3
3 Pop instruction pop REGISTER 1. Copies the value at the top of the stack to REGISTER 2. Increments (by 4) the stack pointer register (ESP) pop EDI bottom of the stack (higher address) ESP content of the stack and ESP before pop
4
4 Control flow instructions (subroutine call/return) a procedure that calls another procedure: caller procedure that has been called: callee main() { … countLowCaseChar(“Hello World”) … }
5
5 The caller should Before calling a procedure: pass parameters to the procedure: push to the stack in reverse order (the last parameter is pushed first) execute call (to perform the procedure call) After the procedure returns: pop parameters from the stack (to restore the stack to its state before call was performed) the return value is in EAX
6
6 The callee should At the beginning of procedure: access passed parameters using ESP When the procedure is done: the return value should be placed in EAX execute ret instruction (return to the caller)
7
7 Example void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; }
8
8 Example (cont.) void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } ECXESP EBXESP+4 EAXESP+8 10 (param1)ESP ECXESP+4 EBXESP+8 EAXESP+12 Return AddrESP 10 (param1)ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 ECXESP EBXESP+4 EAXESP+8
9
Example (cont.) 9 Return AddrESP param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; } ECXESP EBXESP+4 Return AddrESP+8 param1ESP+12 ECXESP+16 EBXESP+20 EAXESP+24 Return AddrESP param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16
10
__declspec(naked) __declspec(naked) tells C compiler not to insert prolog/epilog code: 1.function parameters are not copied from stack to the parameter variables, and local variables are not allocated have to retrieve function parameters from stack 2.registers are not saved to stack in the beginning of the function and are not restored at the end have to do it yourself 3.return statement cannot be used you need to explicitly insert ret Assembly instruction at the end of your function you need to return the value by putting it in EAX 10
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.