Home > Back-end >  Assembly MIPS: Cannot debug floating-point power function
Assembly MIPS: Cannot debug floating-point power function

Time:09-27

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

CodePudding user response:

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!

  • Related