Write this code in MIPS please!! This program must prompt the user to input an i
ID: 3753971 • Letter: W
Question
Write this code in MIPS please!!
This program must prompt the user to input an integer value between 1 and 5 inclusive. If the user input an integer greater than 5 or less than 1, the program must prompt an error message and ask user to try again. This process repeated until the user enters a valid integer, say n. Then, the program must compute the sum of the cube of the numbers from 1 to n and display the result on the console.
For example, when the input integer is 3, the result is 36 (which is 13 + 23+33).
The messages showing on the console should be:
Please input the integer: 6
!!!Invalid, try again
Please input the integer: 3
The result is: 36
Explanation / Answer
PROGRAM CODE:
prompt: .asciiz " Please input a number: "
errMsg: .asciiz "Invalid! Please input a number between 1 and 5. "
outMsg: .asciiz "The result is: "
.text
input:
#prompt and read int
li $v0, 4
la $a0, prompt
syscall
#read int and store in $t0
li $v0, 5
syscall
move $t0, $v0
blt $t0, 1, error
bgt $t0, 5, error
li $t1, 1 #counter
li $t2, 0 #sum
loop:
bgt $t1, $t0, output
mul $t3, $t1, $t1 # t3 = t1 * t1
mul $t3, $t3, $t1 # t3 = t3 * t1
add $t2, $t2, $t3 #add the cube to sum
add $t1, $t1, 1 #increment counter
b loop
error:
li $v0, 4
la $a0, errMsg
syscall
b input
output:
li $v0, 4
la $a0, outMsg
syscall
li $v0, 1
move $a0, $t2
syscall
#exit
li $v0, 10
syscall
OUTPUT:
-----
Please input a number: 6
Invalid! Please input a number between 1 and 5.
Please input a number: 3
The result is: 36
-- program is finished running --
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.