flat assembler
Message board for the users of flat assembler.

Index > Main > Stack Frames

Author
Thread Post new topic Reply to topic
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007
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! Sad Thanks and please elaborate.
Post 12 May 2005, 14:52
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22
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 )
-returnaddress
-saved ebp [the stack ptr] ebp = ESP = stack frame ptr
-local long variable aNumber [EBP - 4] [stack ptr ESP after sub esp,4]
Post 13 May 2005, 00:17
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007
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! Smile
Post 13 May 2005, 13:01
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.