LESSON
2:
The stack...
The stack is a very important part of ASM programming. I will try
my best in illustrating exactly how the stack works. The stack is
basically a FILO buffer (First In Last Out),
this means that the first item PUSHED
into the stack is the last one to POPED
out. Converly the Last In, is also the First out.
These
are the instructions/ideas that will be covered in this lesson:
PUSH
POP
MORE DETAILS
ABOUT THE STACK
PUSH
To add an item
to the stack we use the PUSH instruction. You can only PUSH the following
registers:
Here are some examples:
push af ;adds the value
stored in AF to the stack
push bc ;adds the value stored
in BC to the stack
NOTE: The register
still contains the value it had before being PUSHED
NOTE:
It is very important that you DON'T exit the program before POPPING, all
PUSHED values. The operating system, I believe, calls what is in
the stack after the last program terminates. Having random values
in there will cause a crash, or some unwanted results.
back to list of instructions
POP
To remove an
item from the stack we use the POP instruction. You can only POP
to the following registers:
Here are some examples:
pop af ;stores the value
on the top of the
;stack in AF (removes that value from
the stack)
pop bc ;stores the value on the
top of the
;stack in BC (removes that value from the stack)
back to list of instructions
THE STACK
So, WHAT
is the stack? You can think of it in several ways:
-
a STACK of cards
-
PUSH = adds a card to
the top of the stack
-
POP = takes a card off
the top of the stack
-
a pile of books
-
PUSH = place a book on
the top of the pile
-
POP = takes a book of
the top of the pile
-
.... and so on
OK, Get the Idea?
If not maybe this will help. Lets say we start with an empty stack:
Next we do the
following commands:
ld bc,10
push bc
Then this is our new stack:
ld de,26
push de
Then this is our new stack:
ld de,15
push de
Then this is our new stack:
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:
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:
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 correctness
of any statements made above.
© 1997 mindless productions