3.1 Write a recursive MIPS procedure to compute Ackerman Function. The program s
ID: 3631214 • Letter: 3
Question
3.1 Write a recursive MIPS procedure to compute Ackerman Function. The program should take two integer numbers, m and n, and compute A(m,n) for them. The result is also an integer.3.2 Write a main program that takes inputs of m and n from the keyboard; call the recursive procedure, and then print the result.
Note: Ackerman function is a very rapidly growing function. Even values of 4 for m and n will yield an extremely large number. A(4, 2) is about 2×10^19728. So use small values of m and n, like 1, 2 or 3 when testing your program.
Explanation / Answer
.data read_m: .asciiz "Enter the value of m:" read_n: .asciiz "Enter the value of n:" result: .asciiz "The value of A(m,n) is: " .text .globl main main: sub $sp,$sp,20 # Set up main's stack frame: sw $ra,16($sp) sw $fp,12($sp) add $fp,$sp,20 la $a0,read_m li $v0,4 syscall li $v0,5 syscall move $t0,$v0 la $a0,read_n li $v0,4 syscall li $v0,5 syscall move $t1,$v0 move $a0,$t0 move $a1,$t1 jal acker move $t2,$v0 la $a0,result li $v0,4 syscall move $a0,$t2 li $v0,1 syscall li $v0,10 syscall acker: #recursive function starts here bgt $a0,0,acker_recurse add $v0,$a1,1 jr $ra acker_recurse: bgt $a1,0,case_3 sub $sp,$sp,8 sw $ra,4($sp) sw $fp,($sp) add $fp,$sp,8 sub $a0,$a0,1 li $a1,1 jal acker lw $ra,4($sp) # restore Return Address. lw $fp,($sp) # restore Frame Pointer. add $sp,$sp,8 # restore Stack Pointer. jr $ra case_3: sub $sp,$sp,12 sw $ra,8($sp) sw $fp,4($sp) add $fp,$sp,12 move $s0,$a0 move $s1,$a1 sw $s0,($sp) sub $a1,$s1,1 jal acker move $s2,$v0 lw $s0,($sp) move $a0,$s0 move $a1,$s2 jal acker lw $ra,8($sp) # restore Return Address. lw $fp,4($sp) # restore Frame Pointer. add $sp,$sp,12 # restore Stack Pointer. jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.