Stack and Subroutines in 8085 8085 Microprocessor by Ravinder Nath Rajotiya - October 13, 2021October 13, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Stack and SubroutinesStackInstructions Used:Usage:Example:SubroutineUnconditional subroutine CALL and RET instructions Stack and Subroutines Stack and subroutine are powerful mechanism in 8085 microprocessor for modularizing the assembly program, Stack A stack is a first in last out memory locations in the r/w memory and is used during the execution of a program. This memory is generally specified by the programmer in the main program. To point in the stack memory every processor has a pointer known as the stack pointer. To use the stack, the stack pointer is fist loaded with the beginning address of the stack memory by using the instruction LXI SP, XXXXh. Where XXXXh denotes a 16-bit memory address. The stack may grow up or down. In 8085 the stack grows downward in the reverse order, that means, that once the stack is defined, storing of the data begins at a memory address one less than the address in the SP. As a general rule, the stack is initialized at the higher available memory location to prevent the program from being destroyed by the stack information. The size of the stack is limited only by the available memory. Instructions Used: The stack uses only two instructions : PUSH – to store register pair contents on the stack POP – to retrieve the contents from top two locations into the register pair. Also the microprocessor automatically stores the contents of the PC when a subroutine is called. And top two stack memory content retrieved in PC when returning from subroutine. Usage: LXI SP, XXXXh Initialize SP to the beginning of the stack memory PUSH Rp ; where Rp can be register pair BC, DE, HL or PSW (A+Flag). // Decrements the SP and copies contents of high-order register; again the decrement the //SP and then copy the contents of low-order register in memory pointed by SP PUSH B //decrement SP, copy high byte (Reg B); decrement SP and copy low byte (reg-C) PUSH PSW // decrement SP, copy high byte (Reg A); decrement SP and copy low byte (flag register) POP Rp // copies contents of top two memory location in the specified register pair. // first the contents from stack top indicated by SP is copied in low-order register and SP //incremented; then the high byte is copied in the high byte register and SP again //incremented Example: LXI SP, 2095h LXI H, 1234h PUSH H — — POP B The 1st instruction in the above code initializes the stack pointer (SP) to 2095 indicating beginning of the stack memory. The 2nd instruction load register pair HL with the data 1234h with high-order byter “12” in H and low-order byte “34” in register L. Copy contents of register pair “HL” in two locations stack memory 2094 and 2093 as stack grow down. Figure-1: PUSH and POP operation on stack Finally the instruction POP B retrieves top two stack locations in register pair BC with content of location 2093 in low-order register ‘C’ and SP incremented. Then copies content of loactioon 2094 in the high-order register ‘B’. Subroutine A subroutine is used to modularise the main program. It is a set of instructions that are written separately from the main program to perform tasks that repeatedly occurring tasks. 8085 has two instructions CALL and RET for calling the subroutine and returning from the subroutine. Unconditional subroutine CALL and RET instructions CALL label / 16-bit address – A three byte instruction On execution of this instruction, the content of the program counter (PC), which is the address of the instruction following the CALL instruction, is saved on to the stack, and execution of the program is transferred to subroutine. RET – it is one byte instruction On execution of this instruction, the content of the stack is retrieved in PC and the execution of the main program continues. Example: Write a program in AL of 8085 to provide given ON/OFF time to three traffic lights(G,R, Y) and two pedestrian sign (walk and Don’t walk). The signal lights and the sign are turned ON/OFF by data bit of an output port as shown below: Green light D0 15 seconds Yellow Light D2 5 seconds Red Light D4 20 Seconds Walk D6 15 seconds Don’t walk D7 25 seconds Traffic and pedestrian flow are in same direction; the pedestrian should cross the road when light is Green. Solution: Analysis of Lights (LEDs in this example) Fig-2: Traffic Light Analysis Flowchart for traffic light Figure-3: Flowchart of Traffic Light Complete Program Main Traffic Light Program Delay Subrouting Address Opcode Mnemonic Address Opcode Mnemonic XX00 31 99 00 LXI SP, XX99 XX50 D5 Delay : PUSH D XX03 3E 41 START: MVI A, 41h XX51 F5 PUSH PSW XX05 D3 PORT# OUT PORT# XX52 11 LO, HI_BYTE Seconds: LXI D, count XX07 06 0F MVI B, 0Fh XX55 1B Loop1 : DCX D XX09 CD 50 XX CALL Delay XX56 7A MOV A, D XX0C 3E 84 MVI A, 84h XX57 B3 ORA E XX0E D3 PORT# OUT PORT# XX58 C2 55 XX JNZ loop1 XX10 06 05 MVI B, 05 XX5B 05 DCR B XX12 CD 50 XX CALL Delay XX5C C2 52 XX JNZ seconds XX15 3E 90 MVI A, 90h XX5F F1 POP PSW XX17 D3 PORT# OUT PORT# XX60 D1 POP D XX19 06 14 MVI B, 14h XX61 C9 RET XX1B CD 50 XX CALL Delay XX1E C3 03 XX JMP START As seen in the program coding for main program and subroutine program, the main program starts from memory location XX00h and continues up to XX1Eh. the delay subroutine is call three time from main main program from address XX09h, XX12h and XX1Bh and a result the return address XX1C, XX15h and XX1Eh respectively are saved on to the stack and retrieved when RET instruction are encountered in the delay subroutine. The stack pointer is initialized in the beginning of the main program to XX99h, so that the stack stores the return address at XXFF and XXFE locations Figure-4: Multiple-Call mechanism Conditional subroutine CALL and RETURn instructions Conditional Call Instructions Conditional Return Instructions Remarks CC RC Call / return on carry (when cy=1) CNC RNC Call / return on no carry (when cy=0) CZ RZ Call / return on zero (when Z=1) CNZ RNZ Call / return on zero (when Z=0) CM RM Call / return on minus (when S=1) CP RP Call / return on minus (when S=0) CPE RPE Call / return when parity is even (when P=1) CPO RPO Call / return when parity is odd (when P=0) Comparison of Subroutine CALL/RET and PUSH/POP instructions CALL and RET PUSH and POP On a call to subroutine, uP automatically stores the return address (i.e. address of next instruction) onto the stack PUSH instruction stores the content of the register pair onto the stack While saving the return address on the stack the SP is decremented by two. Procedure is decrement SP and save the contents On executing the PUSH instruction, the SP is decremented by two. Procedure is decrement SP and save the contents When RET is executed, the contents from top two locations are save in the PC. Procedure is read the content and increment the SP On executing the POP instruction, the contents from top two locations are save in the specified register pair. Procedure is read the content from top of stack and increment the SP There are unconditional and conditional CALL and RET instructions There is no conditional PUSH or POP instructions. Share on Facebook Share Send email Mail Print Print