Mips Recursive Function Error in : invalid program counter value: 0x00000001

0

I am currently writing c program that can translate my own language grammar into mips code.

int i;
int b;
int add() {
    if (i < 10) {
        i = i + 1;
        return add();
    }
    return i;
}

i = 0;
b = add();

But my issue is in mips where I am running into memory address issues on how to properly handle recursion functions. I know my C code isn't optimizing mips but please ignore that. How can I fix this small mips program so I can handle other types of recursive functions.

        .text
        .globl          main
main:
        li              $t0, 0
        sw              $t0, i
        subu            $sp, $sp, 0
        jal             add
        addu            $sp, $sp, 0
        move            $t0, $v1
        sw              $t0, b
        li              $v0, 10
        syscall
add:
        subu            $sp, $sp, 4
        sw              $ra, ($sp)
        lw              $t0, i
        li              $t1, 10
        slt             $t2, $t0, $t1
        beq             $t2, 0, L1
        lw              $t0, i
        li              $t1, 1
        add             $t3, $t0, $t1
        sw              $t3, i
        subu            $sp, $sp, 4
        sw              $t2, 4($sp)
        jal             add
        lw              $t2, 4($sp)
        addu            $sp, $sp, 4
        move            $t0, $v1
        move            $v1, $t0
L1:
        lw              $t0, i
        move            $v1, $t0
        lw              $ra, ($sp)
        addu            $sp, $sp, 4
        jr              $ra
        .data
        .align          4
i:      .word           0
b:      .word           0

Any suggestions or answers would be much appreciated!

recursion
assembly
mips

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0