Please calculate the total clock cycles for each function seperately then calculate how much faster would the functions be if a better data cache reduced the average memory operations time to 4 cycles? Code: # Calculating Fibonacci numbers # fib(0) = 0 # fib(1) = 1 # fib(n) = fib(n-1)+fib(n-2) .text .globl main print_str: li $v0, 4 # print string at ($a0) syscall # jr $ra # return; print_eol: la $a0, eol # print "\n" li $v0, 4 # syscall # jr $ra # return; print_int: li $v0, 1 # print integer ($a0) syscall # jr $ra # return; # fib(n) - recursive function to compute nth Fibonacci number # fib: sub $sp,$sp,12 # save registers on stack sw $a0, 0($sp) # save $a0 = n sw $s0, 4($sp) # save $s0 sw $ra, 8($sp) # save $ra to allow recursive calls bgt $a0,1, gen # if n>1 then goto generic case move $v0,$a0 # output = input if n=0 or n=1 j rreg # goto restore registers gen: sub $a0,$a0,1 # param = n-1 jal fib # compute fib(n-1) move $s0,$v0 # save fib(n-1) sub $a0,$a0,1 # set param to n-2 jal fib # and make recursive call add $v0, $v0, $s0 # $v0 = fib(n-2)+fib(n-1) rreg: lw $a0, 0($sp) # restore registers from stack lw $s0, 4($sp) # lw $ra, 8($sp) # add $sp, $sp, 12 # decrease the stack size jr $ra main: la $a0, en # print "n = " jal print_str li $v0, 5 # read integer syscall # move $a0, $v0 # $a0 := $v0 jal fib # call fib(n) move $s0, $v0 # store result in $s0 la $a0, fibstr # print "fib(n) = " jal print_str # move $a0,$s0 # print fib(n) jal print_int # jal print_eol # print "\n" li $v0,10 # exit syscall # .data eol: .asciiz "\n" en: .asciiz "n = " fibstr: .asciiz "fib(n) = "
Please calculate the total clock cycles for each function
seperately then calculate how much faster would the functions be if a better data cache reduced the average memory operations time to 4 cycles?
Code:
# Calculating Fibonacci numbers
# fib(0) = 0
# fib(1) = 1
# fib(n) = fib(n-1)+fib(n-2)
.text
.globl main
print_str:
li $v0, 4 # print string at ($a0)
syscall #
jr $ra # return;
print_eol:
la $a0, eol # print "\n"
li $v0, 4 #
syscall #
jr $ra # return;
print_int:
li $v0, 1 # print integer ($a0)
syscall #
jr $ra # return;
# fib(n) - recursive function to compute nth Fibonacci number
#
fib: sub $sp,$sp,12 # save registers on stack
sw $a0, 0($sp) # save $a0 = n
sw $s0, 4($sp) # save $s0
sw $ra, 8($sp) # save $ra to allow recursive calls
bgt $a0,1, gen # if n>1 then goto generic case
move $v0,$a0 # output = input if n=0 or n=1
j rreg # goto restore registers
gen: sub $a0,$a0,1 # param = n-1
jal fib # compute fib(n-1)
move $s0,$v0 # save fib(n-1)
sub $a0,$a0,1 # set param to n-2
jal fib # and make recursive call
add $v0, $v0, $s0 # $v0 = fib(n-2)+fib(n-1)
rreg: lw $a0, 0($sp) # restore registers from stack
lw $s0, 4($sp) #
lw $ra, 8($sp) #
add $sp, $sp, 12 # decrease the stack size
jr $ra
main:
la $a0, en # print "n = "
jal print_str
li $v0, 5 # read integer
syscall #
move $a0, $v0 # $a0 := $v0
jal fib # call fib(n)
move $s0, $v0 # store result in $s0
la $a0, fibstr # print "fib(n) = "
jal print_str #
move $a0,$s0 # print fib(n)
jal print_int #
jal print_eol # print "\n"
li $v0,10 # exit
syscall #
.data
eol: .asciiz "\n"
en: .asciiz "n = "
fibstr: .asciiz "fib(n) = "
Step by step
Solved in 2 steps with 6 images