A86: Re: More ASM Questions


[Prev][Next][Index][Thread]

A86: Re: More ASM Questions




The "stack" that computers use is a LIFO queue--last in, first out.  This
means that the last value "pushed" will be the value "popped".  You can
think of it like a stack of plates in a cafeteria.  If you put a red plate
on the stack, the put blue plate on, then take off a plate, you will get the
blue plate back.  If you take off another plate, you will get the red plate
back.  When ever you "pop" something, it is removed from the stack.

The calc's stack only works with values, not registers.  However, you can
only push registers, not values.  When you push a register, the value of the
register is placed on the stack.  When you pop a value off the stack into a
register, the value is moved from the top of the stack into the register.
The register it was pushed from doesn't matter in the slightest, because
only the value is stored on the stack.  For example, to move the value in HL
into IX:

push hl       ; the value in HL is "pushed" onto the stack
pop ix        ; the value on the stack is "popped" off into IX

Note that HL still contains the original variable, but IX has the same thing
HL has.  When pushing, the value is only copied, it is not changed.  Now,
the order of pushing and popping can be important if you are using the stack
to save your registers (a common use of the stack).  Since it is LIFO, you
must pop in the reverse order that you pushed, or else the values will be
all messed up:

push af   ; AF is now on the top of the stack      (...AF)
push bc   ; BC is now on the top, AF is below it   (...AF, BC)
push de   ; DE is on top of BC, which is on AF     (...AF, BC, DE)

...code here...

pop de    ; since DE is on top of the stack, it is popped first  (...AF, BC)
pop bc    ; BC is now on top, so it is popped next               (...AF)
pop af    ; the top is now AF, so pop it last                    (...)

The dots represent the other data on the stack.  The stack area "below" what
you push will contain other data, such as where your program will RETurn
when it is done.

A quick warning about the stack is that you must ALWAYS pop for every time
that you push.  CALL/RET use the stack.  Think of CALL as a push and RET as
a pop, so you can't push stuff, call a routine, then pop stuff in that
routine, or it will mess up.  If you push stuff and don't pop it, then try
to RETurn from the program, RET will pop the wrong data from the stack (your
data, not the return address) and will jump to an invalid locatin, thus
crashing the calc!

So, if your program crashes for no reason, count your pushes and pops!

--
David Phillips <david@acz.org>
http://www.acz.org/

----- Original Message -----
From: Chris Flanigan <cflan@granitecity.com>
To: 86 Assembly <assembly-86@lists.ticalc.org>
Sent: Saturday, January 23, 1999 4:07 PM
Subject: A86: More ASM Questions


>
>Sorry for all of the questions. I just want to learn this langauge.
>
>I have a question about the logic of using the stack.
>
>If I push AF on the stack my stack would "look" like this
>
>AF
>
>If I push BC on the stack, my stack would now "look" like this
>
>BC
>AF
>
>Is this correct?
>
>If I popped BC would the stack look like this
>
>AF
>BC
>
>I don't really understand this. Again, I know its simple but...
>
>I will learn this language, oh yes. It may take me time but I'll one day
>get it.
>
>Thanks for everyones help and cooperation.
>
>This message was written by Chris Flanigan.
>
>If this is in reply to an inquiry about the TI Archive, direct
>all e-mail to ti_archive@hotmail.com.
>
>If this is a personal message, direct all e-mail to cflan@granitecity.com
>
>
>