Hi All, I have a program here that asks the user to input two floating point num
ID: 3622327 • Letter: H
Question
Hi All,I have a program here that asks the user to input two floating point numbers and then display their sum.
However, I can't seem to get it to work! it always outputs 0.0
Example:
Enter a floating point number: 2
Enter another floating point number: 3
Their sum is: 0.0
PLEASE HELP! IT WOULD BE GREATLY APPRECIATED!
--------------------------------------
.data
vals: .space 4000
askuser1: .asciiz "Enter a floating point number: "
askuser2: .asciiz "Enter another floating point number: "
sum: .asciiz "Their sum is: "
sum2: .asciiz "This: "
newline: .asciiz " "
.text
.globl main
main:
la $a0, askuser1
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
add $s0, $t0, $zero #first integer value -> $s0
la $a0, askuser2
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
add $s1, $t1, $zero #second integer value -> $s1
#Integer implementation of floating-point addition
#Initialize variables
add $s2, $zero, $zero #initialize sum variable to 0
add $t3, $zero, $zero #initialize SUM OF SIGNIFICANDS value to 0
#get EXPONENT from values
sll $s5, $s0, 1 #getting the exponent value
srl $s5, $s5, 24 #$s5 = first value EXPONENT
sll $s6, $s1, 1 #getting the exponent value
srl $s6, $s6, 24 #$s6 = second value EXPONENT
#get SIGN from values
srl $s3,$s0,31 #$s3 = first value SIGN
srl $s4,$s1,31 #$s4 = second value SIGN
#get FRACTION from values
sll $s7,$s0,9
srl $s7,$s0,9 #$s7 = first value FRACTION
sll $t8,$s1,9
srl $t8,$s1,9 #$t8 = second value FRACTION
#compare the exponents of the two numbers
compareExp:
beq $s5,$s6, addSig
blt $s5,$s6, shift1 #if first < second, go to shift1
blt $s6,$s5, shift2 #if second < first, go to shift2
j compareExp
shift1: #shift the smaller number to the right
srl $s7,$s7,1 #shift to the right 1
addi $s5,$s5,1
j compareExp
shift2: #shift the smaller number to the right
srl $t8,$t8,1 #shift to the right 1
addi $s6,$s6,1
j compareExp
addSig:
add $t3,$s7,$t8 #Add the SIGNIFICANDS
result:
sll $t4,$s3,31 #SIGN
#FRACTION
sll $t5,$s6,23 #EXPONENT
add $t6,$t4,$t5
add $t6,$t6,$t3
li $v0, 4
la $a0, sum
syscall
mtc1 $t6,$f0
cvt.s.w $f1, $f0
mov.s $f2, $f1
li $v0,2
syscall
li $v0, 10
syscall
# END OF PROGRAM
------------------------------------- Hi All,
I have a program here that asks the user to input two floating point numbers and then display their sum.
However, I can't seem to get it to work! it always outputs 0.0
Example:
Enter a floating point number: 2
Enter another floating point number: 3
Their sum is: 0.0
PLEASE HELP! IT WOULD BE GREATLY APPRECIATED!
--------------------------------------
.data
vals: .space 4000
askuser1: .asciiz "Enter a floating point number: "
askuser2: .asciiz "Enter another floating point number: "
sum: .asciiz "Their sum is: "
sum2: .asciiz "This: "
newline: .asciiz " "
.text
.globl main
main:
la $a0, askuser1
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
add $s0, $t0, $zero #first integer value -> $s0
la $a0, askuser2
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
add $s1, $t1, $zero #second integer value -> $s1
#Integer implementation of floating-point addition
#Initialize variables
add $s2, $zero, $zero #initialize sum variable to 0
add $t3, $zero, $zero #initialize SUM OF SIGNIFICANDS value to 0
#get EXPONENT from values
sll $s5, $s0, 1 #getting the exponent value
srl $s5, $s5, 24 #$s5 = first value EXPONENT
sll $s6, $s1, 1 #getting the exponent value
srl $s6, $s6, 24 #$s6 = second value EXPONENT
#get SIGN from values
srl $s3,$s0,31 #$s3 = first value SIGN
srl $s4,$s1,31 #$s4 = second value SIGN
#get FRACTION from values
sll $s7,$s0,9
srl $s7,$s0,9 #$s7 = first value FRACTION
sll $t8,$s1,9
srl $t8,$s1,9 #$t8 = second value FRACTION
#compare the exponents of the two numbers
compareExp:
beq $s5,$s6, addSig
blt $s5,$s6, shift1 #if first < second, go to shift1
blt $s6,$s5, shift2 #if second < first, go to shift2
j compareExp
shift1: #shift the smaller number to the right
srl $s7,$s7,1 #shift to the right 1
addi $s5,$s5,1
j compareExp
shift2: #shift the smaller number to the right
srl $t8,$t8,1 #shift to the right 1
addi $s6,$s6,1
j compareExp
addSig:
add $t3,$s7,$t8 #Add the SIGNIFICANDS
result:
sll $t4,$s3,31 #SIGN
#FRACTION
sll $t5,$s6,23 #EXPONENT
add $t6,$t4,$t5
add $t6,$t6,$t3
li $v0, 4
la $a0, sum
syscall
mtc1 $t6,$f0
cvt.s.w $f1, $f0
mov.s $f2, $f1
li $v0,2
syscall
li $v0, 10
syscall
# END OF PROGRAM
-------------------------------------
Explanation / Answer
enter floating point numbers as 2.0 and 3.0