I have a very unusual problem that I can't figure out, and couldn't find a solution anywhere.
I have written a code for calculating exponentiation of floating point numbers in MIPS. The Algorithm works fine but in the end when the syscall should print the returned value, it prints only 0.
Following is the code used to print the returned value, it seems perfectly fine to me, I have no clue whats wrong.
mov.s $f12, $f31
li $v0, 2
syscall
I tried to debug the code in qtSpim and everything seems to work fine, even the values in respected registers are correct at the time of syscall.
Sample Output :
FP Registers while printing the result:
The values in registers seem to be fine, 4 * 4 = '16' in decimal or '10' in hex.
Complete Code :
.text
main:
addi $v0,$zero,4
la $a0,enter
syscall
li $v0, 6
syscall
mtc1 $t0, $f10
add.s $f31,$f0,$f10
addi $v0,$zero,4
la $a0,enter_2
syscall
li $v0, 5
syscall
mtc1 $t0, $f10
add.s $f12,$f31,$f10 # $a0 = x
addu $a0,$v0,$zero # $a1 = n
jal exp
mtc1 $t0, $f10
add.s $f31, $f0, $f10 #Save returned value
addi $v0,$zero,4
la $a0,result
syscall
mov.s $f12, $f31
li $v0, 2
syscall
addi $v0,$zero,4
la $a0,end
syscall
addi $v0,$zero,10
syscall
exp:
mtc1 $t0, $f10
add.s $f12,$f12,$f10 # let $f12 = x;
addu $s1,$a0,$0 # let $s1 = n
li $t1, 1
mtc1 $t1, $f11
add.s $f31, $f10, $f11 # f31=r=1;
add.s $f1, $f12, $f10 # f1=p=s0;
while:
and $t3, $s1, $t1
bne $t3,$t1,if_1
mul.s $f31, $f31, $f1
if_1:
srl $s1, $s1, $t1
bne $s1, $zero, if_2
add.s $f0, $f31,$f10
jr $ra
if_2:
mul.s $f1,$f1,$f1
j while
jr $ra
.data
enter: .asciiz "\nPlease enter a number : "
enter_2: .asciiz "\nPlease enter an exponent : "
result: .asciiz "\nResult : "
end: .asciiz "\n\nThe End :)"
$t0
without having initialized it to anything? Also, the fact that you're doingadd.s
directly after anmtc1
without anycvt.s.w
looks suspicious. – Michael