Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Henzon Jay D.

Bael 05/11/21

BSEE-III

Activity 5

CPE 232 - LEC

1. What is stack? Explain the use of the stack, and stack pointer and how they are affected
by instruction such as PUSH, POP, CALL and RET.

Answer:

Stack is a memory usage model. It is simply part of the system memory, and a pointer
register (inside the processor) is used to make it work as a first-in/last-out buffer. The
common use of a stack is to save register contents before some data processing and then
restore those contents from the stack after the processing task is done.

When doing PUSH and POP operations, the pointer register, commonly called stack
pointer, is adjusted automatically to prevent next stack operations from corrupting
previous stacked data. More details on stack operations are provided on later part of this
chapter. It is not necessary to use both SPs. Simple applications can rely purely on the
MSP. The SPs are used for accessing stack memory processes such as PUSH and POP.
The CALL instruction preserves the current value of the instruction pointer, pushing it
onto the stack in order to support nested function calls, and then loads the instruction
pointer with the new address, provided as an operand to the instruction. This value on the
stack is referred to as the return address. Whenever the function has finished executing,
the RET instruction pops the return address off of the stack and restores it into the
instruction pointer, thus transferring control back to the function that initiated the
function call.

2. What is subroutine? How is it useful? Explain the use of stack in CALL and RETURN
instructions.

Answer:

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. 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 instruction is used to return the execution to the calling routine.

3. The first four instruction of a typical subroutine are:


PUSH PSW, PUSH H,
PUSH B, PUSH D

Answer:

PUSH PSW

–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

PUSH H Transferring the (HL) to stack memory. (one byte instruction)


PUSH B Transferring the (BC) to stack memory. (one byte instruction)

PUSH D Transferring the (DE) to stack memory. (One byte instruction)

PUSH B requires 1-Byte, 3-Machine Cycles (Opcode Fetch, Memory Write, and Memory Write)
and 12 T-States for execution

4. If the CALL and RET instructions of the subroutine? Explain clearly.

Answer:

The CALL instruction interrupts the flow of a program by passing control to an internal
or external subroutine. An internal subroutine is part of the calling program. An external
subroutine is another program. The RETURN instruction returns control from a
subroutine back to the calling program and optionally returns a value. When calling an
internal subroutine, CALL passes control to a label specified after the CALL keyword.
When the subroutine ends with the RETURN instruction, the instructions following
CALL are processed.

5. If the CALL and RET instructions are not provided in the 8085A, could it possibly be
possible to write subroutines for this microprocessor? If so, how will you call and return
from the subroutine?

Answer:

If the condition associated with the conditional CALL is not met, the program counter
contents are saved on the stack, and the address contained in the CALL instruction is
loaded into program counter. Whenever the instructions in a subroutine are required to be
executed, we branch program control to the subroutine using th CALL instruction.
CALL is a 3-Byte instruction, with 1 Byte for the opcode, and 2 Bytes for the address of
the subroutine. CALL mnemonics stands for “call a subroutine”. After executing the
instructions written in the subroutine we shall want to return control to the next
instruction written after the CALL instruction then we shall use mnemonic RET.
Here RET stands for RETurn from the subroutine. RET is a 1-Byte instruction.

6. Explain the necessity of subroutine documentation.

Answer:

Subroutine program must provide enough information so that other users can utilize the
subroutine without having to examine its internal structure. So along with subroutine
program it is necessary to give the following guidelines.

Description of the purpose of the subroutine


A list of passing parameters
Return value
Register and memory and memory locations used
Sample code

7. What do you mean by nested subroutines?

Answer:

A nested function (or nested procedure or subroutine) is a function which is defined


within another function, the enclosing function. Due to simple recursive scope rules, a
nested function is itself invisible outside of its immediately enclosing function, but can
see (access) all local objects (data, functions, types, etc.) of its immediately enclosing
function as well as of any function(s) which, in turn, encloses that function. The nesting
is theoretically possible to unlimited depth, although only a few levels are normally used
in practical programs.

When one subroutine calls another subroutine to complete a particular task, the
operations is called nesting. The second subroutine may in turn call a third subroutine and
so on each successive CALL without an intervening return creates an additional level of
nesting. These routines are called nested subroutines.

8. Explain the reentrant subroutine.

Answer:

In some situations it may happen that subroutine1 is called from main program,
subroutine2 is called from subroutine1 and subroutine1 is again called from subroutine2
in this situation program execution flow re-enters in the subroutine1. These types of
subroutines are called re-entrant subroutines.

9. Explain the recursive subroutine with the help of example.

Answer:

A recursive subroutine is a subroutine which calls itself. Recursive subroutines are used
to work with complex data structures called trees. If the subroutine is called with N
(recursion depth) = 3, then the N is decremented by 1 after each subroutine CALL and
the subroutine is called until N=0.

You might also like