In: Computer Science
Write a MIPS Assembly Language program to perform the following operations:
int RecurseFunc( int Dn, int Up )
{
if( Dn < 1 ) return 0;
return Dn * Up + RecurseFunc( Dn - 1, Up + 1 );
}
Please find the code below::
.data
prompt: .asciiz "\nEnter first integer: "
prompt1: .asciiz "\nEnter second integer: "
.text
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t0,$v0
li $v0,4
la $a0,prompt1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0
move $a0,$t0 #pass parameter to function
move $a1,$t1 #pass parameter to function
jal RecurseFunc #call function
move $s3,$v0 #get result
move $a0,$s3 #ready to print
li $v0,1 #print integer
syscall
li $v0,10 #terminat
syscall
RecurseFunc :
subu $sp,$sp,4 # point to the place for the new item,
sw $ra,($sp) # store the contents of $ra as the new top.
subu $sp,$sp,4 # point to the place for the new item,
sw $a0,($sp) # store the contents of $a0 as the new top.
subu $sp,$sp,4 # point to the place for the new item,
sw $a1,($sp) # store the contents of $t5 as the new top.
blt $a0,1,zeroValue #check three terminate condition
sub $a0,$a0,1 #dn-1
add $a1,$a1,1 #up+1
jal RecurseFunc
move $s0,$v0
add $a0,$a0,1 #dn-1
sub $a1,$a1,1 #up+1
mul $t0,$a0,$a1
add $v0,$s0,$t0
j ifExit
j ifExit
zeroValue:
li $v0,0 #load zero
j ifExit
ifExit:
lw $a1,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
lw $a0,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
lw $ra,($sp) # store the contents of $ra as the new top.
addu $sp,$sp,4 # point to the place for the new item,
jr $ra
output: