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

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

Related

Assembly MIPS: Cannot debug floating-point power function

I have been trying to implement a floating-point function in Assembly MIPS 32-bits, and I have not been able to pinpoint the exact fault. It seems to be in the mul.s $fd, $fsrc1, $fsrc2 command, since the contents of the specific floating-point register remain unchanged (I have checked this with syscalls as well, printing the contents in each case) but I seem to be unable to find the fix. Anyone got any ideas? Full code below:
.align 2
.data
number: .float 0.5
.text
.globl main
main:
li $a0, 3
l.s $f12, number
jal power
li $v0, 2
mtc1 $a0, $f0
syscall
li $v0, 17
syscall
power:
#Build stack
subu $sp,$sp,56 # Stack frame is 56 bytes long
sw $a0, 12($sp) # Save Argument 0 ($a0)
swc1 $f12, 8($sp) # Save Argument 1 ($a1) (integer arg)
sw $s0, 56($sp) # Save $s0
sw $s1, 52($sp) # Save $s1
swc1 $f20, 48($sp) # Save $f20
swc1 $f21, 44($sp) # Save $f21
sw $ra, 16($sp) # Save return address
sw $fp, 20($sp) # Save frame pointer
addiu $fp, $sp, 52
#-----------------------------------------------------------------------#
move $s0, $a0 #stores value of n
mov.s $f22, $f12 #stores value of x
li $s1, 0 #initialize counter with 0
powerLoop:
mul.s $f20, $f20, $f20 #y=y*y
addi $s1, $s1, 1 #increase counter
bgtu $s0, $s1, powerLoop #continue loop as long as counter<n
mov.s $f0, $f20 #return the results
#-----------------------------------------------------------------------#
#Pop stack
lw $a0, 12($sp) #Load back $a0
lwc1 $f12, 8($sp) #Load back $f12
lw $s0, 56($sp) # Load $s0
lw $s1, 52($sp) # Load $s1
lwc1 $f20, 48($sp) # Load $f20
lwc1 $f21, 44($sp) # Load $f21
lw $ra, 16($sp) # Restore old value of $ra
lw $fp, 20($sp) # Restore old value of $fp
addiu $sp, $sp, 56 # Pop stack
jr $ra
SOLVED:
Despite some errors in registers, the main error is in the
li $v0, 2
mtc1 $a0, $f0
syscall
FIX:
li $v0, 2
mov.s $f12, $f0
syscall
REASON: $f12 IS ALWAYS, due to convention, the floating-point argument, even for syscalls. It will not print the contents of $a0 but rather $f12!

MIPS32, Using the Stack for recursive multiplication, there is no output

So my test case is 7 for the multiplicand and 9 for the multiplier, I am trying to use Recursive Multiplication by using repeated addition, recursively. I'm a beginner in stacks in regards to MIPS so what could be the reason why nothing is wanting to output?
I am required to use $a0 -> Multiplicand, $a1 -> Multiplier, and $v0 -> Product
.text
main:
#Printing out prompt1
li $v0, 4
la $a0, prompt1
syscall
#Getting user input -> Multiplicand
li $v0, 5
syscall
move $a0, $v0
#Printing out prompt2
li $v0, 4
la $a0, prompt2
syscall
#Getting user input -> Multiplier
li $v0, 5
syscall
move $a1, $v0
#Loop
mult:
addiu $sp,$sp, -8
sw $a0, 4($sp)
sw $ra, 0($sp)
bne $a0, 1, else
add $v0, $v0, 0
j exit
else:
sub $a1, $a1, 1
jal mult
exit:
lw $a0, 4($sp)
add $v0, $v0, $a0
sw $v0, 4($sp)
addiu $sp, $sp, 8
jr $ra
lw $v0, multiplicand
li $v0, 1
la $a0, multiplicand
syscall
#Ending Program
li $v0, 10
syscall
.data
prompt1: .asciiz "Enter the multiplicand: "
prompt2: .asciiz "Enter the multiplier: "
multiplicand: .space 101

Swap procedure not working in MIPS

The objective of this program is to swap two numbers without using temporary variables. I came up with a solution by just using math but when I assemble, the numbers don't swap and I can't figure out why. Can anyone explain my mistake?
.text
main:
la $a0,n1
la $a1,n2
jal swap
li $v0,1 # print n1 and n2; should be 27 and 14
lw $a0,n1
syscall
li $v0,11
li $a0,' '
syscall
li $v0,1
lw $a0,n2
syscall
li $v0,11
li $a0,'\n'
syscall
li $v0,10 # exit
syscall
swap: #this is my code and where I'm having problems
sub $a0, $a0, $a1
add $a1, $a1, $a0
sub $a0, $a1, $a0
jr $ra
.data
n1: .word 14
n2: .word 27

MIPS - Division of 3 floats

One of my first MIPS tasks.
I tried a lot and read a few tutorials but I`m unable to find the failure.
(3.0 + 3.0 + 3.0) 'div' 3.0 gives the result 0.0
Hope you can help me to find my mistake.
.text
.globl main
main:
##Enter first float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
move $s0,$v0
mtc1 $s0,$f0
##Enter second float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
move $s1,$v0
mtc1 $s1,$f1
##Enter third float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
move $s2,$v0
mtc1 $s2,$f2
##Calculate"((f0+f1+f2) div by 3.0)"
l.s $f3, fp1
add.s $f0, $f0, $f1
add.s $f0, $f0, $f2
div.s $f0, $f0, $f3
mfc1 $s0, $f0
la $a0,prompt2
li $v0,4
syscall
move $a0, $s0
li $v0,2
syscall
.data
fp1: .float 3.0
prompt1: .asciiz "Enter a float: "
prompt2: .asciiz "The result is: "
Thank you!
You're using the syscalls incorrectly.
The description for syscall 6 (read float) says: "Result $f0 contains float read".
So after each syscall 6 you can do mov.s $fn,$f0 (where $fn is $f1, $f2, etc) to copy the float that has been read into one of the other floating-point registers.
The description for syscall 2 (print float) says: "Arguments $f12 = float to print".
So to print the result of the division you could simply place the result in $f12 (i.e. div.s $f12, $f0, $f3) and then use syscall 2.
Thank you very much!
I kept trying and with the help of your hints I finally got it.
Here is the solution (if anyone has similar problems)
.text
.globl main
main:
##Enter first float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
mov.s $f1, $f0
##Enter second float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
mov.s $f2, $f0
##Enter third float:
la $a0,prompt1
li $v0,4
syscall
li $v0,6
syscall
mov.s $f3, $f0
##Calculate"((f0+f1+f2) div by 3.0)"
l.s $f4, fp1
add.s $f1, $f1, $f2
add.s $f1, $f1, $f3
div.s $f12, $f1, $f4
la $a0,prompt2
li $v0,4
syscall
mfc1 $a0, $f12
la $v0,2
syscall
.data
fp1: .float 3.0
prompt1: .asciiz "Enter a float: "
prompt2: .asciiz "The result is: "

swap mips variables not being saved

.text
main:
la $a0,n1
la $a1,n2
jal swap
li $v0,1 # print n1 and n2; should be 27 and 14
lw $a0,n1
syscall
li $v0,11
li $a0,' '
syscall
li $v0,1
lw $a0,n2
syscall
li $v0,11
li $a0,'\n'
syscall
li $v0,10 # exit
syscall
swap:
xor $a0 $a0 $a1
xor $a1 $a0 $a1
xor $a0 $a0 $a1
jr $ra
L1:
.data
n1: .word 14
n2: .word 27
ok so my goal was to add the goal to swap
however, no matter what I do with the variables inside swap it doesn't seem to be saved
i am editing a0 and a1, shouldn't that work?
the code always prints 14 and 27