Docsity

Problem Set 4 with Solutions | Computer Organization | COMP 411, Assignments of Computer Architecture and Organization

Toggle sidebar
bg1
Comp 411 - Fall 2007 - 1 - Problem Set #4
The U
NIVERSITY
of
N
ORTH
C
AROLINA
at
C
HAPEL
H
ILL
Comp 411 Computer Organization
Fall 2007
Problem Set #4
Issued Wednesday, 9/19/06; Due Wednesday, 10/4/06
Homework Information: Some of the problems are probably too time consuming to be done the
night before the due date, so plan accordingly. Late homework will not be accepted. Feel free to
get help from others, but the work you hand in should be your own.
Problem 1. “Stack Detective”
Consider the following recursive C function to compute the n
th
Fibonacci number.
int fib(int n, int a, int b) {
if (n == 0)
return a;
else
return fib(n-1, b, a+b);
}
It is called as follows to find the 10th Fibonacci number:
fib(10, 0, 1);
After compiling, the following assembly code is generated:
fib:
L01: addu $sp,$sp,-16
L02: sw $ra,12($sp)
L03: sw $a0,8($sp)
L04: sw $a1,4($sp)
L05: sw $a2,0($sp)
L06: bne $a0,$0,L09
L07: addu $v0,$a1,$0
L08: beq $0,$0,L14
L09: subu $a0,$a0,1
L10: addu $t0,$a1,$a2
L11: addu $a1,$a2,$0
L12: addu $a2,$t0,$0
L13: jal fib
L14: lw $ra,12($sp)
L15: addu $sp,$sp,16
L16: jr $ra
a) Explain what is stored in each of the 4 words that fib() allocates on the stack. Could this
number be reduced? If so, explain how, if not explain why.
b) Suppose that the statement labeled L08 is replaced with beq $0,$0,L15. Would the
resulting fragment still work? Explain.
c) Rewrite an iterative version of the fib() function that complies with the procedure linkage
convention discussed in Lecture 7 and based on the Fibonacci code fragment given in
Lecture 6.
pf3

Partial preview of the text

Download Problem Set 4 with Solutions | Computer Organization | COMP 411 and more Assignments Computer Architecture and Organization in PDF only on Docsity!

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Comp 411 Computer Organization

Fall 2007

Problem Set

Issued Wednesday, 9/19/06; Due Wednesday, 10/4/

Homework Information: Some of the problems are probably too time consuming to be done the

night before the due date, so plan accordingly. Late homework will not be accepted. Feel free to

get help from others, but the work you hand in should be your own.

Problem 1. “Stack Detective”

Consider the following recursive C function to compute the n

th

Fibonacci number.

int fib(int n, int a, int b) { if (n == 0) return a; else return fib(n-1, b, a+b); }

It is called as follows to find the 10th Fibonacci number:

fib(10, 0, 1);

After compiling, the following assembly code is generated:

fib: L01: addu $sp,$sp,- L02: sw $ra,12($sp) L03: sw $a0,8($sp) L04: sw $a1,4($sp) L05: sw $a2,0($sp) L06: bne $a0,$0,L L07: addu $v0,$a1,$ L08: beq $0,$0,L L09: subu $a0,$a0, L10: addu $t0,$a1,$a L11: addu $a1,$a2,$ L12: addu $a2,$t0,$ L13: jal fib L14: lw $ra,12($sp) L15: addu $sp,$sp, L16: jr $ra

a) Explain what is stored in each of the 4 words that fib() allocates on the stack. Could this

number be reduced? If so, explain how, if not explain why.

b) Suppose that the statement labeled L08 is replaced with beq $0,$0,L15. Would the

resulting fragment still work? Explain.

c) Rewrite an iterative version of the fib() function that complies with the procedure linkage

convention discussed in Lecture 7 and based on the Fibonacci code fragment given in

Lecture 6.

d) Discuss the differences between your iterative fib() implementation and the given recursive

one. Which is faster? Shorter? Uses less memory? Is easier to understand?

Suppose that at some point during the execution of the given recursive fib() function the

computer is interrupted and the stack is examined and found to contain the following:

Memory Address Memory Contents

0x7fffefe0 0x 0x7fffefdc 0x 0x7fffefd8 0x 0x7fffefd4 0x 0x7fffefd0 0x 0x7fffefcc 0x 0x7fffefc8 0x 0x7fffefc4 0x 0x7fffefc0 0x 0x7fffefbc 0x 0x7fffefb8 0x 0x7fffefb4 0x 0x7fffefb0 0x 0x7fffefac 0x 0x7fffefa8 0x 0x7fffefa4 0x 0x7fffefa0 0x 0x7fffef9c 0x 0x7fffef98 0x 0x7fffef94 0xfffffffc $sp →→→→ 0x7fffef90 0x

If you use the MIPS simulator/assembler, SPIM, as an aid in answering the following questions

(which might be a good idea, though it is not necessary), you need to be aware of the following

caveat. SPIM assumes that all of memory, outside the loaded .text and .data segments is filled

with zeros. In reality, this is usually not the case. Upon power up, memory locations are filled

with apparently random values, and over the lifetime of a program the uninitialized values on the

stack reflect the activation records of previously called procedures. Therefore, you need to

consider that some of the values shown in the above stack dump may reflect uninitialized

memory locations.

e) At what memory address can the function fib() be found?

f) What argument (value of n) was passed to the originating call?

g) What is the label of the last executed instruction before the machine was interrupted?

h) What would have been the lowest memory location referenced by the stack pointer during

this particular invocation?