Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MIPS assembly language program that a) Prompt the user for an integer in

ID: 3888918 • Letter: W

Question

Write a MIPS assembly language program that

a) Prompt the user for an integer in the range of 0 to 50. If the user inputs 0 the program stops.

b) Otherwise, the program stores the numbers from 0 up to the input value into an array of words in memory, i.e. initializes the array with values from 0 up to N where N is the value that user has inputted.

c) The program then adds the value of all items of the array together (up to N) by loading them from the main memory then add them up, then prints out the sum with the message "The sum of integers from 0 to N is:". For example, if the user gave 5 as the input the program prints out "The sum of integers from 0 to 5 is 15".

Explanation / Answer

solution:

.data

userPrompt : .asciiz "Enter an Integer from 0 to 50 "
zeroMessage : .asciiz " Entered number is 0 , the program will close "
incorrectEntry : .asciiz " Entered number is greater than 50, which is incorrect, program will close"
sumMessage: .asciiz "The sum of integers from 0 to N is: "
InputVal : .word 0
upperLim : .word 50
list: .word 50 # using an array of size 50


.text

li $v0, 4 # system call code for print_str
la $a0, userPrompt # address of string to print
syscall # print the string

li $v0, 5 # Read the user input.
syscall # user value will be in vo.
move $t0 , $v0 # store the value in $t0


beq $t0, $zero , zeroInput # check if the user input is 0.
lw $t1 , upperLim # load upperLim value 50 to t1
bgt $t0, $t1,greaterThan # if number is > 50 , exit.
b valid


zeroInput:
li $v0 , 4 # system call code for print_str
la $a0 , zeroMessage # address of string to print
syscall # print the string
b exit

greaterThan:
li $v0 , 4 # system call code for print_str
la $a0 , incorrectEntry # address of string to print
syscall # print the string
b exit


valid:
addi $t7, $zero, 1 # t7 has the number to be stored in array.
la $s1, list # $s1 = array address


initlp:
bgt $t7, $t0, initDone # exit loop after storing user input integers. t0 has that value.
sw $t7, ($s1) # list[i] = $t7
addi $s1, $s1, 4 # step to next array cell
addi $t7, $t7, 1 # increment the register $t7 to next number
b initlp


initDone:
la $s1,list # load base addr. of array
addi $t7, $zero, 1 # here t7 is used as counter
move $t2, $zero # t2 will store the sum.


loop:
bgt $t7, $t0, printSum # add the numbers from array.
lw $t4,0($s1) # load array[i]
addi $s1,$s1,4 # increment array pointer
add $t2,$t2,$t4 # update sum
addi $t7, $t7, 1 # increment the loop counter
b loop

printSum:
# print sum message
li $v0,4
la $a0,sumMessage
syscall
# print value of sum
li $v0,1
addi $a0,$t2,0
syscall


exit:
# exit
li $v0 , 10
syscall

output

Enter an Integer from 0 to 50 5
The sum of integers from 0 to N is: 15
-- program is finished running --

Enter an Integer from 0 to 50 10
The sum of integers from 0 to N is: 55
-- program is finished running --

Enter an Integer from 0 to 50 0
Entered number is 0 , the program will close
-- program is finished running --

Enter an Integer from 0 to 50 60
Entered number is greater than 50, which is incorrect, program will close
-- program is finished running --

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote