Write a MIPS assembly language program that a) Prompt the user for an integer in
ID: 3870556 • 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
# Registers used:
# $v0 - syscall parameter and return value. While making syscall, it will read value in $v0 and act accordingly and also store the return value in it.
# $a0 - syscall parameter-- the string to print. For printing the interger, syscall will read the value in $a0 and for printing string, syscall will read the addressof string stored in $a0.
.data #let processor know we will be submitting data to program now
list: .space 51 # space to accomodate maximum 51 words, each word is of size 4 byte
prompt_user: .asciiz "Please input number in range of 0 to 50 " #string asking user to input N
print_result1: .asciiz "The sum of integers from 0 to " #printing the first part of output
print_result1: .asciiz " is " #printing the second part of output
.text
main:
la $a0, prompt_user #load the address into register a0, of string prompting the user to input number between 0 to 50
li $v0, 4 #loads the value 4 into register $v0 which is the op code for print string
syscall # do the syscall after reading register $v0 for op code, sees 4 and prints the string located in $a0
li $v0, 5 # load syscall read_int into $v0 which is op code for reading an integer
syscall # make the syscall to read integer
move $t0, $v0 # move the number read into $t0.
beq $t0, $r0, stop_program # r0 is a special mips register that always contain 0. If user input zero then it go to stop_program.
li $c0,0 # load value 0 to register c0 which will act as counter
addi $t0,$t0,1 #increment $t0 by 1 in order to iterate till the counter $c0 reaches N.
## initialising array to repective value i.e. ith index will store value i.
la $s1, list # load the address of list in $s1
loop1:
beq $c0,$t0, loop1_end #when the counter reaches value N+1 stored in $t0 then the loop will break and go to loop1_end
sw $c0, ($s1) # store the value of $c0 into address stored in $s1. So, (i)th array element will contain i.
addi $c0,$c0,1 # increment the counter by 1
addi $s1,$s1,4 # increment the array location by 4 byte to store the next item. Since each word take 4 byte
b loop1 # branch to location loop1 i.e. next iteration
loop1_end:
la $s1, list # load the address of list in $s1
li $c0,0 # load value 0 to register c0 which will act as counter
li $t2,0 # register t2 will add the sum result which is being initialized to 0
loop2:
beq $c0,$t0, loop2_end #when the counter reaches value N+1 stored in $t0 then the loop will break and go to loop2_end
la $t1, ($s1) # load the value of address located in $s1 into $t1
add $t2,$t2,$t1 # register t2 will add the sum result
addi $c0,$c0,1 # increment the counter by 1
addi $s1,$s1,4 # increment the array location by 4 byte to store the next word in array
b loop2 # branch to location loop1 i.e. next iteration
loop2_end:
la $a0, print_result1 #load the address of string i.e. print_result1 for printing "The sum of integers from 0 to "
li $v0, 4 #loads the value 4 into register $v0 which is the op code for print string
syscall # do the syscall after reading register $v0 for op code, sees 4 and prints the string located in $a0
move $a0, $t0 # move the number N to print into $a0.
li $v0, 1 # load syscall print_int into $v0. This will print value "N"
syscall
la $a0, print_result2 #load the address of string i.e. print_result2 for printing " is " .
li $v0, 4 ##loads the value 4 into register $v0 which is the op code for print string
syscall # do the syscall after reading register $v0 for op code, sees 4 and prints the string located in $a0
move $a0, $t2 # move the summation result stored in $t2 to print into $a0.
li $v0, 1 # load syscall print_int into $v0. This will print value of summation result
syscall
stop_program:
li $v0, 10 # syscall code 10 is for exit.
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.