August 1999
CS202 : COMPUTER ARCHITECTURE AND DATA COMMUNICATIONS

QUESTION 5

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 5

(a) What is a subroutine? Why are subroutines useful? [3]
A subroutine is a set of CPU instructions arranged and executed in sequence or an independent block of code called for by a calling program. The usefulness of a subroutine is that it saves on storage, it is comprised in a modular structure, it has global usage and is therefore easier to test and debug.

 

(b) Explain in detail the what is meant by the term parameter passing in the context of subroutines. [4]
When calling a subroutine, a program must provide to the subroutine the parameters, that is, the operands or their addresses, to be used in the computation. Later, the subroutine returns other parameters, in this case, the result of the computation. This exchange of information between a calling program and a subroutine is referred to as parameter passing. Parameter passing may occur in several ways. The parameters may be placed in registers or in fixed memory locations, where they can be accessed by the subroutine. Alternatively, the parameters may be placed on a stack, possibly the processor stack used for saving the return address.

 

(c) Because of the nature of subroutines or nested subroutines the addresses can be stored onto a memory stack. Explain the operation of stacks. (Your answer should include the PUSH and POP operations and the role of the Stack Limit, Stack Pointer and the Stack Base). [7]
The stack is an area of memory with the stack base as first location and the stack limit as the last. Stack pointer points to top element of stack. PUSH operation increments stack pointer, and writes value from accumulator to memory location pointed to by the stack pointer. POP operation copies value from memory location pointed to by stack pointer, then decrements stack pointer. Stack overflow occurs when the stack pointer passes the stack limit; that is, when the stack pointer is at limit and a PUSH operation is executed.

 

(d) If an opcode mnemonic such as SUB, ADD, MUL, and DIV pops the top two numbers from the stack, performs the appropriate operation in the ALU then pushes the result back onto the stack, write down the expression which is calculated by the following sequence of instructions and give the value of X :

PUSH 2
PUSH 4
PUSH 5
ADD
PUSH 2
MUL
ADD
POP X

Show your working by drawing a diagram showing the state of the stack after each operation, and therefore state the minimum stack size needed to perform this calculation.

[4]
X = ( ( 4 + 5) * 2) + 2
i.e.   X = 20

Diagrams:

pic2.gif (10032 bytes)

Min stack size = 3.

 

(e) There are a number of different ways to implement a stack, using main memory and/or register storage. Which method would be most appropriate for the calculation above, and why? [2]
Entire stack in registers because memory requirement is small.