News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Stacks

Started by Bipolargod, July 08, 2007, 10:10:57 PM

Previous topic - Next topic

Bipolargod

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. 

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Bipolargod

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?

ninjarider

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.

tenkey

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.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

modchip

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

dsouza123

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

farklesnots

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.

Rainstorm

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.

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

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.

TNick

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