MIPS recursion output error

2019-07-31 17:17发布

问题:

I'm a beginner when it comes to MIPS assembly. We were tasked to create a program that accepts two positive integers, n and r. Then, if n=r OR r=0, f(n,r) would be equal to 1. Otherwise, f(n,r) = f(n-1,r) + f(n-1,r-1). This code clearly implements a recursive function with it calling itself.

Here's my code:

    .data 
x: .word 4
y: .word 2

.text
    main: 
        sub $t3, $t3, $t2
        sub $t4, $t4, $t4
        lui $t0, 0x1001 
        lw $t1, 0($t0)     #this is your initial n 
        lw $t2, 4($t0)     #this is your initial r

    form_base:                                   #this part checks the (n==r || r==0) OR otherwise 
        beq $t1,$t2, IF 
        bne $t2, $zero, form_recurse 
       IF:     add $t4, $t4, 1
               jr $ra

    form_recurse: 
        sub $sp, $sp, 16
        sw $ra, 0($sp)
        sw $t1, 4($sp)
        sw $t2, 8($sp)
        add $t1, $t1, -1         #this is the n-1 part
        add $t2, $t2, $zero      #this is the r part 
        jal form_base
        lw $t1, 4($sp)
        lw $t2, 8($sp)
        sw $t4, 12($sp)
        add $t1, $t1, -1        #this is the n-1 part
        add $t2, $t2, -1        #this is the r-1 part 
        jal form_base
        lw $t4, 12($sp)
        sw $t4, 12($t0)
        add $sp, $sp, 16

THE PROBLEM: the output, given n=4 and r=2, should've been 6 but instead it's giving me the value of r as the output itself. Where have I gone wrong? Help appreciated!