Stacks

The location of the stack is in the programmers hand where he wants to keep it in the memory. We control it by setting SP Pointer address. Generally we keep it in the highest location like FFFF because the data goes into the stack in appending order like the first data will be in SP, next will comes it goes to SP - 1 and it goes on. Thus we store in higher location to make sure the appending of data will not overwrite the existing data

Uses of Stacks

  1. To Store Data(PUSH, POP), can read the instructions here
  2. To Store Return Address, When we have multiple sub Routines we need to return address in Last sub routine in the first order, there we use stack
  3. To Read/Write Flag Register, For example if we want to store the value of BC Pair into AF then we use stacks as we can store B value in A using MOV B, A. But we can’t do the same with MOV C, F so we use stack for this
PUSH B      ; Push BC Pair into stack
POP PSW     ; pop and send them into PSW register (AF Pair)

Subroutines

Passing Parameters

  1. Using Registers - Here the parameter is store in an register and it is passed to the Subroutine. This is not scalable as there are only 7 Registers, so we can pass only one or two parameters as we need some registers free for running subroutine also. So this method is used only when we are need 1 to 2 Parameters
MVI B, 25H              ; Store Value of 25H in B(parameter) register
CALL SubRoutine
. . .
. . .
SubRoutine: MOV A, B    ; Move the Value in B(parameter) register into A register
			. . .
			. . .
			RET
HLT
  1. Using Memory - Here the parameter is stored inside the memory address. it is highly scalable. It is good but when we are using this approach the location 2000H cannot be used anywhere in the program
MVI A, 25H              ; Store Value of 25H in A register
STA 2000H               ; Store the value of A register in 2000H(Parameter) Memory location 
CALL SubRoutine
. . .
. . .
SubRoutine: LDA 2000H   ; Use the data in 2000H as parameter
			. . .
			. . .
			RET
HLT
  1. Using Memory pointer - Here the parameter is stored in the M register which holds the memory address of the parameter
LXI H, 4000H            ; Storing value of 4000H in H Register
MVI M, 25H              ; Sotring Data of 25H into M register
CALL SubRoutine
. . .
. . .
SubRoutine: MOV A, M    ; Using memory location M as an Parameter
			. . .
			. . .
			RET
HLT
  1. Using Stacks - Here the parameter is passed by pushing into the stack before calling the subroutine.
    • The subroutine POPs the passed parameter from the stack. And when the microprocessor calls the subroutine it pushes the return address into the stack.
    • This address appears above the passed parameter in the stack
    • Due to LIFO property of the stack we cannot access the passes parameter unless we pop the return address. Hence the inside the subroutine firstly, the return address is popped into the HL pair
    • Then the passes parameter is popped into the BC Pair. Now in this case the RET instruction cannot be used to come back into the main program as the top of the stack no longer contains the return address (as we had take it out of HL Pair)
    • Thus the return address is obtained from the HL Pair using PCHL instruction which causes the control to return to the main program
LXI D, 1200         ; Parameter 1200 Pushed
PUSH D              ; Into the stack
CALL SubRoutine
. . .
. . .
SubRoutine: POP H   ; Return address taken in HL Pair
		    POP B   ; Parameter is accepted into the BC Pair
			. . .
			PCHL    ; PC Gets the return address from HL Pair
			. . .
			RET
HLT