0% found this document useful (0 votes)
11 views38 pages

stacks & subroutines

The document explains the stack and subroutines in a microprocessor, detailing how the stack operates as a LIFO data structure for temporary storage, particularly for addresses and data during subroutine calls. It outlines methods for adding data to the stack, execution of CALL and RET instructions, and the advantages of using subroutines for modular programming and efficiency. Additionally, it provides examples of writing subroutines and the types of call and return instructions available.

Uploaded by

jayashree267geo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

stacks & subroutines

The document explains the stack and subroutines in a microprocessor, detailing how the stack operates as a LIFO data structure for temporary storage, particularly for addresses and data during subroutine calls. It outlines methods for adding data to the stack, execution of CALL and RET instructions, and the advantages of using subroutines for modular programming and efficiency. Additionally, it provides examples of writing subroutines and the types of call and return instructions available.

Uploaded by

jayashree267geo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Stacks & Subroutines

 The stack is a LIFO (last in, first out) data structure implemented in the RAM area

 The stack is an area of memory identified by the programmer for temporary storage of information.

 Used to store addresses and data when the microprocessor branches to a subroutine.

 The return address is also pushed on to the stack.

 Used to swap values of two registers and register pairs

 Apart from the general purpose registers A, B, C, D, E, H, and L, and the Flags registers, there are

two more special purpose registers, each of 16-bit width.

 They are the stack pointer, SP, and the program counter, PC.

 The SP register will hold the address of the top location of the stack.

 The PC will hold the address of the memory location from where the next instruction for execution is

to be fetched.
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.
There are two methods to add data to the stack: Direct method and
Indirect method

Direct method
In the direct method, the stack pointers address is loaded into the
stack pointer register directly.

LXI SP, 8000H


LXI H, 1234H
PUSH H
POP D
HLT
Indirect method

In the indirect method, the stack pointers address is loaded into the
stack pointer register via another register pair.

LXI H, 8000H
SPHL
LXI H, 1234H
PUSH H
POP D
HLT
CALL Execution ● Instruction requires five machine cycles and eighteen T- states: Call
instruction is fetched, 16-bit address is read during M2 and M3 and stored temporarily in W/Z
registers. In next two cycles content of program counter are stored on the stack (address from
where microprocessor continue it execution of program after completion of the subroutine.)
RET Execution ● Program execution sequence is transferred to the memory location 2043H location.M1 is
normal fetch cycle during M2 contents of stack pointer are placed on address bus so 43H data is fetched and
stored on Z register and SP is upgraded. Similarly for M3. Program sequence is transfered to2043H by placing
contents of W/Z on address bus.
What are the advantages of using subroutines?

•Subroutines avoid the repetition of instructions.


•They give an aspect of modular programming to the entire
program.
•Improves efficiency by reducing the size of the memory
needed to store the program.
Writing subroutine

Write a program that will display FF and 11 repeatedly on the seven segment
display. Write a delay subroutine and call it as when necessary.

LXI SP, FFFF


L : MVI A,FF
OUT 00
CALL delay
MVI A,11
OUT 00
CALL delay
JMP L
Writing subroutine for DELAY

delay : MVI B,FF


L1: MVI C, FF
L2 : DCR C
JNZ L2
DCR B
JNZ L1
RET
Call Instructions – The call instruction transfers the program sequence to the memory address
given in the operand. Before transferring, the address of the next instruction after CALL is
pushed onto the stack. Call instructions are 2 types: Unconditional Call Instructions and
Conditional Call Instructions.

(a) Unconditional Call Instructions: It transfers the program sequence to the memory address
given in the operand.

OPCODE OPERAND EXPLANATION EXAMPLE

CALL address Unconditionally calls CALL 2050


(b) Conditional Call Instructions: Only if the condition is satisfied, the instructions executes .

OPCODE OPERAND EXPLANATION EXAMPLE

CC address Call if carry flag is 1 CC 2050

CNC address Call if carry flag is 0 CNC 2050

CZ address Calls if zero flag is 1 CZ 2050

CNZ address Calls if zero flag is 0 CNZ 2050

CPE address Calls if parity flag is 1 CPE 2050

CPO address Calls if parity flag is 0 CPO 2050

CM address Calls if sign flag is 1 CM 2050

CP address Calls if sign flag is 0 CP 2050


Return Instructions – The return instruction transfers the program sequence from the subroutine to the
calling program. Return instructions are 2 types: Unconditional Jump Instructions and Conditional Jump
Instructions.

(a) Unconditional Return Instruction: The program sequence is transferred unconditionally from the
subroutine to the calling program.

OPCODE OPERAND EXPLANATION EXAMPLE

Return from the


RET none RET
subroutine
unconditionally
(b) Conditional Return Instruction: The program sequence is transferred unconditionally from the
subroutine to the calling program only is the condition is satisfied.

OPCODE OPERAND EXPLANATION EXAMPLE

RC none Return from the subroutine if carry flag is 1 RC

RNC none Return from the subroutine if carry flag is 0 RNC

RZ none Return from the subroutine if zero flag is 1 RZ

RNZ none Return from the subroutine if zero flag is 0 RNZ

RPE none Return from the subroutine if parity flag is 1 RPE

RPO none Return from the subroutine if parity flag is 0 RPO

RM none Returns from the subroutine if sign flag is 1 RM

RP none Returns from the subroutine if sign flag is 0 RP

You might also like