Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate the power of double number in MIPS

I have tried to write the code that will calculate the power of the double number recursively. But I do not get the wanted output. My output is Error in : invalid program counter value: 0x00000000
. Here I got base numbers from loop1 and power from loop2. In power function, if the power is 0, the loop will return 1.

.data
array1: .double 2.0 2.0 3.0 3.0 4.2 5.0 2.0 0.0 0.0
array2:  .word 5 4 2 1 0 2 3 0 10
array_size: .word 9 
newLine: .asciiz "\n"
one: .double 1.0
.text

main:
    la $s1 array1
    la $s2 array2
    lw $s3 array_size #s3 = 9
    
    li $s4, 0
loop: 
    bge $s4 $s3 exit 

    l.d $f14 0($s1)
    lw $a2 0($s2)
    
    jal power
    
    li $v0, 3           
        mov.d $f12, $f20   
        syscall             

    addi $s1 $s1 8  
    addi $s2 $s2 4  
    
    addi $s4, $s4, 1 #index
    
    j loop
power: 
    andi $sp 0xfffffff8 
    addi $sp $sp -16
    s.d $f20 ($sp)  
    sw $ra 8($sp)
        s.d $f14, 8($sp)
        
    l.d $f20 one
    beq $a2 $zero done
    
    addi $a2 $a2 -1
    jal power
    
    mul.d $f20 $f14 $f20
    
done:
    l.d $f14, 8($sp)
    lw $ra 8($sp)
    l.d $f20 ($sp)
    addi $sp $sp 16
    jr $ra  
    
exit: 
    li $v0, 10         
    syscall

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re using the same stack slot for both $f20 and $f14.

One slot can only hold one value.  Since that code doesn’t otherwise use $f20, simply remove save & restore of that.

However, you’re saving and restoring $f14 for the benefit of the caller, which is incorrect.  That is a scratch register (call-clobbered) and it should be saved & restored for your own function body’s benefit.

That means that the restore of $f14 should go before the multiplication rather than as part of epilogue.

The multiplication should apply to $f0 and $f14, leaving the result in $f0, and at such a time that $f14 is restored.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading