Review the following code. State which of the functions breaks the register convention rules and what would
Question:
Review the following code. State which of the functions breaks the register convention rules and what would happen if the code was run as is. How would one fix this code, this can be done by stating the parts that need to be fixed or simply rewriting the code.
...
# foo is a function that when called, it
# 1. loops through an array
# 2. applies the function bar on each value in the array
#
# Parameters:
# a0 holds the address of the array
# a1 holds the number of elements in array a0
foo:
addi sp, sp, -12 # adjust sp
sw ra, 0(sp) # save ra in stack
mv s0, a0 # s0 <- arr
add s1, zero, zero # s1 <- counter
L1:
addi s1, s1, 1 # increase the counter
lw a0, 0(a0) # load word into a0
jal ra, bar # call bar
sw a0, 0(s0) # store the updated value back into the array
addi a0, a0, 4 # go to the next element in the array
bne s1, a1, L1 # check if the end of array is reached
lw ra, 0(sp) # restore ra back from stack
addi sp, sp, 12 # adjust sp
jalr zero, ra, 0
# bar is a function that
# 1. squares the value of a0 and
# 2. adds 31 to it
#
# Parameters:
# a0 holds the value to be modified
bar:
mul a0, a0, a0 # a0 <- a0 * a0
addi a0, a0, 31 # a0 <- a0 + 31
jalr zero, ra, 0