Write a MIPS assembly language program to solve the following problem. For a set
ID: 3601693 • Letter: W
Question
Write a MIPS assembly language program to solve the following problem.
For a set of integers stored in an array, calculate the sum of the positive numbers and the sum of the negative numbers. The program should store both sums in memory variables: posSum and negSum. Numbers should be read from the array one at a time with a zero value (0) being used to signal the end of data (the zero value is acting as a "sentinel" value).
For examle, if your array has the values: 1010 -510 -3010 1510 2010 -110 -2610 -1810 010, then you program should update the posSum and negSum variables to 4510 and -8010, respectively.
For example, your .data section for the array values: 1010 -510 -3010 1510 2010 -110 -2610 -1810 010, will be:
.data
array: .word 10,-5,-30,15,20,-1,-26,-18, 0
posSum: .word 0
negSum: .word 0
.text
.globl main
main:
# MIPS Assembly language program here
li $v0, 10 # system call to exit the program
syscall
Before you start writing MIPS assembly language, write a high-level language algorithm. THEN, translate it to MIPS assembly language.
Explanation / Answer
#-------- OUTPUT ---------#
#--------- CODE AND ALGORITHM --------#
# Code to find positive and negative numbers sum in an array
#Data Declarations
.data
array: .word 10, -5, -30, 15, 20, -1, -26, -18, 0
posSumMsg: .asciiz "Positive Sum : "
negSumMsg: .asciiz " Negative Sum : "
newLine: .asciiz " "
posSum: .word 0 # to store positive numbers sum
negSum: .word 0 # to store negative numbers sum
#--
#text/code section
# Basic Algorithm:
# loop through the array accessing each value
# Check if the element is a negative number ( < 0), jump to the block that stores negative sums
# Check if the element is a positive number ( > 0), jump to the block that stores positive sums
# From with each sum block, jump to the loop iteration block so that same number does not get added to both negative and positive sums
# Inside the loop iteration block, verify if the present element is a 0, terminate the loop, else jump to the loop start again
#--
.text
.globl main
main:
# Loop through the array to calculate sum
la $t0, array # array starting address
li $t1, 0 # loop index, i=0
#lw $t2, length # length
li $t3, 0 # initialize sum=0, for posSum
li $t5, 0 # initialize register for negSum
# this is the main loop where we loop over each array element, till we find a 0 which marks ends of array
sumLoop:
lw $t4, ($t0) # get array[i]
blt $t4, 0, getNegSum # jump to negative sum calculation block if number < 0
bgt $t4, 0, getPosSum # jump to positive sum calculation block if number > 0
# this block calculates sum for positive numbers
getPosSum:
add $t3, $t3, $t4 # sum = sum + array[i]
j loopback # jumo to the block that handles element access
# this block stores the negative sum
getNegSum:
add $t5, $t5, $t4
# this block takes care of the loopback mechanism for array element access
loopback:
add $t1, $t1, 1 # i = i+1
add $t0, $t0, 4 # update array address
bnez $t4, sumLoop # stop the loop if the last element was a 0, else, loop back
# store the sums once all elements have been looped over
sw $t3, posSum # save sum
sw $t5, negSum
# print a message
li $v0, 4 # load appropriate system call code into register $v0;
la $a0, posSumMsg
syscall
# print the value
li $v0, 1
# code for printing integer is 1
move $a0, $t3 # move integer to be printed into $a0: $a0 = $t2
syscall
li $v0, 4 # load appropriate system call code into register $v0;
la $a0, negSumMsg
syscall
li $v0, 1
move $a0, $t5
syscall
li $v0, 10 # call code for terminate
syscall # system call
.end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.