0% found this document useful (0 votes)
61 views15 pages

06 CALL & RET - PPSX

The document describes subroutines and stack operations in 8085 assembly language. It includes: 1) How subroutines work by storing the return address on the stack and popping it off when the RET instruction is executed to return to the calling program. 2) The PUSH and POP instructions are used to store registers and flags on the stack and retrieve them later. 3) An example program to calculate the factorial of a number by using a multiplication subroutine that is called repeatedly in a loop from the main factorial program.

Uploaded by

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

06 CALL & RET - PPSX

The document describes subroutines and stack operations in 8085 assembly language. It includes: 1) How subroutines work by storing the return address on the stack and popping it off when the RET instruction is executed to return to the calling program. 2) The PUSH and POP instructions are used to store registers and flags on the stack and retrieve them later. 3) An example program to calculate the factorial of a number by using a multiplication subroutine that is called repeatedly in a loop from the main factorial program.

Uploaded by

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

Subroutines & Stack

Subroutines
FACT: MULTIPLY:
2000 MVI A,00 2100 LXI H, 0000

2010 CALL MULTIPLY


(CALL 2100)
2013 MOV B,A

202A HLT 2115 RET


Subroutines (Contd.)
• 2013 is the “Return Address” that should be
stored in stack.
• SP will tell where to store the return
address in stack.
• When “RET” instruction is executed, stack
will deliver the address 2013 so that
program can return to correct point in FACT.
• SP will tell location in stack, called “STACK
TOP”, to read the return address.
Subroutines (Contd.)
• When we store any data in stack it is
referred as a PUSH operation i.e. PUSHing
into the stack.
• Every read operation from stack is called
POP operation i.e. POPing Off of stack.
• In 8085, every PUSH will store 16-bit data
at the stack top.
• In 8085, every POP will POP off 16-bit data
from the stack top.
PUSH & POP

• PUSH RP RP may be BC, DE or HL


– E.g. PUSH B (SP-1) ← B
(SP-2) ← C
SP ← SP – 2

• PUSH PSW
(SP-1) ← A
(SP-2) ← FLAG
SP ← SP – 2
PUSH & POP Contd.)

• POP RP RP may be BC, DE or HL


– E.g. POP H L ← (SP)
H ← (SP+1)
SP ← SP + 2

• POP PSW
FLAG ← (SP)
A ← (SP+1)
SP ← SP + 2
A Simple Program
Write an 8085 ALP to set Flag Status as
SF=1, ZF=0, AC=1, CY=0, and PF=1.

FLAG: SF ZF X AC X PF X CY
1 0 0 1 0 1 0 0 = 94H

MVI B, 00
MVI C, 94H
PUSH B
POP PSW
Subroutines (Revisited)
FACT: MULTIPLY:
2000 MVI A,00 2100 LXIH 0000

2010 CALL MULTIPLY


(CALL 2100)
2013 MOV B,A

202A HLT 2115 RET


CALL SUBROUTINE
• CALL ADDRESS
– E.g. CALL 2100
Z ← 00 Same as in
W ← 21 JMP 2100
PC ← PC + 2 (PC=2013)
PUSH Return
(SP-1) ← PCH
Address
(SP-2) ← PCL i.e.
SP ← SP – 2 PUSH 2013 into
the Stack
PCH ← W
PCL ← Z
i.e. PC ← 2100
RETurn from SUBROUTINE

• RET
– E.g. RET
PCL ← (SP)
PCH ← (SP+1)
SP ← SP + 2
PC ← Return Address
A Simple Program

Write an 8085 ALP to find Factorial of an


Hexadecimal Integer.

• We use multiplication program (Result 16-bit)


as subroutine.

• Factorial is Main program that would call


subroutine in a loop.

• Following may be useful to write the program.


FACTORIAL (Contd.)
• Main Program (FACT) would check the number
(N) for 0 as 0!=1.
• If appropriate, main program would call
multiplication subroutine (MULTIPLY) in a loop.
• For each next multiplication in loop, multiplier (N)
is decremented by 1 and product of last
multiplication will serve as multiplicand.
• Main program (FACT) ensures proper multiplier
and multiplicand before calling the subroutine.
• The last Product will be declared as Factorial.
• We assume following picture in memory, to write
the program.
FACTORIAL (Contd.)
• Subroutine “MULTIPLY” takes Addr Data Comments
input through memory 2500 06H NUMBER
locations 2503 (8-bit 2501 01
Multiplier) and 2504 (16-bit FACTORIAL
Multiplicand). 2502 00

• Subroutine “MULTIPLY” gives 2503 MULTIPLIER


output through memory 2504 MULTIPLIC
location 2506 (16-bit Product). 2505 AND
• Location 2500 holds the 8-bit 2506
Number, for which factorial has PRODUCT
to be determined. 2507

• Location 2501 would store the


16-bit Factorial of the given
number.
FACTORIAL – MAIN PROGRAM
FACT: LXI SP, 2400 ; Initialize Stack
LXI H,0001 ; Initial Factorial
LDA NUMBER ; Load the Number in A
CPI 00 ; Is the number ‘0’ ?
JZ STORE ; ‘YES’, Declare factorial as 1.
SHLD PRODDUCT ; ‘NO’, Store Initial Product as 1
AGAIN: LHLD PRODUCT
SHLD MULTIPLICAND ; Last Product is new Multiplicand.
STA MULTIPLIER ; Set new Multiplier
CALL MULTIPLY ; Multiply the numbers
DCR A ; Decrement Number for new Multiplier
JNZ AGAIN ; Factorial Done? ‘NO’ Go back
LHLD PRODUCT ; Get result of last product.
STORE: SHLD FACTORIAL ; Store the Factorial.
HLT ; STOP
FACTORIAL – Subroutine MULTIPLY
MULTIPLY: PUSH D ; Save Registers into the Stack
PUSH H
PUSH PSW
LDA MULTIPLIER ; Get Multiplier in A
LHLD, MULTIPLICAND
XCHG ; Get Multiplicand in DE Pair
LXI H,0000 ; Initialize partial product
BACK: DAD D ; Add Multiplicand in HL
DCR A ; Decrement multiplier by 1
JNZ BACK ; Multiplication Done? ‘NO’ go back
SHLD PRODUCT ; ‘YES’ Store the product.
POP PSW ; Retrieve Registers from the Stack
POP H
POP D
RET ; Back to Caller Program

You might also like