# Ackermann Function

Discussion in 'Assembly Language Programming (ALP) Forum' started by final_semester, Sep 23, 2009.

1. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
Can anyone help me in writing Ackermann Function function in assembly language. I'm using MIPS and the function needs to be Recursive.

h**p://img41.imageshack.us/img41/9972/akermannfunction.jpg

2. ### nimeshNew Member

Joined:
Apr 13, 2009
Messages:
770
20
Trophy Points:
0
Occupation:
Location:
Mumbai
go easy on the fonts buddy

3. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
We won't write it for you. How far have you got and where are you stuck?

4. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
here guys I was able to write the C program .. but m having trouble writing the MIPS version of it... I'm having difficulty using the stack pointer and what not...

this is what I have so far
C code
Code:
#include<stdio.h>
#include<conio.h>

int AckFun(int,int);

void main()
{
int result,m,n;

clrscr();
printf("Enter the positive integer values of m and n : ");
scanf("%d %d",&m,&n);

result=AckFun(m,n);

printf("\nThe value of A(%d,%d) = %d",m,n,result);
getch();
}

int AckFun(int m,int n)
{
int send;

if(m==0)
{
if(n==0)
send=1;
else
send=n+1;
}
else if(n==0)
send=AckFun(m-1,1);
else
send=AckFun(m-1,AckFun(m,n-1));
return=send;
}
MIPS code
Code:

main:
li \$so, 3	# m = s0 = a0
li \$s1, 3	# n = s1 = a1

jal ackermann

move \$a0, \$v0
li \$v0, 1
syscall

li	\$v0, 10
syscall

ackermann:

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

case_one: bne \$a0, 0, case_two

5. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
this is how far I have reached

Code:
main:
li \$a0, 3	# m = a0
li \$a1, 3	# n = a1

jal ackermann

move \$a0, \$v0
li \$v0, 1
syscall

li	\$v0, 10
syscall

ackermann:

sw \$ra, 4(\$sp)
sw \$a1,	0(\$sp)

bne \$a0, 0, other

lw \$ra, 4(\$sp)

jr \$ra

other:

bne \$a1, 0, other_two
jal ackermann

other_two:

jal ackermann

jal ackermann

6. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
k..now m stuck here

Code:
main:
li \$a0, 3	# m = 3
li \$a1, 3	# n = 3

jal ackermann

move \$a0, \$v0
li \$v0, 1
syscall

li	\$v0, 10
syscall

ackermann:

addi \$sp, \$sp, -12		# creating space on the stack (3 columns)
sw \$a0, 4(\$sp)			# saving origional m
sw \$a1, 8(\$sp)			# saving origional n
[COLOR="Yellow"]	sw \$ra, 0(\$sp)			# saving origional caller return address           ERROR ON THIS LINE[/COLOR]

case_one:					# m = 0

bne \$a0, 0, case_two

j exit

case_two:					# n = 0

bne \$a1, 0, case_three
li \$a1, 1

jal case_one

case_three:

jal ackermann

jal case_one

exit:

lw \$ra, 0(\$sp)
jr \$ra

7. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Well, if you're in your final semester, you should already know that the exact error message is rather essential in helping identifying the cause of the problem. So why not tell us what the error message is instead of just saying "error on this line".

8. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
my expertise is not in programming but in analog circuit designing, DSP etc... I dont want the solution, just a better explanation why's there a error on that line

9. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Well, how do we know if we don't know what the error is.

OK, let me say it straight. TELL US THE ERROR.

10. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
main:
li \$a0, 3 # m = 3
li \$a1, 3 # n = 3

jal ackermann

move \$a0, \$v0
li \$v0, 1
syscall

li \$v0, 10
syscall

ackermann:

addi \$sp, \$sp, -12 # creating space on the stack (3 columns)
sw \$a0, 4(\$sp) # saving origional m
sw \$a1, 8(\$sp) # saving origional n
sw \$ra, 0(\$sp) # saving origional caller return address ERROR HERE

case_one: # m = 0

bne \$a0, 0, case_two

j exit

case_two: # n = 0

bne \$a1, 0, case_three
li \$a1, 1

jal case_one

case_three:

jal ackermann

jal case_one

exit:

lw \$ra, 0(\$sp)
jr \$ra

11. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Where did the error occur? (I'm asking this, because I've asked "What is the error" twice now and got the answer to "Where did the error occur?" each time. So I'm working on the assumption that the questions "What was the error" and "Where did the error occur" are somehow transposed in your brain, and that asking the other question will get the answer I'm actually interested in.)

12. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
the error occurred when I was running this code in MARS 3.5 java based simulator

13. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
OK I give up. No idea.

14. ### final_semesterNew Member

Joined:
Sep 23, 2009
Messages:
10
0
Trophy Points:
0
ERROR:
Code:
Go: running ackermann.asm

Error in ackermann.asm line 22: Runtime exception at 0x0040002c: address out of range 0x7fbffffc

Go: execution terminated with errors.

Joined:
Sep 23, 2009
Messages:
10