5. (50) In part 5 you must make an assembly program (start with Template-AddArra
ID: 3735910 • Letter: 5
Question
5. (50) In part 5 you must make an assembly program (start with Template-AddArray.s ) to add the contents of an Array and print out the total value and the current element you just read. E.g. “The current element is:” and “The total value of the array is: XX”. Name your new file Lastname-AddArray.s Make sure you comment your code so I (and more so you) understand what you are doing.
For printing each element, use the jal command to jump to a section of the code for printing (don’t worry about saving specific registers for the call – just use any current registers for the reading the appropriate values).
Your program should be similar to the following C-code:
i=0;
while (i<N)
{
sum = sum + A[i];
i++;
}
Construct the loop via slt and a conditional branch. Assume you only have 5 elements and that your array holds the following C-equivalent.
int A[] = 1, 2, 3, 4, 5;
Explanation / Answer
.data
arr: .word 1, 2, 3, 4, 5
.text
.globl main
main:
# Add the array elements
add $x, $zero, $zero
add $sum, $zero, $zero
la $z, arr
loop:
slti $t, $x, 5 #i<10
beq $t, $zero, EXIT
lw $A, ($z) #$A = arr[i]
addi $z, $z, 4
add $sum, $sum, $A #sum = sum + A[i]
addi $x, $x, 1 #incrementing i as i++
j loop
print_result:
#moving sum to $a0
add $a0, $zero, $sum
li $v0, 1
syscall
li $v0, 10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.