In: Computer Science
Write a MIPS Assembly Language program to perform the following operations:
Request and read two integers from the console (Dn and Up),
Call a recursive function to compute the result
int RecurseFunc( int Dn, int Up )
{
if( Dn < 1 ) return 0;
else return Dn * Up + RecurseFunc( Dn - 1, Up + 1 );
}
Print out the results
Submit your code and report here.
CODE IN MIPS:
RecurseFunc(int, int):
daddiu $sp,$sp,-48
sd $31,40($sp)
sd $fp,32($sp)
sd $28,24($sp)
sd $16,16($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(RecurseFunc(int, int))))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(RecurseFunc(int, int))))
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,0($fp)
sll $2,$2,0
sw $2,4($fp)
lw $2,0($fp)
bgtz $2,.L2
nop
move $2,$0
b .L3
nop
.L2:
lw $3,0($fp)
lw $2,4($fp)
mult $3,$2
mflo $16
lw $2,0($fp)
addiu $2,$2,-1
move $3,$2
lw $2,4($fp)
addiu $2,$2,1
move $5,$2
move $4,$3
ld $2,%got_disp(RecurseFunc(int, int))($28)
move $25,$2
1: jalr $25
nop
addu $2,$16,$2
.L3:
move $sp,$fp
ld $31,40($sp)
ld $fp,32($sp)
ld $28,24($sp)
ld $16,16($sp)
daddiu $sp,$sp,48
j $31
nop
.LC0:
.ascii "enter two numbers\000"
.LC1:
.ascii "%d%d\000"
.LC2:
.ascii "%d\000"
main:
daddiu $sp,$sp,-48
sd $31,40($sp)
sd $fp,32($sp)
sd $28,24($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
ld $2,%got_page(.LC0)($28)
daddiu $4,$2,%got_ofst(.LC0)
ld $2,%call16(printf)($28)
move $25,$2
1: jalr $25
nop
daddiu $3,$fp,8
daddiu $2,$fp,4
move $6,$3
move $5,$2
ld $2,%got_page(.LC1)($28)
daddiu $4,$2,%got_ofst(.LC1)
ld $2,%call16(scanf)($28)
move $25,$2
1: jalr $25
nop
lw $2,4($fp)
lw $3,8($fp)
move $5,$3
move $4,$2
ld $2,%got_disp(RecurseFunc(int, int))($28)
move $25,$2
1: jalr $25
nop
sw $2,0($fp)
lw $2,0($fp)
move $5,$2
ld $2,%got_page(.LC2)($28)
daddiu $4,$2,%got_ofst(.LC2)
ld $2,%call16(printf)($28)
move $25,$2
1: jalr $25
nop
move $2,$0
move $sp,$fp
ld $31,40($sp)
ld $fp,32($sp)
ld $28,24($sp)
daddiu $sp,$sp,48
j $31
nop