卡在MIPS递归函数上 [英] Stuck on a MIPS recursive function

查看:139
本文介绍了卡在MIPS递归函数上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing an assignment in MIPS and I feel like my logic is sound but I just can't get the code to produce the correct output. Can anyone look over what I've got and help? This is the corresponding function in C that I'm trying to implement.

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,因此很显然,我浪费了很多时间.

This is what I've got in MIPS. For the sample output, when the user enters 8 the output is supposed to be 1096. I get 22,735 so obviously I'm messing up big time.

.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

推荐答案

当用户输入8时,输出应该是1096.

when the user enters 8 the output is supposed to be 1096.

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

Before considering your MIPS code, your C code doesn't appear to produce a correct result either:

#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.也许这是我们应该开始调试的地方.

This code prints 7842, not the 1096 you claim. Perhaps this is where we should begin debugging.

手动仔细检查:

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
>>> 

这篇关于卡在MIPS递归函数上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆