Re: A86: PUSH and POP


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

Re: A86: PUSH and POP



Butler Family wrote:

> Could someone please explain the use of push and pop to me?  sorry to
> bother ya'll but i am a newbie to ASM
>
> thanx,
>     Patrick

The basic idea of PUSH and POP is simple.  It's a way to store values in
the STACK.  The STACK is basically a FILO (First In Last Ouf) buffer.
here is an example:

  ld bc,1
  push bc    ; adds the value of bc (1) to the top of the stack
  ld bc,2
  push bc   ; adds the value of bc (2) to the top of the stack
  ld de,3
  push de   ; adds the value of de (3) to the top of the stack.


a "picture" of the stack may now lok like this:

3  <- top of the stack
2
1  <- bottom of the stack

any other numbers you add to the stack will be added to the TOP of the
stack..

when you use POP you "recall" the value on the TOP of the stack.  there
fore the command:

  pop bc

would load the value (3) into the register bc, removing it from the
stack...  then:
  pop de
would load the value (2) (now it's on the top of the stack, because 3
was taken off) into the register de... and so on.


WHY   would you do this?!?  loops are one reason:

  ld b,10  ; the number of times to run the loop
LOOP:
  push bc       ; adds the value in bc to the stack

  ld c,b
  ld b,65    ; lets say your PutSprite routine takes the input (b,c) and
HL (the address of the pic)
  ld hl,MYPIC
  call PutSprite

pop bc       ; gets the value on the top of the stack (your previous
value of bc)
  djnz LOOP  ; dec b .... jp nz,LOOP  : basically

another reason is if you function destroys the value of a register...
ex:


   ld b,1
   ld c,3
   push bc          ;save our value
   call ADDBC   ; run our real stupid routine that makes a=b+c
   pop bc           ; get our init values back (ADDBC changed the value
of b)

:
.
.

ADDBC:
   ld a,b   ; this makes no sence but thats ok..
   ld b,c   ; the point is the value of b was overwritten
   add b   ;
   ret

there are LOTS of other reasons too...


hope that helped some

--
Trent Lillehaugen
Computer Engineering Major
California Polytechnic University, San Luis Obispo
<tllilleh@polymail.calpoly.edu>



References: