I'm preparing for my ASM exam and I'm having trouble understanding the concept of passing parameters via the stack. Usually we must use local variables when writing recursive procedures. Here's a little code snippet that I'm unable to test atm:
procedure1: PUSH EBP MOV EBP, ESP PUSH EAX PUSH EBX MOV EAX, [EBP+8] ;EAX receives the 1st parameter ex EAX = 10 MOV EBX, [EBP+12];EBX receives the 2nd parameter ex EBX = 20 ADD EAX,10 ADD EBX,10 SUB ESP,8 ;Creating space for 2 local variables 4 bytes each MOV [EBP-4], EAX MOV [EBP-8], EBX CALL procedure2 ;POPS, MOV, RET procedure2: PUSH EBP MOV EBP, ESP PUSH EAX PUSH EBX MOV EAX, [EBP+8] ;EAX should receive the 1st local parameter ex EAX = 20 MOV EBX, [EBP+12];EBX should receive the 2nd local parameter ex EBX = 30 ;POP, MOV, RET
What I want to be able to do is create local variables that can be accessed via the stack by another procedure. I'm not sure whether the "local" variables would be placed at EBP+8 and EBP+12. Also whenever I ADD or SUB ESP does it automatically adjust (sorry for this one but I want to be sure).
Also can someone tell me whether pushing the registries first and doing SUB ESP, X is different from doing SUB ESP, X and then saving the REGs?
P.S I'm using NASM
Edited 6 Years Ago by Swiftle: n/a