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:PUSHPOPMORE 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:
- a STACK of cards
- PUSH = adds a card tothe top of the stack
- POP = takes a card offthe top of the stack
- a pile of books
- PUSH = place a book onthe top of the pile
- POP = takes a book ofthe 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 thefollowing commands:ld bc,10push bcThen this is our new stack: ld de,26push deThen this is our new stack:ld de,15push deThen this is our new stack:pop bc;now bc contains the value;that was on the top of the stack;in this case 15Then this is our new stack:pop de;now de contains the value;that was on the top of the stack;in this case 26Then this is our new stack:pop hl;now hl contains the value;that was on the top of the stack;int this case 10Then this is our new stack:back to list of instructions
LESSON1 INDEX LESSON3This is the end of Lesson1, I do not gaurantee any correctnessof any statements made above.