0% found this document useful (0 votes)
140 views58 pages

Chapter 2

The passage discusses the importance of teaching students how to learn effectively and efficiently. It states that students should be taught strategies for organizing, categorizing, memorizing, and retaining information. These skills will help students learn new concepts across all subjects more quickly and allow them to build on prior knowledge throughout their education.

Uploaded by

SomaticQi
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)
140 views58 pages

Chapter 2

The passage discusses the importance of teaching students how to learn effectively and efficiently. It states that students should be taught strategies for organizing, categorizing, memorizing, and retaining information. These skills will help students learn new concepts across all subjects more quickly and allow them to build on prior knowledge throughout their education.

Uploaded by

SomaticQi
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/ 58

SOLUTIONS

CHAPTER 2
EXERCISE 2.4
 The following problems deal with translating
from C to MIPS. Assume that the variables f, g, h,
i, and j are assigned to registers $s0, $s1, $s2, $s3,
and $s4, respectively. Assume that the base
address of the arrays A and B are in registers $s6
and $s7, respectively.
a. f = -g - A[4];
b. B[8] = A[i - j]
 2.4.1 For the C statement above, what is the
corresponding MIPS assembly code?
 Solution:

a. lw $s0, 16($s6)
sub $s0, $0, $s0
sub $s0, $s0, $s1
b. sub $t0, $s3, $s4
slli $t0, $t0, 2
add $t0, $s6, $t0
lw $t1, 0($t0)
sw $t1, 32($s7)
 2.4.2 For the C statements above, how many
MIPS assembly instructions are needed to
perform the C statement?
 Solution:

a. 3
b. 5
 2.4.3 For the C statement above, how many
different registers are needed to carry out the C
statement?
 Solution:

a. 3
b. 6
 The following problems deal with translating from
MIPS to C. Assume that the variables f, g, h, i, and j
are assigned to registers $s0, $s1, $s2, $s3, and $s4,
respectively. Assume that the base address of the
arrays A and B are in registers $s6 and $s7,
respectively.
a. slli $s2, $s4, 1 // h=j*2
add $s0, $s2, $s3 // f=j*2+i
add $s0, $s0, $s1 // f=j*2+i+g
b. add $t0, $s6, $s0 //t0= &A[f/4]
add $t1, $s7, $s1 // t1= &B[g/4]
lw $s0, 0($t0) //s0= A[f/4]
addi $t2, $t0, 4 //t2= &A[f/4+1]
lw $t0, 0($t2) // t0= A[f/4+1]
add $t0, $t0, $s0 // t0= A[f/4]+ A[f/4+1]
sw $t0, 0($t1) // B[g/4]= A[f/4]+ A[f/4+1]
 2.4.4 For the MIPS assembly instructions above,
what is the corresponding C statement?
 Solution:

a. f = 2j + i + g;
b. B[g] = A[f] + A[1+f];
 2.4.5 For the MIPS assembly instructions above,
rewrite the assembly code to minimize the
number if MIPS instructions (if possible) needed
to carry out the same function.
 Solution:

a. slli $s2, $s4, 1


add $s0, $s2, $s3
add $s0, $s0, $s1
b. add $t0, $s6, $s0
add $t1, $s7, $s1
lw $s0, 0($t0)
lw $t0, 4($t0)
add $t0, $t0, $s0
sw $t0, 0($t1)
 2.4.6 How many registers are needed to carry
out the MIPS assembly as written above? If you
could rewrite the code above, what is the minimal
number of registers needed?
 Solution:

a. 5 as written, 5 minimally
b. 7 as written, 6 minimally
EXERCISE 2.6
 The following problems deal with translating
from C to MIPS. Assume that the variables f, g, h,
i, and j are assigned to registers $s0, $s1, $s2, $s3,
and $s4, respectively. Assume that the base
address of the arrays A and B are in registers $s6
and $s7, respectively. Assume that the elements
of arrays A and B are 4-byte words:
a. f = -g+h+B[1]
b. f=A[B[g]+1]
 2.6.1 For the C statement above, what is the
corresponding MIPS assembly code?
 Solution:

a. lw $t0, 4($s7) # $t0 <-- B[1]


sub $t0, $t0, $s1 # $t0 <-- B[1] − g
add $s0, $t0, $s2 # f <-- B[1] −g + h
b. sll $t0, $s1, 2 # $t0 <-- 4*g
add $t0, $t0, $s7 # $t0 <-- Addr(B[g])
lw $t0, 0($t0) # $t0 <-- B[g]
addi $t0, $t0, 1 # $t0 <-- B[g]+1
sll $t0, $t0, 2 # $t0 <-- 4*(B[g]+1) = Addr(A[B[g]+1])
add $t0, $t0, $s6
lw $s0, 0($t0) # f <-- A[B[g]+1]
 2.6.2 For the C statements above, how many
MIPS assembly instructions are needed to
perform the C statement?
 Solution:

a. 3
b. 6
 2.6.3 For the C statements above, how many
registers are needed to carry out the C statement
using MIPS assembly code?
 Solution:

a. 5
b. 4
 The following problems deal with translating
from MIPS to C. The following problems deal
with translating from C to MIPS. Assume that
the variables f, g, h, i, and j are assigned to
registers $s0, $s1, $s2, $s3, and $s4, respectively.
Assume that the base address of the arrays A
and B are in registers $s6 and $s7, respectively.
a. sub $s0, $s0, $s1
sub $s0, $s0, $s3
add $s0, $s0, $s1
b. addi $t0, $s6, 4
add $t1, $s6, $0
sw $t1, 0($t0)
lw $t0, 0($t0)
add $s0, $t1, $t0
 The following problems deal with translating
from MIPS to C. The following problems deal
with translating from C to MIPS. Assume that
the variables f, g, h, i, and j are assigned to
registers $s0, $s1, $s2, $s3, and $s4, respectively.
Assume that the base address of the arrays A
and B are in registers $s6 and $s7, respectively.
a. sub $s0, $s0, $s1 // f=f-g
sub $s0, $s0, $s3 // f=f-g-i
add $s0, $s0, $s1 //f=f-i
b. addi $t0, $s6, 4 // t0=&(A[1])
add $t1, $s6, $0 // t1=&A[0]
sw $t1, 0($t0) // A[1]= &A[0]
lw $t0, 0($t0) // t0=&A[0]
add $s0, $t1, $t0 // f0=&A[0]+&A[0]
 2.6.4 For the MIPS assembly instructions above,
what is the corresponding C statement?
 Solution:

a. f = f – i;
b. f = 2 * (&A);
 2.6.5 For the MIPS assembly above, assume that
the registers $s0, $s1, $s2, and $s3 contain the
values 0x0000000a, 0x00000014, 0x0000001e,
and 0x00000028, respectively. Also, assume that
register $s6 contains the value 0x00000100, and
that memory contains the following values:
Address Value
0x00000100 0x00000064
0x00000104 000000c8
0x00000108 0000012c

Find the value of $s0 at the end of the assembly


code.
 Solution:
a. $s0 = −30 // add $s0, $s0, $s1 //f=f-i
b. $s0 = 512 //f0=&A[0]+&A[0]
 2.6.6 For each MIPS instruction, show the value
of the opcode(OP), source register(RS), and target
register(RT) fields. For the I-type instructions,
show the value of the immediate field, and for the
R-type instructions, show the value of the
desination register(RD) field.
 Solution:
a Type opcode rs rt rd immed
sub $s0,$s0, $s1 R-type 0 16 17 16
sub $s0, $s0, $s3 R-type 0 16 19 16
add $s0, $s0, $s1 R-type 0 16 17 16
b Type opcode rs rt rd immed
addi $t0, $s6, 4 I-type 8 22 8 4
add $t1, $s6, $0 R-type 0 22 0 9
sw $t1, 0($t0) I-type 43 8 9 0
lw $t0, 0($t0) I-type 35 8 8 0
add $s0, $t1, $t0 R-type 0 9 8 16
EXERCISE 2.14
 The following figure shows the placement of a bit
field in register $t0.
31 i, i-1 j,j-1 0
Field
32 - i bits i – j bits j bits

In the following problems, you will be asked to


write MIPS instructions to extract the bits “Field”
from register $t0 and place them into register $t1
at the location indicated in the following table.
31 31 – (i - j) 0
a Field 000…000

31 14 + i – j bits 14 0
b 111…111 Field 111…111
 2.14.1 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 22 and j = 5 and places the
field into $t1 in the format shown in the data
table.
 Solution:

a. lui $t1, 0x003f //


ori $t1, $t1, 0xffe0
and $t1, $t0, $t1
srl $t1, $t1, 5
b. lui $t1, 0x003f (reverse the pattern)
ori $t1, $t1, 0xffe0
or $t1, $t0, $t1
sll $t1, $t1, 9
 2.14.2 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 4 and j = 0 and places the
field into $t1 in the format shown in the data
table.
 Solution:

a. add $t1, $t0, $0


sll $t1, $t1, 28
b. andi $t0, $t0, 0x000f
sll $t0, $t0, 14
ori $t1, $t1, 0x3fff
sll $t1, $t1, 18
ori $t1, $t1, 0x3fff
or $t1, $t1, $t0
 2.14.3 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 31 and j = 28 and places the
field into $t1 in the format shown in the data
table.
 Solution:
a. srl $t1, $t0, 28
sll $t1, $t1, 29
b. srl $t0, $t0, 28
andi $t0, $t0, 0x0007
sll $t0, $t0, 14
ori $t1, $t1, 0x7fff
sll $t1, $t1, 17
ori $t1, $t1, 0x3fff
or $t1, $t1, $t0
 In the following problems, you will be asked to
write MIPS instructions to extract the bits “Field”
from register $t0 shown in the figure and place
them into register $t1 at the location indicated in
the following table. The bits shown as “XXX” are
to remain unchanged.
31 31 – (i - j) 0
a Field XXX…XXX

31 14 + i – j bits 14 0
b XXX…XXX Field XXX…XXX
 2.14.4 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 17 and j = 11 and places the
field into $t1 in the format shown in the data
table.
 Solution:

a. srl $t0, $t0, 11


sll $t0, $t0, 26
ori $t2, $0, 0x03ff
sll $t2, $t2, 16
ori $t2, $t2, 0xffff
and $t1, $t1, $t2
or $t1, $t1, $t0
 2.14.4 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 17 and j = 11 and places the
field into $t1 in the format shown in the data
table.
 Solution:

b. srl $t0, $t0, 11


sll $t0, $t0, 26
srl $t0, $t0, 12
ori $t2, $0, 0xfff0
sll $t2, $t2, 16
ori $t2, $t2, 0x3fff
and $t1, $t1, $t2
or $t1, $t1, $t0
 2.14.5 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 5 and j = 0 and places the
field into $t1 in the format shown in the data
table.
 Solution:

a. sll $t0, $t0, 27


ori $t2, $0, 0x07ff
sll $t2, $t2, 16
ori $t2, $t2, 0xffff
and $t1, $t1, $t2
or $t1, $t1, $t0
 2.14.5 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 5 and j = 0 and places the
field into $t1 in the format shown in the data
table.
 Solution:

b. sll $t0, $t0, 27


srl $t0, $t0, 13
ori $t2, $0, 0xfff8
sll $t2, $t2, 16
ori $t2, $t2, 0x3fff
and $t1, $t1, $t2
or $t1, $t1, $t0
 2.14.6 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 31 and j = 29 and places the
field into $t1 in the format shown in the data
table.
 Solution:

a. srl $t0, $t0, 29


sll $t0, $t0, 30
ori $t2, $0, 0x3fff
sll $t2, $t2, 16
ori $t2, $t2, 0xffff
and $t1, $t1, $t2
or $t1, $t1, $t0
 2.14.6 Find the shortest sequence of MIPS
instructions that extracts a field from $t0 for the
constant values i = 31 and j = 29 and places the
field into $t1 in the format shown in the data
table.
 Solution:

b. srl $t0, $t0, 29


sll $t0, $t0, 30
srl $t0, $t0, 16
ori $t2, $0, 0xffff
sll $t2, $t2, 16
ori $t2, $t2, 0x3fff
and $t1, $t1, $t2
or $t1, $t1, $t0
EXERCISE 2.20
 This exercise deals with recursive procedure calls. For the
following problems, the table has an assembly code
fragment that computes the factorial of a number. However,
the entries in the table have errors, and you will be asked
to fix these errors. For number n, factorial of n =
1×2×3×…×n.
 a.FACT: sw $ra, 4($sp)  b.FACT: addi $sp, $sp, 8
 sw $a0, 0($sp)  sw $ra, 4($sp)
 addi $sp, $sp, -8  sw $a0, 0($sp)
 slti $t0, $a0, 1  add $s0, $0, $a0
 beq $t0, $0, L1  slti $t0, $a0, 2
 addi $v0, $0, 1  beq $t0, $0, L1
 addi $sp, $sp, 8  mul $v0, $s0, $v0
 jr $ra  addi $sp, $sp, -8
 jr $ra
 L1: addi $a0, $a0, -1  L1: addi $a0, $a0, -1
 jal FACT  jal FACT
 addi $sp, $sp, 8  addi $v0, $0, 1
 lw $a0, 0($sp)  lw $a0, 0($sp)
 lw $ra, 4($sp)  lw $ra, 4($sp)
 mul $v0, $a0, $v0  addi $sp, $sp, -8
 jr $ra  jr $ra
 2.20.1 The MIPS assembly program above
computes the factorial of a given input. The
register input is passed through register $a0, and
the result is returned in register $v0. In the
assembly code, there are a few errors. Correct the
MIPS errors.
 Solution:
 a.
 FACT:
 addi $sp, $sp, −8 # make room in stack for 2 more items
 sw $ra, 4($sp) # save the return address
 sw $a0, 0($sp) # save the argument n
 slti $t0, $a0, 1 # $t0 = $a0 x 2
 beq, $t0, $0, L1 # if $t0 = 0, goto L1
 add $v0, $0, 1 # return 1
 add $sp, $sp, 8 # pop two items from the stack
 jr $ra # return to the instruction after jal
 L1:
 addi $a0, $a0, −1 # subtract 1 from argument
 jal FACT # call fact(n−1)
 lw $a0, 0($sp) # just returned from jal: restore n
 lw $ra, 4($sp) # restore the return address
 add $sp, $sp, 8 # pop two items from the stack
 mul $v0, $a0, $v0 # return n*fact(n−1)
 jr $ra # return to the caller
 Solution:
 b.
 FACT:
 addi $sp, $sp, −8 # make room in stack for 2 more items
 sw $ra, 4($sp) # save the return address
 sw $a0, 0($sp) # save the argument n
 slti $t0, $a0, 1 # $t0 = $a0 x 2
 beq, $t0, $0, L1 # if $t0 = 0, goto L1
 add $v0, $0, 1 # return 1
 add $sp, $sp, 8 # pop two items from the stack
 jr $ra # return to the instruction after jal
 L1:
 addi $a0, $a0, −1 # subtract 1 from argument
 jal FACT # call fact(n−1)
 lw $a0, 0($sp) # just returned from jal: restore n
 lw $ra, 4($sp) # restore the return address
 add $sp, $sp, 8 # pop two items from the stack
 mul $v0, $a0, $v0 # return n*fact(n−1)
 jr $ra # return to the caller
 2.20.2 For the recursive factorial MIPS program
above, assume that the input is 4. Rewrite the
factorial program to operate in a non-recursive
manner. Restrict your register usage to registers
$s0-$s7. What is the total number of instructions
used to execute your solution from 2.20.2 versus
the recursive version of the factorial program?
 Solution:
a.
25 MIPS instructions to execute non-recursive vs. 45
instructions to execute (corrected version of) recursion
Non-recursive version:
 FACT: addi $sp, $sp, −4
 sw $ra, 4($sp)
 add $s0, $0, $a0
 add $s2, $0, $1
 LOOP: slti $t0, $s0, 2
 bne $t0, $0, DONE
 mul $s2, $s0, $s2
 addi $s0, $s0, −1
 j LOOP
 DONE: add $v0, $0, $s2
 lw $ra, 4($sp)
 addi $sp, $sp, 4
 jr $ra
 Solution:
b.
25 MIPS instructions to execute non-recursive vs. 45
instructions to execute (corrected version of) recursion
Non-recursive version:
 FACT: addi $sp, $sp, −4
 sw $ra, 4($sp)
 add $s0, $0, $a0
 add $s2, $0, $1
 LOOP: slti $t0, $s0, 2
 bne $t0, $0, DONE
 mul $s2, $s0, $s2
 addi $s0, $s0, −1
 j LOOP
 DONE: add $v0, $0, $s2
 lw $ra, 4($sp)
 addi $sp, $sp, 4
 jr $ra
 2.20.3 Show the contents of the stack after each
function call, assuming that the input is 4.
 Solution:
a.
Recursive version
 FACT: addi $sp, $sp, −8
 sw $ra, 4($sp)
 sw $a0, 0($sp)
 add $s0, $0, $a0
 HERE: slti $t0, $a0, 2
 beq $t0, $0, L1
 addi $v0, $0, 1
 addi $sp, $sp, 8
 jr $ra
 L1: addi $a0, $a0, −1
 jal FACT
 mul $v0, $s0, $v0
 lw $a0, 0($sp)
 lw $ra, 4($sp)
 addi $sp, $sp, 8
 jr $ra
 Solution:
a.
at label HERE, after calling function FACT with
input of 4:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
$sp-> −8 contents of register $a0
at label HERE, after calling function FACT with
input of 3:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
$sp -> −16 contents of register $a0
 Solution:
a.
at label HERE, after calling function FACT with
input of 2:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
−16 contents of register $a0
−20 contents of register $ra
$sp -> −24 contents of register $a0
 Solution:
a.
at label HERE, after calling function FACT with
input of 1:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
−16 contents of register $a0
−20 contents of register $ra
−24 contents of register $a0
−28 contents of register $ra
$sp -> −32 contents of register $a0
 Solution:
b.
Recursive version
 FACT: addi $sp, $sp, −8
 sw $ra, 4($sp)
 sw $a0, 0($sp)
 add $s0, $0, $a0
 HERE: slti $t0, $a0, 2
 beq $t0, $0, L1
 addi $v0, $0, 1
 addi $sp, $sp, 8
 jr $ra
 L1: addi $a0, $a0, −1
 jal FACT
 mul $v0, $s0, $v0
 lw $a0, 0($sp)
 lw $ra, 4($sp)
 addi $sp, $sp, 8
 jr $ra
 Solution:
b.
at label HERE, after calling function FACT with
input of 4:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
$sp-> −8 contents of register $a0
at label HERE, after calling function FACT with
input of 3:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
$sp -> −16 contents of register $a0
 Solution:
b.
at label HERE, after calling function FACT with
input of 2:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
−16 contents of register $a0
−20 contents of register $ra
$sp -> −24 contents of register $a0
 Solution:
b.
at label HERE, after calling function FACT with
input of 1:
old $sp -> 0xnnnnnnnn ???
−4 contents of register $ra
−8 contents of register $a0
−12 contents of register $ra
−16 contents of register $a0
−20 contents of register $ra
−24 contents of register $a0
−28 contents of register $ra
$sp -> −32 contents of register $a0
 For the following problems, the table has an
assembly code fragment that computes a
Fibonacci number. However, the entries in the
table have errors, and you will be asked to fix the
errors. For number n, the Fibonacci of n is
calculated as follows:
n Fibonacci of n
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
 a.  b.
FIB: addi $sp, $sp, -12 FIB: addi $sp, $sp, -12
sw $ra, 0($sp) sw $ra, 8($sp)
sw $s1, 4($sp) sw $s1, 4($sp)
sw $a0, 8($sp)
sw $a0, 0($sp)
slti $t0, $a0, 1
slti $t0, $a0, 3
beq $t0, $0, L1
addi $v0, $a0, $0
beq $t0, $0, L1
j EXIT addi $v0, $0, 1
L1: addi $a0, $a0, -1 j EXIT
jal FIB L1: addi $a0, $a0, -1
addi $s1, $v0, $0 jal FIB
addi $a0,$a0, -1 addi $a0, $a0, -2
jal FIB jal FIB
add $v0, $v0, $s1 add $v0, $v0, $s1
EXIT:lw $ra, 0($sp) EXIT:lw $a0, 0($sp)
lw $a0, 8($sp) lw $s1, 4($sp)
lw $s1, 4($sp) lw $ra, 8($sp)
addi $sp, $sp, 12
addi $sp, $sp, 12
jr $ra
jr $ra
 2.20.4 The MIPS assembly program above
computes the Fibonacci of a given input. The
integer input is passed through register $a0, and
the result is returned in register $v0. In the
assembly code, there are a few errors. Correct the
MIPS errors.
 Solution:
a.
 FIB:  L1:
 addi $sp, $sp, −12  addi $a0, $a0, −1

 sw $ra, 8($sp)  jal FIB


 addi $s1, $v0, $0
 sw $s1, 4($sp)
 addi $a0, $a0, −1
 sw $a0, 0($sp)
 jal FIB
 slti $t0, $a0, 3
 add $v0, $v0, $s1
 beq $t0, $0, L1
 EXIT:
 addi $v0, $0, 1
 lw $a0, 0($sp)
 j EXIT
 lw $s1, 4($sp)
 lw $ra, 8($sp)
 addi $sp, $sp, 12
 jr $ra
 Solution:
b.
 FIB:  L1:
 addi $sp, $sp, −12  addi $a0, $a0, −1
 jal FIB
 sw $ra, 8($sp)
 addi $s1, $v0, $0
 sw $s1, 4($sp)
 addi $a0, $a0, −1
 sw $a0, 0($sp)
 jal FIB
 slti $t0, $a0, 3
 add $v0, $v0, $s1
 beq $t0, $0, L1
 EXIT:
 addi $v0, $0, 1  lw $a0, 0($sp)
 j EXIT  lw $s1, 4($sp)
 lw $ra, 8($sp)
 addi $sp, $sp, 12
 jr $ra
 2.20.5 For the recursive Fibonacci MIPS
program above, assume that the input is 4.
Rewrite the Fibonacci program to operate in a
non-recursive manner. Restrict your register
usage to registers $s0 - $s7. What is the total
number of instructions used to execute your
solution from 2.20.2 versus the recursive version
of the factorial program?
 Solution:
a.
23 MIPS instructions to execute non-recursive vs. 73
instructions to execute (corrected version of) recursion.
Non-recursive version:
 FIB:  LOOP:  EXIT:
 addi $sp, $sp, −4  slti $t0, $a0, 3  add $v0, s1, $0
 sw $ra, ($sp)  bne $t0, $0, EXIT  lw $ra, ($sp)
 addi $s1, $0, 1  add $s3, $s1, $0  addi $sp, $sp, 4
 addi $s2, $0, 1  add $s1, $s1, $s2  jr $ra
 add $s2, $s3, $0
 addi $a0, $a0, −1
 j LOOP
 Solution:
b.
23 MIPS instructions to execute non-recursive vs. 73
instructions to execute (corrected version of) recursion:
Non-recursive version:
 FIB:  LOOP:  EXIT:
 addi $sp, $sp, −4  slti $t0, $a0, 3  add $v0, s1, $0
 sw $ra, ($sp)  bne $t0, $0, EXIT  lw $ra, ($sp)
 addi $s1, $0, 1  add $s3, $s1, $0  addi $sp, $sp, 4
 addi $s2, $0, 1  add $s1, $s1, $s2  jr $ra
 add $s2, $s3, $0
 addi $a0, $a0, −1
 j LOOP
 2.20.6 Show the content of the stack after each
function call, assuming that the input is 4.
 Solution:
a.
Recursive version
 FIB:  L1:

 addi $sp, $sp, −12  addi $a0, $a0, −1


 jal FIB
 sw $ra, 8($sp)
 addi $s1, $v0, $0
 sw $s1, 4($sp)
 addi $a0, $a0, −1
 sw $a0, 0($sp)
 jal FIB
 HERE:
 add $v0, $v0, $s1
 slti $t0, $a0, 3
 EXIT:
 beq $t0, $0, L1
 lw $a0, 0($sp)
 addi $v0, $0, 1  lw $s1, 4($sp)
 j EXIT  lw $ra, 8($sp)
 addi $sp, $sp, 12
 jr $ra
a.
At label HERE, after calling function FIB with
input of 4:
 old $sp -> 0xnnnnnnnn ???
 −4 contents of register $ra
 −8 contents of register $s1
 $sp -> −12 contents of register $a0
 Solution:
 b.

 Recursive version

 FIB:  L1:
 addi $sp, $sp, −12  addi $a0, $a0, −1
 sw $ra, 8($sp)  jal FIB
 sw $s1, 4($sp)  addi $s1, $v0, $0
 addi $a0, $a0, −1
 sw $a0, 0($sp)
 jal FIB
 HERE:
 add $v0, $v0, $s1
 slti $t0, $a0, 3
 EXIT:
 beq $t0, $0, L1  lw $a0, 0($sp)
 addi $v0, $0, 1  lw $s1, 4($sp)
 j EXIT  lw $ra, 8($sp)
 addi $sp, $sp, 12
 jr $ra
b.
At label HERE, after calling function FIB with
input of 4:
 old $sp -> 0xnnnnnnnn ???
 −4 contents of register $ra
 −8 contents of register $s1
 $sp -> −12 contents of register $a0

You might also like