Question: 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

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 fi x these errors.

a. FACT: addi $sp, $sp, –8 sw $ra, 4($sp)

sw $a0, 0($sp)

slti $t0, $a0, 1 beq $t0, $0, L1 addi $v0, $0, 1 addi $sp, $sp, 8 jr $ra L1: addi $a0, $a0, –1 jal FACT lw $a0, 4($sp)

lw $ra, 0($sp)

addi $sp, $sp, 8 mul $v0, $a0, $v0 jr $ra

b. FACT: addi $sp, $sp, –8 sw $ra, 4($sp)

sw $a0, 0($sp)

slti $t0, $a0, 1 beq $t0, $0, L1 addi $v0, $0, 1 addi $sp, $sp, 8 jr $ra L1: addi $t0, $t0, –1 jal FACT lw $a0, 4($sp)

lw $ra, 0($sp)

addi $sp, $sp, 8 mul $v0, $a0, $v0 jr $ra 2.20.1 [5] <2.8> The MIPS assembly program above computes the factorial 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.

2.20.2 [10] <2.8> For the recursive factorial MIPS program above, assume that the input is 4. Rewrite the factorial program to operate in a nonrecursive 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?
2.20.3 [5] <2.8> Show the contents of the stack after each function call, assuming that the input is 4.
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 fi x these errors.

a. FIB: addi $sp,$sp, –12 sw $ra, 0($sp)
sw $s1, 4($sp)
sw $a0, 8($sp)
slti $t0, $a0, 1 beq $t0, $0, L1 addi $v0,$a0, $0 j EXIT L1: addi $a0,$a0, –1 jal FIB addi $s1,$v0, $0 addi $a0,$a0, –1 jal FIB add $v0, $v0, $s1 EXIT: lw $ra, 0($sp)
lw $a0, 8($sp)
lw $s1, 4($sp)
addi $sp, $sp, 12 jr $ra

b. FIB: addi $sp,$sp, –12 sw $ra, 0($sp)
sw $s1, 4($sp)
sw $a0, 8($sp)
slti $t0, $a0, 1 beq $t0, $0, L1 addi $v0,$a0, $0 j EXIT L1: addi $a0,$a0, –1 jal FIB addi $s1,$v0, $0 addi $a0,$a0, –1 jal FIB add $v0, $v0, $s1 EXIT: lw $ra, 0($sp)
lw $a0, 8($sp)
lw $s1, 4($sp)
addi $sp, $sp, 12 jr $ra 2.20.4 [5] <2.8> 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.
2.20.5 [10] <2.8> For the recursive Fibonacci MIPS program above, assume that the input is 4. Rewrite the Fibonacci program to operate in a nonrecursive 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?
2.20.6 [5] <2.8> Show the contents of the stack after each function call, assuming that the input is 4.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Organization And Design Questions!