LESSON2:

    The stack... The stack is a very important part of ASM programming.  I will trymy best in illustrating exactly how the stack works.  The stack isbasically a FILO buffer (First In Last Out),this means that the first item PUSHEDinto the stack is the last one to POPEDout. Converly the Last In, is also the First out.
 
Theseare the instructions/ideas that will be covered in this lesson:
PUSH
POP
MORE DETAILSABOUT THE STACK
 
PUSH
  To add an itemto the stack we use the PUSH instruction.  You can only PUSH the followingregisters:  Here are some examples:
push af    ;adds the valuestored in AF to the stack
push bc    ;adds the value storedin BC to the stack
   NOTE: The registerstill contains the value it had before being PUSHED
   NOTE: It is very important that you DON'T exit the program before POPPING, allPUSHED values.  The operating system, I believe, calls what is inthe stack after the last program terminates.  Having random valuesin there will cause a crash, or some unwanted results.
 

back to list of instructions


POP
  To remove anitem from the stack we use the POP instruction.  You can only POPto the following registers:

  Here are some examples:
pop af    ;stores the valueon the top of the
         ;stack in AF  (removes that value fromthe stack)
pop bc    ;stores the value on thetop of the
         ;stack in BC (removes that value from the stack)
back to list of instructions


THE STACK
  So, WHATis the stack?  You can think of it in several ways:

 OK, Get the Idea?If not maybe this will help.  Lets say we start with an empty stack:
 
 
Next we do thefollowing commands:
ld bc,10
push bc
Then this is our new stack:
10
 
ld de,26
push de
Then this is our new stack:
26
10
ld de,15
push de
Then this is our new stack:
15
26
10
pop bc
;now bc contains the value
;that was on the top of the stack
;in this case 15
Then this is our new stack:
26
10
pop de
;now de contains the value
;that was on the top of the stack
;in this case 26
Then this is our new stack:
10
pop hl
;now hl contains the value
;that was on the top of the stack
;int this case 10
Then this is our new stack:
back to list of instructions
LESSON1     INDEX     LESSON3
This is the end of Lesson1, I do not gaurantee any correctnessof any statements made above.