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

Write a program in assembly that calculates the sum of all the numbers of a floa

ID: 3599895 • Letter: W

Question

Write a program in assembly that calculates the sum of all the numbers of a floating point array that are smaller than the user input. Assume you defined an array with 8 floating point elements in your program and you ask the user for a real number. The result will be shown in the output with an appropriate message. If there are no smaller numbers than the user input in your array, provide an appropriate message Use array A 11.355 2.670 3.566 4.560 5.980 7.665 12.340 18.540 1 as the floating point array For example Please give a real number: 16.750 The answer is 38.136

Explanation / Answer

####################### MIPS/ MARS 4.5 Assembly Program ####################################

.data

array: .float 1.355, 2.670, 3.566, 4.560, 5.980, 7.665, 12.340, 18.540 # initializing given array 'A' as float values

msg1: .asciiz "Please give a real number: " # message to user

msg2: .asciiz "The answer is: " # message to user

msg3: .asciiz "Sorry there are no smaller numbers in the array." # message to user

input_num: .float 0 # used to store the real number read from the stdin

sum: .float 0 # used to store the result

.text

li $v0, 4 # system call to print the "msg1" on stdout

la $a0, msg1

syscall

li $v0, 6 # system call to read real number from stdin(keyboard)

syscall

swc1 $f0, input_num # storing the input value at location "input_num"

li $t1, 0 # loop variable, used for repeat the loop for 8-times

find_sum: # loop for checking and finding the sum

lwc1 $f2, array($t1) # $f2 = float at '$t1'(index) element of array

c.lt.s $f0, $f2 # comparing input number with $t1(index) element of array

bc1t next_number # going for next indexed element if present number is not less than input number

add.s $f4, $f4, $f2 # cumulative addition of all the array elements which are less than input number

next_number:

addi $t1, $t1, 4 # for moving the next index of array

bne $t1, 40, find_sum # repeating the loop for 8-times

swc1 $f4, sum # storing the result at location "sum"

c.eq.s $f4, $f6 # comparing if there are no smaller elements present in array.

# checking if $f4 = $f6, i.e., sum = 0(initially $f6 contains zero)

bc1f print_sum # if sum is not zero then printing the sum

# if sum is zero, then displaying error message and exiting the program

li $v0, 4 # displaying error message on stdout

la $a0, msg3

syscall

li $v0, 10 # exit the program

syscall

print_sum:

li $v0, 4 # displaying the message on stdout

la $a0, msg2

syscall

li $v0, 2 # printing the result on stdout

mov.s $f12, $f4 # $f12 = result(sum of all smallest numbers than input number)

syscall

li $v0, 10 # exit the program

syscall

####################################### OUTPUT ###########################################

# Please give a real number:

# 16.750

# The answer is:

# 38.136

# -- program is finished running --

#

#

# Reset: reset completed.

#

# Please give a real number:

# 1.004

# Sorry there are no smaller numbers in the array.

# -- 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