About assembly , stacks

Started by
4 comments, last by Xeee 22 years, 8 months ago
kindly, would you tell me what is the use of stacks in assembly (it is created using .STACK , right??!!), i know just the two instructions pop , push , so what is its use?? thanks for care ...Xeee
xee..
Advertisement
I''ll try to be general here (since I''ve only done MIPS assembly, and everything here is basically Intel based), but a stack is just a place in memory where local variables and such are stored. There is usually a specific register which is called the stack pointer (henceforth called SP), and it points to the top of the stack. For example, when a function call is made, the local variables in the calling function must be stored so they aren''t overwritten by local variables in the callee function. They are stored by ''pushing'' them on the stack (ie, saved at the pointer where SP points to, then SP is decremented so the next variable can be saved). When the callee function returns to the previous function, each variable is the ''popped'' off the stack (ie, the SP is incremented, and then the variabled saved at where SP is located is taken off the stack).

That is how a stack is used in assmebly, but stacks can be used for anything else, and often are. They are basically a FILO queue (First In Last Out).

Hope that helps (/me is not much of a teacher)
Nutts

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

The stack is also used to store return addresses for procedures. When you use the CALL instruction, the IP address of the next instruction is pushed on the stack. When the RET address is executed, the last number is popped from the stack and put in the IP. You can also hijack the stack for local variable storage, as long as you have enough space allocated. When someone talks about a stack frame, they''re basically saying that there is a region of memory that holds arguments to a function/procedure, the return address, and some extra space set aside for local variables. I believe the BP register is used to give a base for looking into the stack. The ENTER and LEAVE instructions can take care of maintaining the stack frame for you.
The .STACK assembler directive simply sets aside a fixed amount of memory for use by the system stack instructions. Depending on the memory model, the stack can be placed just past your data segment(s), your code segment(s), or some arbitrary location in memory.
Also I would like to say that the stack is used to pass arguments to functions, all the arguments are PUSHed into the stack in inverse order, and the return value gets stored in AX or EAX so a call to function like:

int function(int n1,int n2);

will be translated to ASM as:

PUSH n2
PUSH n1
CALL Function
TEST EAX,EAX ; see if returned Value is NOT Zero after the function returns.

this is important to know if you are makin an ASM module with functions you want to call from C,
inside the ASM function, the SP is a delimiter where, to one side of the Address pointed by SP there are the arguments to the function, and to the other the local variables (sorry dont remember which way is which)
so if you want to access that data you use offsets from the SP like

MOV EBX,[SP + 4]; will get the integer value in the 2 (if I am not mistaken) variable inside EBX
Well, depending on the processor, sometimes the Return address and parameters are stored in registers, and not on the stack
Example: MIPS processors, the return address is stored in the ra register, and parameters (up to 4) are stored in registers a0 to a3 (granted, the ra must be stored on the stack when another function is called).

Just an FYI

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Yup, the MIPS Processor has 32 registers, having registers even for function arguments and variables as well (v0 to v3?), it also executes 2 instructions per cicle (if I am not mistaken) I have fidled with my PSX I am sure you know more than me about this Processor BeerNutts, Greets!.

This topic is closed to new replies.

Advertisement