Write a MIPS code to compute the sum of N numbers such that each sum skips over
ID: 3667743 • Letter: W
Question
Write a MIPS code to compute the sum of N numbers such that each sum skips over certain numbers. Ask the user for how many numbers to skip (give option of 1-4) and then proceed to compute the sum.
Sum(x1) = N1 + N2 + N3 + …
Sum(x2) = N1 + N3 + N5 + ….
Sum(x3) = N1 + N4 + N7 + ….
Sum(x4) = N1 + N5 + N9 + ….
Sum(x1) determines the sum of all numbers; Sum(x2) determines the sum of numbers skipped by 2, Sum(x3) determines the sum of numbers skipped by 3 and so on. Assume the numbers as given below where N1 = 100, N2 = -7, N3 = 25, N4 = -66 and so on.
________________________________________________________________
.data
.strA: .asciiz “Please enter your choice to skip numbers (1-4) ”
Numbers: .byte 100, -7, 11, 25, -66, 99, -1, 34, 12, 22, -2, -7, 100, 11, 4, 67, 2, -90, 22, 2, 56, 3, -89, 12, -10, 21, 10, -25, -6, 9, 111, 34, 12, 22, -2, -17, 100, 111, -4, 7, 14, -19, -2, 29, 36, 31, -79, 2
.globl main
.text
main: # Commented Program Starts Here
Explanation / Answer
Let Register 1 cntain the sum.
Let Register 2 contain the next number that we need to add
Let Register 3 be the flag which will keep a count till 'n'\1.
1) SUM(X1)
begin:
addi $1, $0, 0 # setting the sum as 0
addi $2, $0, 1 # starting with 1
loop:
slt $3, $4, $2 # checking if number is less than n
bne $3, $0, Ecit if greater than n
add $1, $1, $2 # Keep updating the sum
addi $2, $2, 1 # setting the next NUMBER to add
j loop # Looping the Process
finish: add $2, $1, $0 # Our FInal Result
2) SUM(X2)
begin:
addi $1, $0, 0 # setting the sum as 0
addi $2, $0, 1 # starting with 1
loop:
slt $3, $4, $2 # checking if number is less than n
bne $3, $0, Ecit if greater than n
add $1, $1, $2 # Keep updating the sum
addi $2, $2, 2 # setting the next NUMBER to add
j loop # Looping the Process
finish: add $2, $1, $0 # Our FInal Result
3) SUM(X3)
begin:
addi $1, $0, 0 # setting the sum as 0
addi $2, $0, 1 # starting with 1
loop:
slt $3, $4, $2 # checking if number is less than n
bne $3, $0, Ecit if greater than n
add $1, $1, $2 # Keep updating the sum
addi $2, $2, 3 # setting the next NUMBER to add
j loop # Looping the Process
finish: add $2, $1, $0 # Our FInal Result
4) SUM(X4)
begin:
addi $1, $0, 0 # setting the sum as 0
addi $2, $0, 1 # starting with 1
loop:
slt $3, $4, $2 # checking if number is less than n
bne $3, $0, Ecit if greater than n
add $1, $1, $2 # Keep updating the sum
addi $2, $2, 4 # setting the next NUMBER to add
j loop # Looping the Process
finish: add $2, $1, $0 # Our FInal Result
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.