EE 323: Microprocessor systems design
5. Stack and subroutine
5.1 Stack
      5.1.1. Definition
       Stack is a group of memory location in Read/Write memory. It’s used to
store register content and memory address temporarily during the execution of
program. The starting location of the stack is defined by loading 16bit address to
the stack pointer.
      5.1.2. Operating principle of stack
      Using the instruction PUSH, the content of register pair can be stored in
two consecutive stack memory locations. The instruction POP is used to retrieve
the content of register pair from the stack (opposite to storing process).
       In the stack the storing of information begins at SP-1 and SP-2, and then
the stack pointer is decremented by two. The process of information retrieval
from the stack begins at SP and SP+1, where SP is location indicated by stack
pointer whenever a POP instruction is executed.
     The storage and retrieval of data bytes on the stack should follow the
LIFO sequence (LAST IN FIRST OUT).
      Example:
LD SP, 2300H  ; Load to the stack pointer the value 2300H
LD BC, 332AH  ; Load to the register pair BC the value 332AH
 PUSH BC         ; Store the content of register B and the content of register C
                   temporarily in memory location SP-1 and SP-2
                   respectively and then decrement SP by two. (B at 22FFH
                   and C at 22FEH, SP = 23FEH).
LD BC, 4455H ; Load to the register pair BC the data 4455H
LD HL, 7799H ; Load to register pair HL the data 7799H
ADD HL, BC    ; ADD the content of BC to HL (HL=7799+4455)
POP BC         ; Retrieve the values of BC stored in the stack, copies content
                  of SP to C and content of SP+1 to B (Last in first out), and
                  then increment SP by two (Content of memory address
                    23FE to C and content of memory address 23FF to register
                    B, SP=2400H)
      5.1.3. STACK Instructions
      The Z80 instruction set includes six PUSH and six POP instructions
associated with register pair and index register.
PUSH AF             POP AF
PUSH BC              POP BC
PUSH DE              POP DE
PUSH HL              POP HL
PUSH IX              POP IX
PUSH IY              POP IY
Remark: the flags are not affected by pop and push instructions.
      5.1.4. Alternate register as stack
      The Alternate registers can be used to store the contents of general
purpose registers, the accumulator and the flags using the instruction Exchange.
The alternate registers serve a function similar to that of the stack.
EXX : Exchange the contents of registers BC, DE, HL with BC’, DE’ and HL’
EX AF, AF’ : exchange the contents of accumulator and flags with A’ and F’
      The exchange can be down also between index register and register HL
with stack pointer.
EX (SP), rx : Exchange the contents of the index register IX or IY with the
             contents of the two top locations of stack
EX (SP), HL : Exchange the contents of register HL with the contents of two
             top locations of stack
EX DE, HL    : Exchange the content of DE register with the content of HL
             register.
Remark: the exchange instructions do not affect flag
5.2. Subroutine
      5.2.1. Definition
      A subroutine is group of instructions that performs a subtask required
repeatedly in a main program. The subroutine is written separately from the
main program, and is called by the main program when using need call
instruction.
      5.2.2. Operating principle of subroutine
       When a subroutine is called from the main program using call instruction,
the content of program counter PC (the address of instruction following the call
instruction) is stored in the stack, and the program execution is transferred to the
subroutine address. When the return instruction is executed at the end of the
subroutine, the memory address stored in the stack is retrieved.
     Example:
Main program begins at the address 2000H
      PC           Hexa code                     Assembly code
      2000H       31                  LD SP, 2300H
                  00
                  20
      2025H       CD                  CALL 2100H
                  00
                  21                                             Stack Location
      2028H       ….
                                                                   22FE      28
                                                                   22FF      20
      2045H       76                  HALT                         2300      XX
Subroutine begins at the address 2100H
      2100 H     3E                  LD A, 55H
                 55
      2122H      C9                   RET
      The red arrows show the follow of program execution.
      5.2.3. Subroutine instructions
      The Call and return instructions were given in chapter 3 (Z80 instruction
set and programming techniques).
      5.2.4. Subroutine concepts
       When the register is loaded in main program but the information of this
register is used in subroutine, this information is called parameter passing.
       A subroutine called by another subroutine is called nested subroutine.
When subroutine calls another subroutine, all return addresses are stored on the
stack.
       In case the registers contents is to be changed in the subroutine, they must
be first pushed into the stack at the beginning of the subroutine, then poped back
again before returning to the main program to avoid data loss.
       When a subroutine has two or more than two condition returns, it’s called
multiple ending subroutine.
                                                               Done by D.BELAIDI
                                                                      February, 2021
Reference:
Book: Z80 Microprocessor , Architecture , interfacing , Programming, and design , third
edition, by Ramesh .S.Gaonkar. Pages [253- 280]