Stack and Subroutines

Stack and Subroutines

1. What is a stack?

Ans. A stack is a group of memory locations in the R/W memory.

2. Why stack is used in a program?

Ans. The stack is used to store information temporarily during the execution of a program.

Also the stack is used in subroutine calls to store the return address.

As an example, data generated at a certain point in a program may be needed later

in the program. This data is stored in the stack and retrieved when needed. Because the

number of general purpose registers (GPRs) in a microprocessor is limited–hence not all

the temporary data can be stored in them and this is where the stack plays its part.

3. How the stack is initialised?

Ans. The stack is initialised by a 16-bit register, called the stack pointer (SP) register.

4. Is initialisation of stack a must in a program?

Ans. No, it is not a must. If for programs for which any temporary data that are generated can be stored in GPRs and which don’t require subroutine calls, there is no need to initialise the stack by the SP.

5. What the SP register does in a program?

Ans. The SP register keeps a track with regard to the storage and retrieval of data/information.

6. Who uses the stack?

Ans. The stack is used by both the programmer and the system.

Programmer uses the stack for storage/retrieval of data by using the PUSH/POP instructions respectively.

On the other hand, the system uses the stack to store return address whenever subroutine CALL is used.

7. Comment on the size of the stack.

Ans. It depends on the size of the R/W memory, as well as the program length. Since for a given system, the size of the R/W memory is fixed, thus smaller the size of the program, more would be the size of the stack.

8. Where, in the R/W memory, a programmer should initialise the stack?

Ans. The stack should be initialised at the high end of the memory map. This is explained in Fig. 6.1.

 

10-22-2014 5-27-21 PM

The user program should start at the low end of the memory map, while the stack should be initialised at the high end of the memory map. When the program is being executed, the PC is auto-incremented while as temporary data are stored in the stack, it is auto-decremented. Thus if sufficient gap is not maintained, the program area may get corrupted by the filling-in of the stack. In such a case, the program will ‘crash’.

9. What type of memory is the stack?

Ans. Stack is a ‘last-in first-out’ or LIFO type of memory. This means that data which is pushed last into stack is popped out of it first.

10. How the stack is initialised?

Ans. The stack is initialised by means of the stack pointer. The software instruction is like this: LXI SP, 0044.

It means that the stack is initiated at the memory location 4400 by the stack pointer.

11. What are the software instructions related to stack operations? Ans. The following are stack related software instructions:

LXI SP, address

PUSH rp, POP rp

PUSH PSW, POP PSW

XTHL, SPHL

CALL – RET

DAD SP

12. Does the stack have a fixed address?

Ans. No, the stack does not have a fixed address. But usually it is initialised at the top end of the memory address.

13. What are the typical errors associated with using stack in a program?

Ans. The possible errors in stack usage are: overflow or underflow of stack, PUSH or POP instructions not carried out in proper sequence, etc.

14. By how may memory locations SP is decremented/incremented when PUSH/ POP instruction is performed.

Ans. SP is decremented/incremented by two memory locations when PUSH/POP is executed.

15. What is a subroutine?

Ans. A subroutine is a group of instructions, written separately from the main program, which performs a function that is required repeatedly in the main program.

16. Why a subroutine is used in a program?

Ans. Since a subroutine is called more than once by the main program, thus, use of subroutines saves precious memory space. The more the number of times a subroutine is called by the main program, the more is the saving of memory space.

17. Give some examples of subroutines.

Ans. Some examples of subroutines called by main program are: multiplication program, time delay subroutine, hexa-decimal converter subroutine, display subroutine, apart from any user specific subroutines that may be needed by a programmer.

18. Where do subroutines reside?

Ans. Standard subroutines like display program, time delay program, hexa-decimal conversion

programs are supplied by the manufacturers of microcomputer in monitor ROM, while any user specific programs are written in the R/W memory.

19. How subroutines can be called from the main program?

Ans. Subroutines can be called from the main program either conditionally or unconditionally.

Mnemonic for unconditional call is CALL, while there are eight conditional calls.

These are: CC, CNC, CZ, CNZ, CP, CM, CPE and CPO. CC stands for calling the subroutine if carry flag is set. CPE stands for calling the subroutine if parity flag is set (i.e., even parity).

20. How the program returns from the subroutine?

Ans. The return from the subroutine can be: conditional, unconditional.

The mnemonic for unconditional return is RET, while there are eight conditional

return instructions viz. RC, RNC, RZ, RNZ, RP, RM, RPE and RPO. RZ stands for return

if zero flag is set. RPO stands for return if parity flag is reset (odd parity).

21. Byte wise what are the lengths of CALL and RET instructions? Ans. CALL is a 3-byte instruction, while RET is a 1-byte one.

22. Explain the CALL instruction.

Ans. CALL is a 3-byte unconditional instruction, upon the execution of which the main program branches to the starting address of the subroutine. The CALL instruction looks like this.

CALL memory address

(16-bit)

An example will clarify the situation.

The main program begins at 4080 by initialising the stack pointer at 621F. When

CALL 4400 is encountered at 4100 in the main program, the return address following the CALL is 4103. The program counter is stored with the first address of the subroutine. Thus PC = 4400 and the content of the stack will look like as shown with the upper byte

of the return address i.e., 41 stored at 621E and the lower byte (i.e., 03) at the stack address 621D. Thus, the program now starts at the subroutine starting address 4400.]

10-22-2014 5-27-47 PMWhen the last subroutine instruction RET is encountered, the two contents at the memory locations 621D and 621E are popped out so that the PC content now becomes 4103—which is the return address from the subroutine. Thus the program starts at 4103 in the main program and SP returns to 621F.

23. What is multiple calling of a subroutine?

Ans.

10-22-2014 5-28-17 PM

Subroutines are normally called more than once by the main program. Calling a subroutine more than once by the main program is called “multiple calling of a subroutine.” This is shown in Fig. 6.3.

24. What is meant by ‘parameter passing’?

Ans. Subroutines are scattered at many places in the memory and that they may be called from different locations in the main program. In such cases, various types of information/data are exchanged between the main program and the subroutine. This technique goes by the name “parameter passing”.

25. Name the different types of subroutines. Ans. The different types of subroutines are:

z multiple-calling of a subroutine

z nesting of subroutines

z multiple ending subroutines.

26. Explain nesting of subroutines.

Ans. The process of a subroutine calling a second subroutine and the second subroutine in its turn calling a third one and so on is called nesting of subroutines.

Theoretically speaking, the number of subroutines that can be called by this process

is infinite but, in practice it is limited by the size of memory.

 

10-22-2014 5-29-12 PM

 

In Fig. 6.4, the main program calls the SR-I, SR-I in its turn calls SR-II while SR-II calls SR-III. The execution of the program is shown by the arrow heads. It is to be noted that each time a subroutine is called, the return addresses are saved automatically in the stack.

27. Draw an example of a multiple ending subroutine and explain.

Ans. The example of a multiple ending subroutine is shown in Fig. 6.5 which shows that the main program calls the subroutine starting at 5500.

Now the return from the subroutine can be via paths (1) or (2) (both conditional) or

(3) (unconditional). Thus the return from the subroutine can be effected in three ways—

 

10-22-2014 5-29-35 PM

i.e., why it is called a multiple ending subroutine.

28. What are the techniques of ending a subroutine?

Ans. There are two methods of ending a subroutine—multiple ending and common ending. The example of each type follows:

10-22-2014 5-29-54 PM

29. For a 8085 based system, let the following two instructions are carried out

LXI SP, 0000 H

PUSH D

with D = 09 H and E = FA H assumed.

Show the stack contents after PUSH operation.

Ans. Stack gets decremented by one memory location from the one pointed to by the stack pointer. Since the pointer is at 0000 H, so on decrementing, SP now become FFFF H at which the content of D (i.e., 09 H) is stored. The SP is again decremented to become FFFE H where content of E (i.e., FA H) is stored.

10-22-2014 5-49-52 PM

30.Discuss recursive subroutine.

Ans. A recursive subroutine is a subroutine which is called by itself and are used with complex data structures, known as ‘trees’.

For the flow diagram shown, if the subroutine is called with n = 4 (known as ‘recursive depth’), then until n becomes 0 it will stay within the recursive subroutine.

Below is shown the flow diagram for a recursive subroutine.

 

10-22-2014 5-50-18 PM

31. In how many ways nested subroutines can be classified? Ans. Nested subroutines can be classified in two ways as:

• t subroutine

• e subroutine

32. Discuss re-entrant subroutine.

Ans. In nested subroutines, many subroutines are there. In such a case, if a latter subroutine calls an earlier one, then it is known as re-entrant subroutine.

As an example, say a main program has two subroutines. The main program calls subroutine 1, then subroutine 1 calls subroutine 2. If now subroutine 1 is called from subroutine 2, then this falls under the category of re-entrant subroutines.

A scheme for such re-entrant subroutine is shown below:

 

10-22-2014 5-50-37 PM

 

33. How the stack is initialised?

Ans. There are two ways to initialise the stack

(a) Direct way

(b) Indirect way

Example of method (a) is LXI SP, 4400 H which loads the stack pointer with 4400 H. so that it points at the memory address 4400 H.

Example of method (b) is

LXI H, 16-bit data fi load 16-bit data into HL register pair SPHL fi contents of HL register pair is loaded into SP.

In most of the cases the stack pointer is initialised by direct way, but method (b) is sometimes used when one wants to set the stack pointer by means of programming.

34. At what highest memory location can the SP be initialised.

Ans. The stack pointer is decremented by one memory location before data starts storing into the stack.

Hence, theoretically, the stack pointer can be initialised to a value which is one higher them the highest read/write memory location that is available.

35. Exchange the contents of DE register pair with that of HL register pair, using PUSH, POP instructions.

Ans. It is assumed that data are existing in the four registers—D, E, H and L. The following program then interchanges the contents of DE with that of HL and the stack is already initialised by the stack pointer.

PUSH D PUSH H POP D POP H

36. Write a program which will store the contents of accumulator and flag register

at 2000 H and 2001 H memory locations respectively.

Ans. The following is the program for the above, using PUSH-POP instructions.

LXISP, 4000 H: Stack pointer is initialized at 4000 H.

PUSH PSW: Accumulator and flag register contents pushed into stack

POP B: Accumulator content goes to B and flag register content goes to C register.

MOV A, B: Content of B taken to accumulator.

STA 2000 H: Accumulator content stored into memory location 2000 H. MOV A, C : Content of C moved to accumulator.

STA 2001 H: Accumulator content stored into memory location 2001 H HLT: Program halted.

37. What is meant by ‘parameter passing technique’ as used in subroutines?

Ans. Subroutines are programs, written separately from the main program, to process data or address variable that occurs repeatedly in the main program.

In order that the subroutine can process data, it is necessary to pass the data/address variable to the subroutine. This ‘passing’ of data/address variable is referred to as passing parameters to the subroutine.

There are four ways in which this ‘passing’ can be done, as mentioned below:

(a) by using registers

(b) by using pointers

(c) by using memory

(d) by using stack.

Leave a comment

Your email address will not be published. Required fields are marked *