Summing up two floats always returns 0.0 in MIPS - mips

I'm writing my very first program in assembly, I just have to sum up 2.5 and 2.5
Here's my code
.data
#2.5+2.5=5.0
myNumber1: .float 2.5
myNumber2: .float 2.5
.text
lwc1 $f1, myNumber1
lwc1 $f2, myNumber2
add.s $f3, $f1, $f2
li $v0, 3
syscall
For some reason it always returns 0.0 in the output, but in the memory it seems to sum and store the value in $f3 correctly. What am I missing here?

According to this page the syscall to print a float is 2 not 3. Also, you must have the float to print in register $f12.
Accordingly, this should work:
add.s $f12, $f1, $f2
li $v0, 2
syscall

Related

Mips assembly language on MARS

I'm trying to create a program that will take a FP number (5.2) and output in increments of 1.0 until the highest value (20.2) is reached.
So the smallest value is 5.2 and the biggest is 20.2 there should be a loop.
The output should be:
5.2 6.2 7.2 and so on until 20.2
Try this code :
.data
spaces: .asciiz " "
fp1: .double 5.2
fp2: .double 20.2
fp3: .double 1.0
.text
main:
l.d $f0,fp1 # $f0 register contains 5.2
l.d $f2,fp2 # $f2 register contains 20.2
l.d $f4,fp3 # $f4 register contains 1.0
loop:
li $v0, 3
mov.d $f12,$f0
syscall # syscall print floating point number
li $v0 , 4
la $a0 , spaces
syscall # syscall print space
c.eq.d $f0,$f2 # if $f0 == $f2 jump to EXIT
bc1t EXIT
add.d $f0,$f0,$f4 # else $f0 = $f0+$f4
j loop
EXIT:
li $a0, 0
li $v0, 17 #exit
syscall

QTSPIM telling me c.gt.s $f0, $f1 has syntax error (pointing to .s)

This is my code, and at c.gt.s $f0, $f1 there is a syntax error pointing to the s, but it's a single precision floating point number so it needs to be c.gt.s. What am I doing wrong here?
.data
numA: .float 10.5
numB: .float 12.3
.text
.globl main
main:
la $t0, numA
la $t1, numB
lwc1 $f0, 0($t0)
lwc1 $f1, 0($t1)
c.gt.s $f0, $f1
bc1t swap
li $v0, 10
syscall
swap:
swc1 $f0, 0($f3) #f3 is temp
swc1 $f1, 0($f0)
swc1 $f3, 0($f0)
jr $ra
I found a workaround. Not sure why c.gt.s didnt work, but c.lt.s does and lwc1 and swc1 had to be swapped with l.s and mov.s, respectively.
.data
numA: .float 12.5
numB: .float 10.3
.text
main:
l.s $f0, numA
l.s $f1, numB
c.lt.s $f1, $f0
bc1t swap
li $v0, 10
syscall
swap:
mov.s $f3, $f0 #f3 is temp
mov.s $f0 $f1
mov.s $f1, $f3
li $v0, 10
syscall

There is something wrong with the output of this program MIPS(double)

I am writing a program in MIPS to convert inches into centimeters, but the result is always evaluating to zero. I don't know where I did wrong. I have written the program below. It's compiling, but not evaluating the correct result, always giving 0.
#declaring some things
.data
inchesText: .asciiz "Enter the number in inches: "
resultText: .asciiz " Centimeters are ==> "
inches: .double 0
inchesToCenti: .double 2.54
centi: .double 0
zero: .word 0
result: .double 0
.text
main:
jal getInches
jal inches_To_Centi
jal finalResult
jal Exit
getInches:
# printing string
la $a0,inchesText
li $v0, 4
syscall
# get inches
li $v0, 7
syscall
s.d $f2, inches
jr $ra
inches_To_Centi:
# loading the formula contstant as it is
l.d $f0, inchesToCenti
#actual inches gained through argument
l.d $f2, inches
# mul both of these to get the centimeters
mul.d $f6, $f0, $f2
s.d $f6, centi
jr $ra
finalResult:
# printing text
la $a0, resultText
li $v0, 4
syscall
# now printing the actual value
l.d $f12, centi
li $v0, 3
syscall
Exit:
li $v0, 10
syscall
Is it a lot of time I do not do assemble but I think the solution of your problem is all about the system call you use to read the double value.
The syscall 7 does not store the input value in the $f2 register but into $f0 one.
Change the line #26 to
s.d $f0 inches
To give a little more context, as the line number are not present, the getInches subroutine needs the fix:
getInches:
# printing string
la $a0,inchesText
li $v0, 4
syscall
# get inches
li $v0, 7
syscall
s.d $f0, inches
jr $ra

A mess up with printing text in mips

I'm writing a code that print through a loop "Enter a number" 20 times, and it happens that the text that I want to print after "The Average is" gets messed up like this "ó???ff¦?". Here's the code:
.data
array: .space 20
outputA: .asciiz "The Average is:\n" #prints the average
input: .asciiz "Enter a number:\n" #prints the statement
avgNum: .float 0.0
lengthFloat: .float 3.0
const: .float 0.0 #I did this because you can't use li for float numbers
zero: .float 0.0
one: .float 1.0
.text
la $t9, outputA
lwc1 $f2, lengthFloat
li $t0, 0 #counter i
li $s2, 0 #counter j
la $s1, array #base address of the array
la $k1, input #Displaying the message (The else part)
li $t5, 0 #currentCount for mode
l.s $f3, const #mode value
li $t6, 0 #count for mode
l.s $f14, zero #Just a 0
l.s $f16, one
loop:
slti $s0, $t0, 20 #Checking if the counter is less than 20
beq $s0, $zero, print #if it's greater or equal to 20 exit the loop
move $a0, $k1
li $v0, 4
syscall
li $v0, 6 #Reading a float number
syscall
sll $t1, $t0, 2 #Storing the value in the appropriate place in memory
add $t1, $s1, $t1
swc1 $f0, 0($t1)
add.s $f1, $f1, $f0
addi $t0, $t0, 1 #Adding the counter 1
j loop
print:
#printing avg
move $a0, $t9
li $v0, 4
syscall
li $v0, 2
div.s $f12, $f1, $f2
syscall
mov.s $f10, $f12 #Storig the value of average for later use
A single-precision floating-point number is 4 bytes, so 20 floats require 80 bytes of space. You've only reserved 20 bytes of space, so you'll end up overwriting something else. You'll have to change the declaration of array to array: .space 20*4
Also, you're dividing the sum by 3.0, so you're not calculating the average since that would require dividing by 20.0.

MIPS Floating point addittion troubleshooting

So I'm trying to write a program in MIPS assembly code to help me better understand how floating point addition works. I've written a program that gets two inputs from a user, and adds them WITHOUT using any floating point instructions except mtc1 and mfc1 (for input and output). My code has bugs because when I add 1 + 2 I get 2.74804688. I'm still trying to debug the code but can't seem to grasp the problem. If anyone can help, I would greatly appreciate it.
THIS IS MY CODE (excluding the user input...the first floating point value is in $s0, and the second in $s1)
#Integer implementation of floating-point addition
#Initialize variables
add $s0,$t0,$zero #first integer value
add $s1,$t1,$zero #second integer value
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 $s0,$s0,1 #shift to the right 1
#j compareExp
srl $t8,$t8,1 #shift to the right 1
addi $s6,$s6,1
j compareExp
addSig:
add $t3,$s7,$t8 #Add the SIGNIFICANDS
li $v0, 4
la $a0, sum
syscall
li $v0, 1
move $a0, $t3
syscall
j result
result:
li $v0, 4
la $a0, newline
syscall
sll $t4,$s3,31 #SIGN
#FRACTION
sll $t5,$s6,23 #EXPONENT
add $t6,$t4,$t5
add $t6,$t6,$t3
li $v0, 4
la $a0, sum2
syscall
li $v0, 1
move $a0, $t6
syscall
li $v0, 4
la $a0, newline
syscall
li $v0, 4
la $a0, sum2
syscall
li $v0,2
mtc1 $t6,$f12
syscall
jr $31
# END OF PROGRAM
You are using MARS syscall 5 to read the inputs, which reads them as integers. You should be reading them as floating point numbers.
You are not including the IEEE 754 hidden bit when you extract the fractions.
You are not handling the signs of the inputs correctly.
There's a lot more, but that's enough for now.