卡在MIPS递归函数上

时间:2018-09-15 23:39:09

标签: recursion assembly mips

我正在MIPS中进行作业,我觉得自己的逻辑是合理的,但我只是无法获取代码以产生正确的输出。谁能看得到我的帮助?这是我要实现的C语言中的相应功能。

int function1(int n) {
if (n <= 3) {
    int ans1 = (3*n)-5;
    return ans1;
  }
else {
    int ans1 = (n-1)*function1(n-1) + function1(n-2) - n;
    return ans1;
  }
}

这就是我在MIPS中所拥有的。对于样本输出,当用户输入8时,输出应该为1096。我得到22,735,因此很显然,我浪费了很多时间。

.data

message1: .asciiz "Enter an integer\n"
message2: .asciiz "The integer is: "

.text
.globl main

main:

la $a0, message1
li $v0, 4
syscall # print message1 string

li $v0, 5
syscall # get user input 

move $a0, $v0 # int n 
jal function # call function                  
move $s0, $v0 # save function return value

la $a0, message2
li $v0, 4
syscall # print message2 string

move $a0, $s0
li $v0, 1
syscall # print the solution computed by the recursive function

li $v0, 10
syscall # exit 

function:

addi $sp, $sp, -12 # allocate memory on stack
sw $ra, 0($sp) # save return address
sw $a0, 4($sp) # save argument

ble $a0, 3, base_case # if n is <= 3 go to base case

addi $a0, $a0, -1 # n -= 1
jal function # recursive call
lw $a0, 4($sp) # load n

mul $v0, $v0, $a0 # (n-1) * function(n-1)
sw $v0, 8($sp) # save result

lw $a0, 4($sp) # load n
addi $a0, $a0, -2 # set n -= 2
jal function # recursive call

lw $t0, 8($sp) # load (n-1) * function(n-1)
add $v0, $v0, $t0 # add f(n-2) and (n-1) * f(n-1)

lw $t1, 4($sp) # load n 
sub $v0, $v0, $t1 # (n-1) * f(n-1) + f(n-2) - n

lw $ra, 0($sp) # load return address
addi $sp, $sp, 12 # free stack
jr $ra # return

base_case:

li $t5, 3
mul $v0, $a0, $t5 # (n*3) 
addi $v0, $v0, -5 # (n*3) - 5

lw $ra, 0($sp) # load return address
addi $sp, $sp, 12 # free stack
jr $ra # return

1 个答案:

答案 0 :(得分:0)

  

当用户输入8时,输出应为1096。

在考虑您的MIPS代码之前,您的 C 代码似乎也不会产生正确结果:

#include <stdio.h>

int function1(int n) {
    if (n <= 3) {
        return 3 * n - 5;
    }

    return (n - 1) * function1(n - 1) + function1(n - 2) - n;
}

int main() {
    printf("%d\n", function1(8));

    return 0;
}

此代码显示7842,而不是您要求的1096。也许这是我们应该开始调试的地方。

手动仔细检查:

function1(8)
7 * function1(7) + function1(6) - 8
7 * (6 * function1(6) + function1(5) - 7) + (5 * function1(5) + function1(4) - 6) - 8
7 * (6 * (5 * function1(5) + function1(4) - 6) + (4 * function1(4) + function1(3) - 5) - 7) + (5 * (4 * function1(4) + function1(3) - 5) + (3 * function1(3) + function1(2) - 4) - 6) - 8
7 * (6 * (5 * (4 * function1(4) + function1(3) - 5) + (3 * function1(3) + function1(2) - 4) - 6) + (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) - 7) + (5 * (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7 * (6 * (5 * (4 * (3 * function1(3) + function1(2) - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7 * (6 * (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8

> python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 7 * (6 * (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) + (4 * (3 * 4 + 1 - 4) + 4 - 5) - 7) + (5 * (4 * (3 * 4 + 1 - 4) + 4 - 5) + (3 * 4 + 1 - 4) - 6) - 8
7842
>>>