flat assembler
Message board for the users of flat assembler.

 Index > Main > Stack Frames
Author
shaolin007

Joined: 03 Sep 2004
Posts: 65
shaolin007 12 May 2005, 14:52
Could someone please explain to me in layman terms what is involved in a stack frame so I can look at a high language code in debug and see whats going on? For some reason I can't quite get a grasp on it from the books I have read. It's like I have hit a mental block! Thanks and please elaborate.
12 May 2005, 14:52
r22

Joined: 27 Dec 2004
Posts: 805
r22 13 May 2005, 00:17
int main(long parameter1){
long aNumber;
aNumber = parameter1;
return aNumber
}
---
Code:
```main:
push ebp  ;save ebp
mov ebp, esp ;mov ptr to stack into ebp, EBP = ESP
sub esp, 4 ;subtract 4 from stack ptr reserves 4 bytes (1 long local variable aNumber)
mov ecx, [EBP+8]  ; move parameter1's value into eax
mov [EBP - 4], ecx ; move parameter's value into aNumber
mov EAX, ECX ; set the return value
add esp, 4 ; unreserve the 4 bytes for the local variable aNumber
mov esp, ebp ;set stack back equal to its original value
pop ebp ;take the saved EBP value off the stack
retn 4 ;
; the return VALUE (aNumber) is always stored in EAX, this return statement is to fix the stack because 1 4byte parameter was passed with the function
```

the stack
-parameter1 [EBP+8] ( retn 4 puts stack ptr back here )
-saved ebp [the stack ptr] ebp = ESP = stack frame ptr
-local long variable aNumber [EBP - 4] [stack ptr ESP after sub esp,4]
13 May 2005, 00:17
shaolin007

Joined: 03 Sep 2004
Posts: 65
shaolin007 13 May 2005, 13:01
Thank you for your help. I now understand how it works. Funny how I had problems understanding this through a book but when you explain it, it becomes crystal clear. Thanks again!
13 May 2005, 13:01
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum