being new to assembly... My head is really spinning trying to understand all the concepts. It is quit different from the php i've been programming the last 8 years. So here is my question...could someone please help me to understand the purpose and uses of a STACK. I know it can be used to push and pop addresses if i recall correctly but not exactly sure of its purpose since i thought assembly was coded using a linear fashion. Even this 60 dollar book i bought doesnt give clear and concise understanding of the stack. Ive also read about the first in last out conventions but none of it really makes sense yet. Could someone please help with my ignorance of the subject? Forgive my punctuation, im currently on mobile.

Too bad you didn't learn C before assembly. In any event, the purpose of the stack is quite simple -- its used to save register values, save return addresses, and to allocate memory for local variables. When the function returns to its caller the stack is reset back to where it was before the function was called. There are two ways to clean up the stack -- (1) the calling function cleans it up after the called function returns, and (2) the called function cleans it just before returning to whereever it was called.

On function entry, one of the first thigs the function does is save the stack pointer in the bp register, then increase the stack pointer to make room for local variables. Just before returning the function changes the stack pointer back to the value in the bp register.

All this would become much clearer if you wrote a small C program had set the c compiler to produce the assembly code. I know vc++ 2010 express will do that, but I don't know about other compilers. I'm almost sure g++ will do it too, but don't know.

I'm new to assembly too. I noticed that stack is almost always used for saving the registers the following routine may corrupt.

As an aside, the textbook Assembly Language Step by Step has an excellent explanation of the hardware stack. The book focuses on programming for Linux, but with a virtualizer such as VirtualPC or VirtualBox, it is easy enough to set up a virtual Linux system running inside a Windows PC.

one of the main uses of the stack is subroutines. branching to a subroutine requires that the return address of the instruction following the branch instruction be stored in a location, where upon the completion of the subroutine, the program will be able to return and continue. a subroutine is a sequence of instructions stored in a seperated memory location from the regular sequence of the main program. during the subroutine... the program may branch to further subroutines... since a stack is first-in-last-out... the return address to the 1st subroutine can be placed on top of the memory-cell that contains the return address to the main program... this process can continue and once the final subroutine finshes.. the program can return in reverse order (by removing the return address from the stack... which is done by incrementing/decrementing the stack pointer


main progam:
0x2 - instruction 1
0x4 - instruction 2
0x6 - instruction 3
0x8 - instruction 4 - branch
0xA - instruction 5

subroutine 1
0x45 - instruction 1
0x47 - instruction 2 - branch
0x49 - instruction 3 return

subroutine 2
0x12c3 - instruction 1
0x12c5 return

so the stack would look like this

head -> 0x49

0x - hexadecimal value -- these are memory addresses where the program instructions are stored