0% found this document useful (0 votes)
15 views9 pages

Stack and Subroutines

Uploaded by

itssam4577
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)
15 views9 pages

Stack and Subroutines

Uploaded by

itssam4577
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/ 9

Stack and Subroutines

• The stack is a special area in memory used by the CPU to store register
information or general data information during program execution. The stack has a
top and a bottom.

• A stack is a region of memory that is used to store data temporarily.

• The stack pointer (SP) points to the current top of the stack, which is the location
where the next item will be stored.

• The stack used is a Last in first out (LIFO) structure. In other words, the
programmer defines the bottom of the stack and the stack grows up into reducing
address range.

• Uses push and pull (pop) operations. In case of PUSH operation, the SP register
gets decreased by 2 and new data item used to insert on to the top of the stack.
On the other hand, in case of POP operation, the data item will have to be deleted
from the top of the stack and the SP register will get increased by the value of 2.
In the 8085, the stack is defined by setting the SP (Stack Pointer) register. LXI SP,
FFFFH
PUSH B –
• Decrement SP
• Copy the contents of register B to the memory location pointed
to by SP
• Decrement SP
• Copy the contents of register C to the memory location pointed
to D
POP by SP
• Increment SP
• Copy the contents of the memory location pointed to by the SP
to register E Copy the contents of the memory location pointed
to by the SP to register D
• Increment SP

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.
Step 1

Step 2 Step 3
Step 1

Step 2

Step 3
Subroutine
• 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.

• The 8085 has two instructions for dealing with subroutines.

• The CALL instruction is used to redirect program execution to the


subroutine.

• The RET instruction is used to return the execution to the calling


routine.
If a JMP instruction is executed, we jump to the
destination location, and the execution carries on
from there, without bothering to come back later to
the instruction after the JMP. On the other hand, if a
CALL instruction is executed, we jump to the
subroutine, and the execution carries on from there
till the RET instruction is executed in the subroutine,
and then we come back to the instruction after the
CALL 4000H

• Push the address of the instruction immediately following the CALL onto the
stack .

• Load the program counter with the 16-bit address supplied with the CALL
instruction.

• You must set the SP correctly BEFORE using the CALL instruction.

RET

• Retrieve the return address from the top of the stack

• Load the program counter with the return address.

• Do not modify the stack pointer in a subroutine. You will loose the
return address
Nesting Subroutine
Practice problem

You might also like