I'm trying to understand the example that is given for how a stack frame works. Here is the code of how they appear in the slies. Can someone help me understand?
.data
sum DWORD ?
.code
push 6 ; second argument
push 5 ; first argument
call AddTwo ; EAX = sum
mov sum,eax ; save the sum
AddTwo PROC
push ebp
mov ebp,esp
.
.
Then the next slide goes on to 'recall the AddTwo PROC' (I'm not really sure if this is supposed to be a continuation of the first slide or what...maybe you understand better):
AddTwo PROC,
val1:DWORD, val2:DWORD
mov eax,val1
add eax,val2
ret
AddTwo ENDP
The next slide shows this code as the code that MASM actually generates when AddTwo is assembled:
AddTwo PROC,
val1:DWORD, val2:DWORD
push ebp
mov ebp, esp
mov eax,val1
add eax,val2
leave
ret 8
AddTwo ENDP
Commented out the PROC and ENDP to show that the new code does not need them any more.
Added simple label to complete the translation of PROC.
Added translation of argument addresses, assuming they are illustrating STDCALL calling convention:
;;; AddTwo PROC, val1:DWORD, val2:DWORD
AddTwo:
push ebp
mov ebp, esp
mov eax,[ebp+4] ;;; val1
add eax,[ebp+8] ;;; val2
leave
ret 8
;;; AddTwo ENDP
About the stack frame, please see \masm32\help\asmintro.chm under "The Stack." Great info there.
Also see:
http://www.masm32.com/board/index.php?topic=5160.0
http://www.masm32.com/board/index.php?topic=7597.0
http://www.masm32.com/board/index.php?topic=8743.0
Those were found by typing "help stack frame" into the Search: box at the top of the page.
Quote from: tenkey on February 26, 2009, 10:02:35 PM
;;; AddTwo PROC, val1:DWORD, val2:DWORD
AddTwo:
push ebp
mov ebp, esp
mov eax,[ebp+4] ;;; val1
add eax,[ebp+8] ;;; val2
leave
ret 8
;;; AddTwo ENDP
Hi tenkey,
ebp+4 and ebp+8 are incorrect.
AddTwo should be (we dont need to comment AddTwo proc):
Quote
AddTwo proc
push ebp
mov ebp, esp
mov eax,[ebp+8] ; val1
add eax,[ebp+12] ; val2
leave
ret 8
AddTwo endp
When we «push Val2» then ESP points to Val2
when we «push Val1» then ESP points to Val1
when we «call AddTwo» then ESP points to «ret address» and we go to AddTwo
when we «push ebp» then ESP points to EBP
when we mov ebp, esp then EBP points to EBP
now, in this prog point, the stack frame is
EBP <- [ebp + 0] points to here
ret address <- [ebp + 4] points to here
Val1 <- [ebp + 8] points here to Val1
Val2 <- [ebp + 12] points here to Val2
Rui