# 用C编写的Fibonacci计算器编译为mips会导致无限循环Fibonacci calculator in C, compiled to mips, causes infinite loop

C代码：

``````int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}

int main()
{
fibo(5);
}
``````

``````main: addi \$sp, \$sp, -4
sw \$ra, 0(\$sp)

jal fibo;

lw \$ra, 0(\$sp)

sw \$s0, 0(\$sp)
sw \$s1, 4(\$sp)
sw \$ra, 8(\$sp)

slti \$t0, \$a0, 2
beq \$t0, \$zero, ELSE

jr \$ra

jal fibo;

jal fibo

j EXIT

EXIT: lw \$s0, 0(\$sp)
lw \$s1, 4(\$sp)
lw \$ra, 8(\$sp)

jr \$ra
``````

## 3 个回复3

### ===============>>#1 票数：5

C代码：

``````int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}

int main()
{
fibo(5);
}
``````

``````main: addi \$sp, \$sp, -4
sw \$ra, 0(\$sp)

jal fibo;

lw \$ra, 0(\$sp)

sw \$s0, 0(\$sp)
sw \$s1, 4(\$sp)
sw \$ra, 8(\$sp)

slti \$t0, \$a0, 2
beq \$t0, \$zero, ELSE

jr \$ra

jal fibo;

jal fibo

j EXIT

EXIT: lw \$s0, 0(\$sp)
lw \$s1, 4(\$sp)
lw \$ra, 8(\$sp)

jr \$ra
``````

### ===============>>#2 票数：1

C代码：

``````int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}

int main()
{
fibo(5);
}
``````

``````main: addi \$sp, \$sp, -4
sw \$ra, 0(\$sp)

jal fibo;

lw \$ra, 0(\$sp)

sw \$s0, 0(\$sp)
sw \$s1, 4(\$sp)
sw \$ra, 8(\$sp)

slti \$t0, \$a0, 2
beq \$t0, \$zero, ELSE

jr \$ra

jal fibo;

jal fibo

j EXIT

EXIT: lw \$s0, 0(\$sp)
lw \$s1, 4(\$sp)
lw \$ra, 8(\$sp)

jr \$ra
``````

### ===============>>#3 票数：0

C代码：

``````int fibo(int n)
{
if(n<2) return 1;
else f(n-1)+f(n-2);
}

int main()
{
fibo(5);
}
``````

``````main: addi \$sp, \$sp, -4
sw \$ra, 0(\$sp)

jal fibo;

lw \$ra, 0(\$sp)

sw \$s0, 0(\$sp)
sw \$s1, 4(\$sp)
sw \$ra, 8(\$sp)

slti \$t0, \$a0, 2
beq \$t0, \$zero, ELSE

jr \$ra

jal fibo;

jal fibo

j EXIT

EXIT: lw \$s0, 0(\$sp)
lw \$s1, 4(\$sp)
lw \$ra, 8(\$sp)

jr \$ra
``````

1回复

1回复

1回复

2回复

1回复

2回复

2回复

1回复

6回复