In: Computer Science
CSE/EEE 230 Assignment 4
Fall 2019
Due Sept 30 (11:59PM)
In this assignment, you are to complete a MIPS program so it will
perform the required tasks. The main function of the code is
provided. Do not change the code in the main function. There is a
loop in the main function.
You are to complete the program by writing two functions. Pay
particular attention to the purpose of each function and how the
parameters and return value are to be used.
1. getinput
$a0 - minimum value
$a1 – maximum value
$v0 – integer input in range defined
This function must prompt the user to input an integer and then get
the input from the user. The process of printing a prompt and then
reading input must be repeated until a value from the minimum to
the maximum values is read (inclusive). The input value is then
returned in $v0.
2. printsum
The function is to the add the two parameters ($a0 and $a1) and
then print the sum.
Sample Input and output:
Enter a number 2 This means that the main will loop 2
times.
Enter a number 7
Enter a number 3
The sum is 10
Enter a number -3
Enter a number -1
Enter a number 25
Enter a number 3
Enter a number 0
Enter a number 6
The sum is 9
General:
• Comment the beginning of your programs with your name and
assignment number.
• Comment every instruction.
• Comment each function. Describe which registers are used in the
function and how they are used.
Submit your .asm file with the complete program.
Copyright 2019 D.Calliss Arizona State University All rights
reserved.
.data
# add any strings at this point as needed
.text
.globl main
main:
addi $a0, $0, 2 # set minimum value as 1
addi $a1, $0, 5 # set maximum value as 15
jal getinput # get a positive number for the loop
addi $s0, $v0, 0 # save input value
repeat:
beq $s0, $0, end # while there are more repeats
addi $a0, $0, 1 # set minimum value as 1
addi $a1, $0, 15 # set maximum value as 15
jal getinput # get a value from 1..15
ori $s1, $v0, 0 # save the result
addi $a0, $0, 3 # set minimum value as 3
addi $a1, $0, 18 # set maximum value as 18
jal getinput # get a value from 4..18
ori $a1, $v0, 0 # set the second parameter for printsum
ori $a0, $s1, 0 # set the first parameter for printsum
jal printsum # call function to print sum of input
addi $s0, $s0, -1 # decrement counter
j repeat # do it again
end: ori $v0, $0, 10 # set command to stop program,
syscall # end program
Screenshot
-------------------------------------------------------------------------------------------------
Program
.data
input: .asciiz "Enter a number "
output: .asciiz "The sum is "
# add any strings at this point as needed
.text
.globl main
main:
addi $a0, $0, 1 # set minimum value as 1
addi $a1, $0, 15 # set maximum value as 15
jal getinput # get a positive number for the
loop
addi $s0, $v0, 0 # save input value
repeat:
beq $s0, $0, end # while there are more repeats
addi $a0, $0, 1 # set minimum value as 1
addi $a1, $0, 15 # set maximum value as 15
jal getinput # get a value from 1..15
ori $s1, $v0, 0 # save the result
addi $a0, $0, 3 # set minimum value as 3
addi $a1, $0, 18 # set maximum value as 18
jal getinput # get a value from 4..18
ori $a1, $v0, 0 # set the second parameter for
printsum
ori $a0, $s1, 0 # set the first parameter for
printsum
jal printsum # call function to print sum of
input
addi $s0, $s0, -1 # decrement counter
j repeat # do it again
end:
ori $v0, $0, 10 # set command to stop program,
syscall # end program
#Function to get input from user
getinput:
#Save arguments min and max temporary
add $t0,$0,$a0
add $t1,$0,$a1
#Loop to check the upper and lower bounds of input
again:
#Display prompt for input
addi $v0,$0,4
la $a0,input
syscall
#Read input
add $v0,$0,5
syscall
#Bounds check
blt $v0,$t0,next
bgt $v0,$t1,next
#Return to main
jr $ra
#If not in the bound ask again
next:
j again
#Function to generate sum
printsum:
#Find sum and store in t0
add $t0,$a0,$a1
#Display output message
add $v0,$0,4
la $a0,output
syscall
#Display sum
add $a0,$0,$t0
addi $v0,$0,1
syscall
#Print next line
addi $a0,$0,10
addi $v0,$0,11
syscall
#Return to main
jr $ra
------------------------------------------------
Output
Enter a number 2
Enter a number 7
Enter a number 3
The sum is 10
Enter a number -3
Enter a number -1
Enter a number 25
Enter a number 3
Enter a number 0
Enter a number 6
The sum is 9