Re: LZ: Push And Pop


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

Re: LZ: Push And Pop



On  3 Aug 96 at 15:19, Patti Balsan wrote:


> I am an aspiring ZShell programer and I was wondering if anybody could
> tell me what "push" and "pop" do during a program and how I could use them
> in one of mine.
> 
> Thanx
> 




Push and pop allow you to store the value of registers on the stack.
Think of the stack as a big pile of numbers.  Push stuffs another 
number on top, and pop takes the current number off the top.


For example if you had this code:
  ld hl,$0001         >Put 1 in hl
  push hl              > Puts a 1 on the top of the stack
  ld hl,2                > puts a 2 in hl
  pop hl                > restores hl to 1 by getting it from the stack


In this case you can use the stack to temporarily store registers while 
you need them for something else.  Also you can use it to transfer 
16 bit values that the ld command wouldn't normally handle.


  ld hl,$0001          >put 1 in hl
  push hl               > puts a 1 on top of the stack
  pop bc                > bc now has a 1 in it!


I just about forgot, but you can also put more than one number on the 
stack.  In fact you can put quite a few numbers, but the TI-85 will 
eventually run out of space.  This is especially useful at the 
beginning of routines in order to save the contents of the registers.  
Ex:


  ;Beginning of routine
  push af
  push bc
  push de
  push hl
  
  .....
  Do whatever here.
  .....
  pop hl
  pop de
  pop bc
  pop af


You just have to remember to pop everything off in backwards order.  
Finally you can use the stack to pass parameters to routines.  This is 
somewhat complicated and is the way higher level languages like C/C++ 
use to pass values.


The idea is if you have a function like int Add(int a,int b), you want 
to put two integer values on the stack, and then the routine can put 
those values in any register it wants.  Ex:


  ld bc,$0010        > Parameter a
  push bc
  ld de,$0020        > Parameter b
  push de
  CALL_(Add)


  pop de               > de now contains $10+$20==$30!!


  Rest Of Program


; Add Routine
Add:
  pop hl                > In this example puts a $20 in hl
  pop de               > Puts $10 in de
  add hl,de
  push hl              > The stack can also be used to return values
  ret




If you have any further questions on the subject post them to this list 
or email me at:


Josh Pieper
ppieper@nemonet.com  


  


Follow-Ups: References: