Reputation: 240
My program does what it's supposed to do recursively, but right after my program calculates my final answer, the program terminates with errors. More specifically: Error in : invalid program counter value: 0x00000000
I believe it's a problem with my jr $ra because while debugging my code, I saw that the error is thrown at the second jr $ra right after the program calculates the answer.
How would I solve this problem? I need the program to print out the number stored in register $v0, but the program fails before I can do that.
.data
msg: .asciiz "Enter a positive integer n: \n"
msg2: .asciiz "Its CValue is: \n"
.text
li $v0,4
la $a0,msg
syscall
li $v0,5
syscall
move $a0,$v0
cValue:
addi $sp,$sp,-4
sw $ra,0($sp)
bgt $a0,1,recursive
li $v0,1
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
recursive:
addi $a0,$a0,-1
jal cValue
li $t7,3
mult $v0,$t7
mflo $v0
addi $v0,$v0,1
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
Upvotes: 2
Views: 7969
Reputation: 58507
The last jr $ra
performed will jump to whatever $ra
was set to before first entering cValue
. Since you don't have an initial jal cValue
, the value of $ra
will be whatever it was when your program started. Typically that would be somewhere in the C runtime that takes control when your main
returns, and that's the behavior I get in PcSpim.
I don't know why you get the "Invalid program counter" error since you haven't specified your execution environment. But as I've mentioned above, adding a jal cValue
should solve the problem by continuing execution after the jal
once the subroutine has finished. Then you can do whatever you need to do to exit the program cleanly.
Upvotes: 2