Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


About assembly , stacks


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Xeee   Members   -  Reputation: 144

Like
Likes
Like

Posted 06 August 2001 - 03:04 PM

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

Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 3595

Like
Likes
Like

Posted 07 August 2001 - 07:47 AM

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

#3 MartinJ   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 August 2001 - 04:35 AM

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.

#4 Kwizatz   GDNet+   -  Reputation: 1308

Like
Likes
Like

Posted 08 August 2001 - 04:55 AM

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

#5 BeerNutts   Crossbones+   -  Reputation: 3595

Like
Likes
Like

Posted 08 August 2001 - 11:12 AM

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

#6 Kwizatz   GDNet+   -  Reputation: 1308

Like
Likes
Like

Posted 08 August 2001 - 05:37 PM

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!.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS