(A) Write the function \"sumAll\" that has three parameters: $a0 holding an addr
ID: 3585817 • Letter: #
Question
(A) Write the function "sumAll" that has three parameters: $a0 holding an address in data memory, $a1 holding the total number of words to sum and $a2 holding the location of the sum in memory. For example, if you wanted to sum 10 integers starting at 0x10010000, $a0 would hold 0x10010000 and $a1 would hold 10. The function is to store the sum of numbers in memory at the location indicated in $a2.
(B) Write the function "printAll" that has two parameters: $a0 holding an address in data memory and $a1 holding the total number of words to print. For example, if you wanted to print 10 integers starting at 0x10010000, $a0 would hold 0x10010000 and $a1 would hold 10. The function is to print the numbers from memory to the screen, one integer per line of output.
(C) Write the function "sumAll" that has two parameters: $a0 holding an address in data memory and $a1 holding the total number of words to sum. For example, if you wanted to sum 10 integers starting at 0x10010000, $a0 would hold 0x10010000 and $a1 would hold 10. The function is to return the sum of the numbers in memory through $v0.
Explanation / Answer
Given below are the answers
Answer A
#---- sumAll function------------------------------------------
sumAll:
move $t0, $a0, #base address
li $t1, 1 #counter
li $t2, 0 #sum
loop1:
bgt $t1, $a1, end_loop1 #if counter is more than n
lw $t3, ($t0) #get current number
add $t2, $t2, $t3 #sum = sum + current number
add $t1, $t1, 1 #increment counter
add $t0, $t0, 4 #increment pointer
b loop1
end_loop1:
sw $t2, ($a2) #store the sum in the address specified in a2
jr $ra
#-- end of sumAll --------------------------------------------
Answer B
#---- printAll function------------------------------------------
printAll:
move $t0, $a0 #base address
li $t1, 1 #counter
loop2:
bgt $t1, $a1, end_loop2
#print the current number
li $v0, 1
lw $a0, ($t0)
syscall
#print newline
li $v0, 4
la $a0, newline
syscall
add $t1, $t1, 1 #increment counter
add $t0, $t0, 4 #increment pointer
b loop2
end_loop2:
jr $ra
#-- end of printAll --------------------------------------------
Answer C
#---- sumAll function------------------------------------------
sumAll:
move $t0, $a0, #base address
li $t1, 1 #counter
li $t2, 0 #sum
loop1:
bgt $t1, $a1, end_loop1 #if counter is more than n
lw $t3, ($t0) #get current number
add $t2, $t2, $t3 #sum = sum + current number
add $t1, $t1, 1 #increment counter
add $t0, $t0, 4 #increment pointer
b loop1
end_loop1:
move $v0, $t2 #store the sum in $v0
jr $ra
#-- end of sumAll --------------------------------------------
==================
A complete program using answer A and B is given below to show the correctness. Hope the answer helped. If it did, please don't forget to rate the answer .Thank you.
.data
arr: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
sum: .word 0
newline: .asciiz " "
msg: .ascii " The sum is "
.text
#set up parameters and call sumAll
la $a0, arr
li $a1, 10
la $a2, sum
jal sumAll
#setup parameters and call printAll
la $a0, arr
li $a1, 10
jal printAll
#display the sum
li $v0, 4
la $a0, msg
syscall
li $v0, 1
lw $a0, sum
syscall
#exit
li $v0, 10
syscall
#---- sumAll function------------------------------------------
sumAll:
move $t0, $a0, #base address
li $t1, 1 #counter
li $t2, 0 #sum
loop1:
bgt $t1, $a1, end_loop1 #if counter is more than n
lw $t3, ($t0) #get current number
add $t2, $t2, $t3 #sum = sum + current number
add $t1, $t1, 1 #increment counter
add $t0, $t0, 4 #increment pointer
b loop1
end_loop1:
sw $t2, ($a2) #store the sum in the address specified in a2
jr $ra
#-- end of sumAll --------------------------------------------
#---- printAll function------------------------------------------
printAll:
move $t0, $a0 #base address
li $t1, 1 #counter
loop2:
bgt $t1, $a1, end_loop2
#print the current number
li $v0, 1
lw $a0, ($t0)
syscall
#print newline
li $v0, 4
la $a0, newline
syscall
add $t1, $t1, 1 #increment counter
add $t0, $t0, 4 #increment pointer
b loop2
end_loop2:
jr $ra
#-- end of printAll --------------------------------------------
output
1
2
3
4
5
6
7
8
9
10
The sum is 55
-- program is finished running --
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.