Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Stack and $sp, push/pop ?

Status
Not open for further replies.

Ciralia

Programmer
Oct 22, 2002
51
0
0
US
In assembly I find it very difficult to do anything, as I am used to programming in C, and one of the problems is figuring out how to use the stack to store values.

I know there is a separate register $sp used to put values on the stack. For example:

sub $sp, $sp, 2 #make room for 1 halfword on stack

After this point however I do not know how to add values to the stack such as:

.half 0x010A

And how do I print values that are on the stack? In my text books I have found nothing that prints out this hex value from the stack. Any help would be appreciated =).
 
You need a good assembler text-book. The stack and its use is quite a big subject. The stack is used in at least three different ways:
(1) by the processor as a place to put return-addresses every time you call a proc of any sort. This is largely transparent; you don't have to notice what's going on. But it is the reason why if you push something and forget to pop it, and then do a ret, horrible things happen!
(2) just to store numbers you know you need a few lines further down:
push bx ; need this number
push ax ; need this number too
mov ax, something_i_need_here
add ax, something_else
mov bx, another_thing
sub ax, bx
mov somewhere_ax
pop ax ; number back again
pop bx ; first in, last out...
(OK, not very good code, but you get the point)
(3) the stack is also used for local variables, by setting up a "stack frame". This is what the sub sp, something is all about. In a good book on assembly there will be chapters on interfacing with C and other high level languages. They usually explain what a stack frame looks like and how to use it, better than I can, but I'll try if you've got any specifics.
Good luck.
 
See the text book I have has two pages on this whole topic, and I know this is what I need. So, the ax, and bx are registers? Also, if I have 5 items on the stack and I want to specifically remove item 3, do I use the frame pointer to get to the right spot?
 
There are two ways to get items from the stack.
(1) use pop. This removes only the most recently added item (that's why it's a stack. It's just like a stack of paper on a desk).
(2) reference the stack just like any other bit of memory. The stack, specifically, is the memory in the stack segment, so you use ss. I've never tried manipulating the stack pointer, sp, directly for referencing stack memory, except for (sub sp, something) to make space for local variables. Normal procedure is to move the stack pointer into bp, which automatically works with the stack segment.
You can then retrieve item 5 by mov ax, ss:[bp+/-offset]. The offset depends on how you set up the stack frame, and whether the call was far/near and things like that.
 
notice to Ciralia
very important is the SIZE of the stuff you put on the stack. in 32 bit intel CPUs you can put WORDs and DWORDs.
probably it can be helpful if you consider DWORDs on the stack as an array of int addressed by the ESP register. btw, r u in 16 or 32-bit world?
concerning ur question on item 3 on stack, u can not "remove" it - this is just an address in ur address space. u can do anything with its content - that's what assembly is about - including ignoring it. is it equivalent of "remove"?

success, oleksii
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top