## PieterA

Hi everyone

First post here on this forum. I'm awfully stuck in an exercice I have to complete as an assignment. I'm studying computer science and this has been my first year.

This is what I am supposed to do:

Your project consists of implementing the following C code:

``````int f1(int a) {
if (a < 3)
return (a);
else
return (f2(a-1, a-2) + f2(a-2, a-1));
}
int f2(int x, int y) {
return 2*f1(x) + y;
}``````

This is what I have so far:

``````.text
main:
# retrieve input from user
syscall     # syscall for read int

# store user input as first operand
move \$a0, \$v0     # store read int as argument

# calculate result
jal f1     # function call

j display

j exit

f1:
# make room on stack for \$ra and \$a0
move \$fp, \$sp     	# new fp is sp
addi \$sp, \$sp, -8     # sp gets bigger

# store both those registers on the stack
sw \$a0, 0(\$fp)     # store function argument
sw \$ra, -4(\$fp)     # store return address

# recursive condition
slti \$t0, \$a0, 3    # if a < 3, t0 = 1
li \$t1, 1			# Load int into t1 for comparison
bne \$t0, \$t1, f1_L1 # Branch if a >= 3
move \$v0, \$a0    # set a0 as result
j f1_return     # go to return code

# recursive calls
f1_L1:
addi \$a1, \$a0, -1 # Prepare first argument
addi \$a2, \$a0, -2 # Prepare second argument
jal f2_L1 # Function call
jal f2_L1

# return sequence f1
f1_return:
# restore \$ra and \$a0 registers
lw \$ra, -4(\$fp)     # restore previous return address
lw \$a0, 0(\$fp)     # restore previous function argument

move \$sp, \$fp     # sp gets smaller (remove frame)

jr \$ra     # continue at return address

# f2
f2_L1:

# make room on stack for \$ra, \$a1, \$a2
move \$fp, \$sp     	# new fp is sp
addi \$sp, \$sp, -12     # sp gets bigger

# store all three registers on the stack
sw \$a2, 0(\$fp)     # store function argument 2
sw \$a1, -4(\$fp)     # store function argument 1
sw \$ra, -8(\$sp)		# store return adress

move \$a0, \$a1
jal f1
li \$t0, 2
mult \$v0, \$t0
mflo \$v1

# return sequence f2
f2_return:
# restore \$ra, \$a1, \$a2
lw \$ra, -8(\$fp)     # restore previous return address
lw \$a1, -4(\$fp)     # restore previous function argument
lw \$a2, 0(\$fp)

move \$sp, \$fp     # sp gets smaller (remove frame)

jr \$ra     # continue at return address

# display result
display:
move \$a0, \$v0     # store arg for syscall
li \$v0, 1     # load code for display int
syscall     # syscall for display int

# exit program
exit:
li \$v0, 10     # load code for exit
syscall     # syscall for exit``````

When I run this code, I get an error:

Error in cs06.asm line 42: Runtime exception at 0x00400058: arithmetic overflow

I have highlighted this line in the code.

Anyone sees anything to help me ? Thanks.

## PieterA

Is my explanation of the problem confusing? Or is MIPS not as widespread as I think?