In: Computer Science
There are two programming questions for you to do. Please submit two.s or .asm files. You can use the usual settings as the previous homework. But if you want to use pseudo-instructions, you will need to uncheck "Bare Machine" and check "Accept Pseudo Instructions"
(10 points) Declare an array of integers, something like: . data size : . word 8 array : . word 23 , -12 , 45 , -32 , 52 , -72 , 8 , 13 Write a program that computes the sum of all the odd integers in the array. Put the sum in register $1
Given below is the code for the question. I have given 2
screenshots of before and after execution to show that the answer
is stored in $1. (There will not be any output printed on
screen)
Please do rate the answer if it helped. Thank you.
.data
size: .word 8
array: .word 23 , -12 , 45 , -32 , 52 , -72 , 8 , 13
.text
lw $t0, size
li $t1, 0
li $t5, 0 #sum
loop:
bge $t1, $t0, end_loop
#calc offset and get num at offset
mul $t2, $t1, 4
lw $t3, array($t2)
#check if odd by dividing by 2
div $t4, $t3, 2
mfhi $t4 #get remainder
beq $t4, 0 , next #even
add $t5, $t5, $t3
next:
add $t1, $t1, 1
b loop
end_loop:
move $1, $t5
#exit
li $v0, 10
syscall