MIPS 1 The Factorial The factorial of a nonnegative integer
MIPS
1) The Factorial The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. Enclosed is a simplified version of the MIPS assembly language recursive implementation of the factorial function. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multinliots. 2) Recursive definition of multiplication The function rmult(a, b) for two positive integers 1 S a, and 1 5 b, is defined as the following: rmult(a, 1) = a; rmult(a, b) = a + rmult(a, b-1) Write a recursive version of rmult) in C or C++ and a pseudo C program (based on chapter 2 in the book) then use these programs to develop a MIPS program that gets as input two integers 0a 255, and 0 b 255, and returns the result of mult(a, b) in $v1Solution
a)
this program computes factorial of entered number with recursion.
# it illustrates how to set up the procedure stack
#
.data
msg_str: .asciiz \"Enter some Number: \"
.text
.globl main
main:
la $a0, msg_str
li $v0, 4
syscall
li $v0, 5
syscall
move $a0,$v0 # compute 4!
jal fac
move $a0,$v0 # get result
li $v0,1 # print integer
syscall
li $v0,10
syscall
#
# fac(arg) - computes factorial of arg (arg!)
# argument is passed in $a0
# stack frame:
#
# | ...high address... |
# |--------------------|
# | |
# |--------------------|
# | return address | +4
# |--------------------|
# $sp->| saved $s0 | +0
# |--------------------|
# | ...low address... |
#
#
fac:
# prologue to procedure
addi $sp,$sp,-8 # push space for activation frame
sw $s0,0($sp) # save $s0, which we use
sw $ra,4($sp) # save return address
# start of actual procedure work
move $s0,$a0 # get argument ($a0)
li $v0,0x00000001 # 1
beq $s0,$v0,L2 # end of recursion?
addi $a0,$s0,-1 # set up argument (f-1)
jal fac # recursive call
mult $v0,$s0 # multiply
mflo $v0 # return mul result
j L3 # exit procedure via epilogue
L2:
li $v0,0x00000001 # return value
# epilogue to exit procedure
L3:
lw $ra,4($sp) # restore $ra
lw $s0,0($sp) # restore $s0
addi $sp,$sp,8 # pop activation frame
jr $ra # return
b)
data
str1: .asciiz \"Enter a: \"
str2: .asciiz \"Enter b: \"
str5: .asciiz \"a*b = \"
newline: .asciiz \"\ \"
.text
main: li $v0, 4 # system call code for print_string
la $a0, str1 # address of str1
syscall # print str1
#get the first number from user, put it into $s0
li $v0, 5 # system call code for read_int
syscall # read an integer into $v0 from console
add $s0, $v0, $zero # copy $v0 into $s0 (a)
#read print_string for str2
li $v0, 4 # system call code for print_string
la $a0, str2 # address of str1
syscall # print str1
# get second number from user, put it into $t1
li $v0, 5 #load syscall for read_int
syscall #make the syscall
move $s1, $v0 #move the number read into $s1(b)
#DO THE CALCULATIONS................................................
div $s0, $s1 #diving $s0 by $s1
mflo $t0 #storing value of lo(quotient) in
#register $t0
mfhi $t1 #storing value of hi(remainder) in
#register $t1
mult $s0, $s1
mflo $t2
li $v0,1
move $a0, $t2
syscall
li $v0,4
la $a0, str5
syscall
#read print_string for str3
li $v0, 4 # system call code for print_string
la $a0, str3 # address of str1
syscall # print str1
#print a/b
li $v0, 1 #load syscall print_int into $v0
move $a0, $t0 #move the number to print into $t2
syscall
# read print string for str4
li $v0, 4
la $a0, str4
syscall
# print remainder
li $v0, 1
move $a0, $t1
syscall
#end of program


