Stack, Subroutine & Related Instructions

Stack, Subroutine & Related Instructions

The Stack
  • The stack is an area of memory identified by the programmer for temporary storage of information.
  • The stack is a LIFO (Last In First Out) structure. 
  • The stack normally grows backwards into memory. 
– In other words, the programmer defines the bottom of the stack and the stack grows up into reducing address range. 
Given that the stack grows backwards into memory, it is customary to place the bottom of the stack at the end of memory to keep it as far away from user programs as possible. 
  • In the 8085, the stack is defined by setting the SP (Stack Pointer) register. 
  • LXI SP, FFFFH 
  • This sets the Stack Pointer to location FFFFH (end of memory for the 8085). 
  • The Size of the stack is limited only by the available memory  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. 
– Both PUSH and POP work with register pairs ONLY. 

Stack is a set of memory locations in the Read/Write memory which is used for temporary storage of binary information during the execution of a program. It is implemented in the Lastin- first-out (LIFO) manner. i.e., the data written first can be accessed last, One can put the data on the top of the stack by a special operation known as PUSH. Data can be read or taken out from the top of the stack by another special instruction known as POP.


Stack is implemented in two ways. In the first case, a set of registers is arranged in a shift register organization. One can PUSH or POP data from the top register. The whole block of data moves up or down as a result of push and pop operations respectively. In the second case, a block of RAM area is allocated to the stack. A special purpose register known as stack pointer (SP) points to the top of the stack. Whenever the stack is empty, it points to the bottom address. If a PUSH operation is performed, the data are stored at the location pointed to by SP and it is decremented by one. Similarly if the POP operation is performed, the data are taken out of the location pointed at by SP and SP is incremented by one. In this case the data do not move but SP is incremented or decremented as a result of push or pop operations respectively.

• Program:-


2000 LXI SP,2099H            Load the stack pointer register with the address 2099.
2003 LXI B ,42F2H             Loads data in the BC register pair.
2006 PUSH B                       The content of the BC register pair pushed into stack.

2010 POP D                         Saved data in stack pointer register to DE register pair.

PUSH B (1 Byte Instruction) 

– Decrement SP 

  • The stack pointer is decremented by one to 2098 H.
– Copy the contents of register B to the memory location pointed to by SP 

  •  The contents of the B register are copied to  memory location 2098H.
– Decrement SP 

  • The stack pointer register is again decremented by one to 2097H.
– Copy the contents of register C to the memory location pointed to by SP 

  •  The contents of the C register are copied to memory location 2097H.
  • The contents of the register pair BC are not destroyed 





• POP D (1 Byte Instruction) 

– Copy the contents of the memory location pointed to by the SP to register E 

  • The contents of the top of the stack location shown by the stack pointer are copied in the E register.
– Increment SP 

  • The  stack pointer register is incremented by one to  2098 H.
– Copy the contents of the memory location pointed to by the SP to register D 

  • The contents of the top of the stack (now it is 2098H) are copied in the D register
– Increment SP 

  • The contents of memory location 2097H and 2098 are not destroyed until some other data bytes are stored in these location.                                                        


Operation of the Stack 

• During pushing, the stack operates in a “decrement then store” style. 
– The stack pointer is decremented first, then the information is placed on the stack. 
• During poping, the stack operates in a “use then increment” style. 
– The information is retrieved from the top of the the stack and then the pointer is incremented. 
• The SP pointer always points to “the top of the stack”. 
LIFO 
• The order of PUSHs and POPs must be opposite of each other in order to retrieve information back into its original location. PUSH B PUSH D ... POP D POP B 


 The PSW Register Pair 

• The 8085 recognizes one additional register pair called the PSW (Program Status Word). This register pair is made up of the Accumulator and the Flags registers. • It is possible to push the PSW onto the stack, do whatever operations are needed, then POP it off of the stack.


• Program:-

LXI SP,FFFF 
PUSH PSW 
POP PSW

PUSH PSW Register Pair 

• PUSH PSW (1 Byte Instruction) 

– Decrement SP
 – Copy the contents of register A to the memory location pointed to by SP 
– Decrement SP 
– Copy the contents of Flag register to the memory location pointed to by SP

 - Microprocessor Pop PSW Register Pair 



POP PSW (1 Byte Instruction) 

– Copy the contents of the memory location pointed to by the SP to Flag register 
– Increment SP 
– Copy the contents of the memory location pointed to by the SP to register A 
– Increment SP
- Microprocessor Modify Flag Content using PUSH/POP 






 Subroutines 

• A subroutine is a group of instructions that will be used repeatedly in different locations of the program. Rather than repeat the same instructions several times, they can be grouped into a subroutine that is called from the different locations. 
• In Assembly language, a subroutine can exist anywhere in the code. However, it is customary to place subroutines separately from the main program. 
 • The 8085 has two instructions for dealing with subroutines. The CALL instruction is used to redirect program execution to the subroutine. The RET insutruction is used to return the execution to the calling routine. 


- Microprocessor The CALL Instruction 

• CALL 4000H (3 byte instruction) 

– When CALL instruction is fetched, the MP knows that the next two Memory location contains 16 bit subroutine address in the memory. CALL 4000 40 00 [W] [Z]Register
– MP Reads the subroutine address from the next two memory location and stores the higher order 8 bit of the address in the W register and stores the lower order 8 bit of the address in the Z register 
– Pushe the address of the instruction immediately following the CALL onto the stack [Return address] 
– Loads the program counter with the 16-bit address supplied with the CALL instruction from WZ register. 

The RET Instruction 

• RET (1 byte instruction) 

– Retrieve the return address from the top of the stack – Load the program counter with the return address. RET SP
- Microprocessor Things to be considered in Subroutine • The CALL instruction places the return address at the two memory locations immediately before where the Stack Pointer is pointing. – You must set the SP correctly BEFORE using the CALL instruction. • The RET instruction takes the contents of the two memory locations at the top of the stack and uses these as the return address. – Do not modify the stack pointer in a subroutine. You will loose the return address.
Things to be considered in Subroutine • Number of PUSH and POP instruction used in the subroutine must be same, otherwise, RET instruction will pick wrong value of the return address from the stack and program will fail. 


Conditional CALL and RET Instructions 

• The 8085 supports conditional CALL and conditional RTE instructions. 



Opcode
Description
Opcode
Description
Flag Status
CC
Call on Carry
RC
Return on Carry
CY=1
CNC
Call on no Carry
RNC
Return on no Carry
CY=0
CP
Call on positive
RP
Return on positive
S=0
CM
Call on minus
RM
Return on minus
S=1
CZ
Call on zero
RZ
Return on zero
Z=1
CNZ
Call on no zero
RNZ
Return on no zero
Z=0
CPE
Call on parity even
RPE
Return on parity even
P=1
CPO
Call on parity odd
RPO
Return on parity odd
P=0

Advantages of Subroutine –
  1. Decomposing a complex programming task into simpler steps.
  2. Reducing duplicate code within a program.
  3. Enabling reuse of code across multiple programs.
  4. Improving tractability or makes debugging of a program easy.
Application of Stack: Stack provides a powerful data structure which has applications in many situations. The main advantage of the stack is that, We can store data (PUSH) in it with out destroying previously stored data. This is not true in the case of other registers and memory locations. Stack operations are also very fast

The stack may also be used for storing local variables of subroutine and for the transfer of parameter addresses to a subroutine. This facilitates the implementation of re-entrant subroutines which is a very important software property.

The disadvantage is, as the stack has no fixed address, it is difficult to debug and document a program that uses stack.
  • Program 
Write a main Program and a conversion subroutine to convert the binary number stored at D000H into its equivalent BCD number. Store the result from memory location D100H.

Program :-
                  C000: LXI SP,C7FF H 
                  C003: LDA D000H
                  C006: CALL SUBROUTINE (C00A)
                  C009: RST 1

Subroutine to convert binary number into its equivalent BCD number.

                 C00A: PUSH B 
                 C00B: PUSH D
                 C00C: MVI B,64 H 
                 C00E: MVI C,0A H
                 C010: MVI D,00 H
                 C012: MVI E,00 H 
   STEP1: C014: CMP B
                 C015: JC STEP 2 (C01D)
                C018: SUB B
                C019: INR E 
                C01A:  JMP STEP1(C014)
   STEP2: C01D: CMP C
                 C01E: JC STEP 3 (C026)
                C021: SUB C
                 C022: INR D
                 C023: JMP STEP 2 (C01D)
   STEP3: C026: STA D100H
                 C029: MOV A,D
                 C02A: STA D101 H
                 C02D: MOV A,E
                 C02E: STA D102 H
                 C031: POP D
                 C032: POP B
                 C033: RET 



REFERENCES
1.     R. S. Gaonkar, Microprocessor Architecture, Programming, and Applications with the 8085, Fifth Edition, Penram International Publishing (India) Private Limited.
2.     S Ghoshal, Microprocessor Based System Design, Macmillan India Limited, 1996
3.     M. Mano, Digital Logic and Computer Design, Prentice – Hall India
4.     B. Ram - Fundamentals of Microprocessor and Microcontrollers
5.     “Microprocessors: Principles and Applications” by A Pal
6.     “Microprocessors and Microcontrollers : Architecture, Programming and Interfacing Using 8085, 8086 and 8051” by Soumitra Kumar Mandal
7.     “Introduction to Microprocessors and Microcontrollers” by Crisp John Crisp
8.     “Microprocessors And Microcontrollers” by A Nagoor Kani
9.     “Microprocessors And Microcontrollers : Architecture, Programming and System Design 8085, 8086, 8051, 8096” by KRISHNA KANT

10. 8 - Bit Microprocessor” by Vibhute

Comments