Question
Question : Write a MIPS assembly language program that a) Prompt the user for an integer in the range of 0 to 50. If the
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".
My code:
.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
.data
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.
la $t0, InputVal # Store the user input in
move $t0 , $v0 # store the value in $t0
beq $t0, $0 , numbersEqual # check if the user input is 0.
la $t1 , upperLim # load upperLim value 50 to t1
slt $t3,$t0,$t1 # check if the number is less than 51.
beq $t3,$zero,greaterThan # if number is > 50 , exit.
numbersEqual:
li $v0 , 4 # system call code for print_str
la $a0 , zeroMessage # address of string to print
syscall # print the string
li $v0 , 10 # system call code for exit
syscall # exit
greaterThan:
li $v0 , 4 # system call code for print_str
la $a0 , incorrectEntry # address of string to print
syscall # print the string
li $v0 , 10 # system call code for exit
syscall # exit
list: .space 200 # Reserve space for 50 integers
listsz: .word 50 # using an array of size 50
mov $t7,1 # t7 has the number to be stored in array.
lw $s0, listsz # $s0 = array dimension
la $s1, list # $s1 = array address
beq $t7, $t0, initDone # exit loop after storing user input integers. t0 has that value.
sw $t7, ($s1) # list[i] = $t0
addi $s1, $s1, 4 # step to next array cell
addi $t7, $t7, 1 # increment the register $t0
b initlp
initDone:
la $t3,listsz # load base addr. of array
mov $t7,1 # here t7 is used as counter
mov $t2,0 # t2 will store the sum.
loop: beq $t7, $t0, printSum # add the numbers from array.
lw $t4,0($s1) # load array[i]
addi $t3,$t3,4 # increment array pointer
add $t2,$t2,$t4 # update sum
addi $t7, $t7, 1 # increment the loop
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
li $v0 , 10
syscall
I can't figure out what the problem of the code is, please solve the issue and post the revised code.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started