The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Bipolargod on July 08, 2007, 10:10:57 PM

Title: Stacks
Post by: Bipolargod on July 08, 2007, 10:10:57 PM
Can someone tell what a stack is and its purpose.  Anywhere I go they dont tell me what it does when you use and what purpose it serves.  Can some wone please tell me and also if they can show me an example.  If I get this I think I will have not trouble at all learning X86. 
Title: Re: Stacks
Post by: hutch-- on July 09, 2007, 02:35:24 AM
Bipolargod,

Welcome on board. The stack is a section of memory reserved within an executable program which is used for temporary storage of data including temporary storage fore passing arguments to procedures.

The isntruction that work dorectly on the stack are PUSH, POP, CALL RET (xxx) and you can also alter the stack memory by using the address in the register EBP.
Title: Re: Stacks
Post by: Bipolargod on July 09, 2007, 02:45:55 AM
Thank you for the welcome.

Ok so I get the temporary storage.  But can you explain why people use it and how it effects programs?
Title: Re: Stacks
Post by: ninjarider on July 09, 2007, 03:51:49 AM
the stack is a way of passing variables from program to program. it is also used by the computer itself to move around in the code.

if you wanted to have windows perform a function for you. you would first "push" all the required variables on the stack and then call the function.
Title: Re: Stacks
Post by: tenkey on July 09, 2007, 05:51:31 AM
The stack is a storage area that is managed in a last-in-first-out manner.

Adding data to the stack is known as "pushing", removing the most recently added data from the stack is known as "popping".

Think of a stack of paper where you put paper into a hopper (tray) on top any existing paper, and remove them from the top.
Title: Re: Stacks
Post by: modchip on July 09, 2007, 07:35:43 AM
I'm a noob at this but here's what I know... You use the stack to temporarily store some data that you'll need later.

Once I encountered a problem. I was looping some code, I used the ecx register as my counter, then in the middle, I call some functions. The funny thing is, the loop count changes, primarily because something in the middle code changes its value. So one solution I thought of was PUSHing the counter value to the stack before executing my code, then after that, I restore/POP the value back from the stack. Then it worked! :D
Title: Re: Stacks
Post by: dsouza123 on July 09, 2007, 09:45:13 PM
Local variables are held on the stack.

From loadfile.asm


LoadFile proc lpName:DWORD,lpSaved:DWORD

    LOCAL hFile     :DWORD
    LOCAL fl        :DWORD
    LOCAL bRead     :DWORD
    LOCAL hMem$     :DWORD      ; source memory handle
    LOCAL hBuffer$  :DWORD      ; result memory handle
Title: Re: Stacks
Post by: farklesnots on July 13, 2007, 12:21:36 AM
Also, the stack holds the return address for all procedures CALLs. The return address is the the address of the first instruction after the CALL. At the exit point of the procedure a RET instruction causes the processor to pop the address off the stack into the instruction pointer (EIP) and continue execution from that point.
Title: Re: Stacks
Post by: Rainstorm on July 16, 2007, 11:11:47 AM
modchip,
  am learning too   ..what you say maybe because ecx may need to be preserved accross the function call that you are making. - so everytime you call the function it destroys your original value in ecx.Anyhow you figured out the solution on your own (the push/pop you do preserves it). there is some info on this in the help files in masm32.
Title: Re: Stacks
Post by: hutch-- on July 16, 2007, 11:33:16 AM
Rainstorm,

the file is Asm Intro help but you seem to have got something wrong about register preservation. Within a procedure ECX can be changed without having to restore it before the end of the proc where EBX ESI EDI must be the same value on exit from a proc as it was on entry.
Title: Re: Stacks
Post by: Rainstorm on July 16, 2007, 02:50:42 PM
hutch,
    yes.. ecx doesn't have to be preserved within a procedure..but wouldn't he have to preserve it accross a call ? - Since he said he was doing a function call in his code & the value of ecx was  getting affected accross the call. - on the other hand in my usual code i can usually use ebx esi edi freely accross those functions.. because they are preserved in the procedures of those functions.. so I don''t have to preserve them accross the function calls.. if I need to use them. am i right ?

Rainstorm.
Title: Re: Stacks
Post by: TNick on July 17, 2007, 10:33:50 AM
Stack. True is that I had a hard time understanding it at first. I will add my comment to already posted infos...

The Stack is just a memory area from, let's say 0B00 0000h to 0B01 0000h. The esp register is used to indicate your "current position" in this area. When your first line of code is executed, esp may hold a value like 0B00 0010h. A PUSH instruction will do two things at once: it will put the argument you provide at address pointed by esp (0B00 FF10h) add will substract 4 from esp value, which will be now 0B00 FF0Ch. POP instruction will add 4 to esp and will copy the value pointed by esp in argument. (all this assuming that we are working with 32 bit values).

Nick