Page 174 - DCAP210_INTRODUCTION__TO_MICROPROCESSORS
P. 174
Introduction to Microprocessors
Notes There are also a number of small microprocessors that implements a stack directly in hardware
and some microcontrollers have a fixed-depth stack that is not directly accessible. Examples are
the PIC microcontrollers, the Computer Cowboys MuP21, the Harris RTX line, and the Novix
NC4016. Many stack-based microprocessors were used to implement the programming language
Forth at the microcode level. Stacks were also used as a basis of a number of mainframes and
mini computers. Such machines were called stack machines, the most famous being the Burroughs
B5000.
Search which stack was used in 8085 microprocessor.
12.2 Saving Information on the Stack
Information is saved on the stack by PUSHing it on. It is retrieved from the stack by POPing it off.
The 8085 provides two instructions: PUSH and POP for storing information on the stack and
retrieving it back.
Processor families like the x86, Z80, 6502, and many others have special instructions
that implicitly use a dedicated (hardware) stack pointer to conserve opcode space.
12.3 PUSH & POP
As you may have guessed, push and pop "pushes" bytes on the stack and then takes them off.
When you push something, the stack counter will decrease with 2 (the stack "grows" down, from
higher addresses to lower) and then the register pair is loaded onto the stack. When you pop, the
register pair is first lifted of the stack, and then SP increases by 2.
N.B: Push and Pop only operate on words (2 bytes i.e.: 16 bits).
You can push (and pop) all register pairs: BC, DE, HL and PSW (Register A and Flags). When you
pop PSW, remember that all flags may be changed. You can't push an immediate value. If you
want, you'll have to load a register pair with the value and then push it. Perhaps it's worth noting
that when you push something, the contents of the registers will still be the same; they won't be
erased or something. Also, if you push DE, you can pop it back as HL (you don't have to pop it
back to the same register where you got it from).
The stack is also updated when you CALL and RETURN from subroutines. The PC (program
counter which points at the next instruction to be executed) is pushed to the stack and the calling
address is loaded into PC. When returning, the PC is loaded with the word popped from the top
of the stack (TOS).So, when is this useful? It's almost always used when you call subroutines.
For example, you have an often used value stored in HL. You have to call a subroutine that you
know will destroy HL. Instead of first saving HL in a memory location and then loading it back
after the subroutine, you can push HL before calling and directly after the calling pop it back. Of
course, it's often better to use the pushes and pops inside the subroutine. All registers you know
will be changed are often pushed in the beginning of a subroutine and then popped at the end, in
reverse order! Don't forget - last in first out. If you want to only push one 8 bit register, you still
have to push its "friend". Therefore, be aware that if you want to store away D with pushing and
popping, remember that E will also be changed back to what it was before. In those cases, if you
don't want that to happen, you should try first to change register (try to store the information in
E in another register if you can) or else you have to store it in a temporary variable.
Before executing a program, you should keep track of your pushes and pops, since they are
responsible for 99% of all computer crashes! For example, if you push HL and then forget to pop
168 LOVELY PROFESSIONAL UNIVERSITY