Write assembly code (MIPS PROGRAMMING) and create a program that has a while-loo
ID: 3630983 • Letter: W
Question
Write assembly code (MIPS PROGRAMMING) and create a program that has a while-loop to add only the even-indexed integers of this 7 element array. |17|11|24|13|14|52|33|So it should print out 17+24+14+33. You should use 'Shift Left Logical'.
Then take that same array, and store in the even-indexed elements the sum of your answers. So your final array should have the following values:
|17|11|41|13|55|52|88|. NOTE: You get 41 by adding 17+24. Then add 14 to get 55. Then add 33 to get 88.. You should print the final array on the console.
Explanation / Answer
.text.globl main
main:
la $a0,number_prompt ##printing message to ask user to enter an integers
li $v0,4
syscall
li $t0,0 #initialise loop varaible to 0
li $t1,6
li $t2,2
li $s1,0 ##initialise a variable to hold sum to 0
li $s2,0 ##initialise a variable to hold evensum to 0
subi $sp,$sp,28
li 17,24($sp)
li 11,20($sp)
li 24,16($sp)
li 13,12($sp)
li 14,8($sp)
li 52,4($sp)
li 33,0($sp)
addi $sp,$sp,28
while:
beq $t0,$t1,end_loop #if index is 6exit loop
rem $s0,$t0,$t2 ##find remainder by dividing by 2 and put in $s0
beqz $s0,evensum ##if remainder is 0 it means index is even. so branch toevensum
ret: ##label just for convience to comback from label even sum
add $s1,$v0,$s1 ##update sum by adding read value
addi $t0,1 ##loop increment i.e increase the index
b while
end_loop:
li $t0,0 #initialise loop varaible to 0
printing:
beq $t0,$t1,end_print #if index is 6exit loop
lw $a0,-24($sp);
sub $sp,$sp,4
addi $t0,1 ##loop increment i.e increase the index
b printingend_print:
li $v0,10 ##exit from the program
syscall
evensum:
add $s2,$s2,$v0 ##update ven sum with the current value
sw $s2,0($sp) ##store that even sum in the even index
b ret ##go back to the loop
##P.S : The integer reading thing is fixed :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.