CHAPTER 9 - Stack And Subroutines

EXAMPLE 9.1 - PG NO:283

In [1]:
## page no 283
## example no 9.1
## PUSH POP AND DELAY INSTRUCTIONS
def hex2dec(s):
	 return int(s, 16)

print ('LXI SP,2099H \n \n')# ## the stack pointer is located at 2099H.
print ('LXI H,42F2H ')#
print ('H--> 42     L-->F2 \n \n')#
print ('PUSH H')# ## sends the data of HL register pair in the stack.
## stack pointer is decremented by one to 2098H and the contents of the H register are copied to memory location 2098H
print ('2098H--> 42')# 
## stack pointer is again decremented by one to 2097H and the contents of the L register are copied to memory location 2097H
print ('2097H--> F2 \n \n')#
print ('Delay Counter \n \n')#


n=hex2dec('42F2')#

#for i in '1n'                        ## DELAY LOOP
#    (
#    )
    
 

print ('POP H ')# ## sends the data in the stack back to the HL register pair.
## the contents of the top of the stack are copied to L register and the stack pointer is incremented by one to 2098H
print ('L--> F2H ')#
## the contents of the current location of stack are copied to H register and the stack pointer is again incremented by one to 2099H.
print ('H--> 42H ')#
LXI SP,2099H 
 

LXI H,42F2H 
H--> 42     L-->F2 
 

PUSH H
2098H--> 42
2097H--> F2 
 

Delay Counter 
 

POP H 
L--> F2H 
H--> 42H 

EXAMPLE 9.2 - PG NO:285

In [2]:
## page no 285
## example no 9.2
## EXCHANGE OF DATA USING STACK.
print ('LXI SP,2400H \n \n'); ## the stack pointer is located at 2400H.
print ('LXI H,2150H \n ');
print ('H--> 21     L-->50 \n \n');
print ('LXI B,2280H \n ');
print ('B--> 22     C-->80 \n \n');
print ('PUSH H \n'); ## sends the data of HL register pair in the stack.
## stack pointer is decremented by one to 23FFH and the contents of the H register are copied to memory location 23FFH
print ('23FFH--> 21 \n'); 
## stack pointer is again decremented by one to 23FEH and the contents of the L register are copied to memory location 23FEH
print ('23FEH--> 50 \n \n');
print ('PUSH B \n'); ## sends the data of BC register pair in the stack.
## stack pointer is decremented by one to 23FDH and the contents of the H register are copied to memory location 23FDH
print ('23FDH--> 22 \n'); 
## stack pointer is again decremented by one to 23FCH and the contents of the L register are copied to memory location 23FCH
print ('23FCH--> 80 \n \n');
print ('PUSH PSW \n'); ## sends the data of accumulator & flag register in the stack.
## stack pointer is decremented by one to 23FBH and the contents of the H register are copied to memory location 23FBH
print ('23FBH--> contents of accumulator \n'); 
## stack pointer is again decremented by one to 23FAH and the contents of the L register are copied to memory location 23FAH
print ('23FAH--> contents of flag register \n \n');

print ('To exchange the data. \n \n')
print (' POP PSW \n'); ## sends the data in the stack back to the accumulator & flag register.
## the contents of the top of the stack are copied to A register and the stack pointer is incremented by one to 23FBH
print ('A--> contents of accumulator \n');
## the contents of the current location of stack are copied to flag register and the stack pointer is again incremented by one to 23FCH.
print ('F--> contents of flag register \n \n');
print (' POP H \n'); ## sends the data in the stack back to the HL register pair.
## the contents of the current location of the stack are copied to L register and the stack pointer is incremented by one to 23FDH
print ('L--> 80H \n');
## the contents of the current location of stack are copied to H register and the stack pointer is again incremented by one to 23FEH.
print ('H--> 22H \n \n');
print (' POP B \n'); ## sends the data in the stack back to the BC register pair.
## the contents of the current location of the stack are copied to C register and the stack pointer is incremented by one to 23FFH
print ('C--> 50H \n');
## the contents of the current location of stack are copied to B register and the stack pointer is again incremented by one to 2400H.
print ('B--> 21H \n');
LXI SP,2400H 
 

LXI H,2150H 
 
H--> 21     L-->50 
 

LXI B,2280H 
 
B--> 22     C-->80 
 

PUSH H 

23FFH--> 21 

23FEH--> 50 
 

PUSH B 

23FDH--> 22 

23FCH--> 80 
 

PUSH PSW 

23FBH--> contents of accumulator 

23FAH--> contents of flag register 
 

To exchange the data. 
 

 POP PSW 

A--> contents of accumulator 

F--> contents of flag register 
 

 POP H 

L--> 80H 

H--> 22H 
 

 POP B 

C--> 50H 

B--> 21H 

EXAMPLE 9.3 - PG NO:292

In [3]:
## page no 292
## example no 9.3
## EXCHANGE INFORMATION BETWEEN STACK AND PROGRAM COUNTER
print ('After the CALL instruction \n \n');
print ('STACK MEMORY: \n \n');
print ('23FFH--> 20 \n');
print ('23FEH-->43 \n');
print ('Stack pointer--> 23FEH \n');
print ('Program counter--> 2070H \n \n');
print ('After RET instruction \n \n');
print ('Program counter--> 2043H \n');
print ('Stack pointer--> 2400H');
After the CALL instruction 
 

STACK MEMORY: 
 

23FFH--> 20 

23FEH-->43 

Stack pointer--> 23FEH 

Program counter--> 2070H 
 

After RET instruction 
 

Program counter--> 2043H 

Stack pointer--> 2400H