I need to write a MIPS code to compute the sum of N numbers such that each sum s
ID: 670401 • Letter: I
Question
I need to write a MIPS code to compute the sum of N numbers such that each sum skips over certain numbers, asking the user to input how many numbers to skip (from 1 to 4), and then compute the sum based on that.
___________________________________________________
.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
___________________________________________________
From what I understand, I'll just need to add by different amounts of bytes each time depending on if they enter 1, 2, 3, or 4. What I'm not sure of is how to track when it finishes going through 'Numbers' and outputs what the final sum is. I'm thinking a counter would work, though I'm not sure how to start implementing one for each of the 4 cases.
Any guidance at all would be much appreciated!
Explanation / Answer
# reserve space for data and variables after .data directive
.data
# string contants – terminated will a null character (i.e. all Os)
title: .asciiz "Program computes the sum of N integers"
prompt: .asciiz "Input an integer? "
strA : .asciiz “ Please enter your choice to skip numbers (1-4): ”
o_title: .asciiz "The answer is: "
#input numbers
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
# ASCII string with carriage return and line feed (a newline)
newline: .byte 10,13,00,00
# force address to next word boundary after putting bytes in memory
.align 2
# integer variables
n: .word 0
sum: .word 0
jumptable: .word top, case1, case2, case3
#instructions appear after .text directive
.text
main:
# Print out program description
# puts title
li $v0,4
la $a0,title
syscall
# Print out carriage return and line feed
# putc ' '
li $v0,4
la $a0,newline
syscall
# Print out carriage return and line feed
# putc ' '
li $v0,4
la $a0,newline
syscall
# Prompt user for input
# puts prompt
li $v0,4
la $a0,prompt
syscall
# Read in an integer, N
# geti $a0
li $v0,5
syscall
add $a0,$v0,$zero
#code to read the skip count
li $v0, 4 # Code to print a string
la $a1, strA
syscall
li $v0, 5 # Code to read an integer
syscall
add $t1, $v0, $zero # place the value on t1
la $a1, jumptable # Load address of jumptable
sll $t0, $v0, 2 # Compute word offset
add $t1, $a1, $t0 #Form a pointer into jumptable
lw $t2, 0($t1) # Load an address from jumptable
jr $t2 # Jump to specific case “switch”
case1: addi Sa0,$t2,-5 # skip last 5 integers (count-=skipcount)
b default
case2: addi Sa0,$t2,-10 # skip last 10 integers
b default
case3: addi Sa0,$t2,-15 # skip last 15 integers
b default
case4: addi Sa0,$t2,-20 # skip last 20 integers
default:
# Program to compute Sum
xor $a1,$a1,$a1 # count = 0;
xor $a2,$a2,$a2 # sum = 0;
loop: add $a2,$a2,$a1 # loop: sum = sum + count
addi $a1,$a1,Numbers # count = count + 1
# here change the instruction to read the numbers given in input
ble $a1,$a0,loop # if count <= N then goto loop
# save result (some registers will be destroyed by puts call)
sw $a2,sum
# Print out carriage return and line feed
# putc ' '
li $v0,4
la $a0,newline
syscall
# Print out output title string
# puts o_title
li $v0,4
la $a0,o_title
syscall
# Load result
lw $a0,sum
# Print out result
# puti $a0
` li $v0,1
syscall
# Print out carriage return and line feed
# putc ' '
li $v0,4
la $a0,newline
syscall
# End program and return to simulator
# done
li $v0,10
syscall