Question

In: Electrical Engineering

Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...

Assignment Instructions:

1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM

2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??, and 1 ? ??, is defined as the following: ??????????(??, 1) = ??; ??????????(??, ??) = ?? + ??????????(??, ?? ? 1) Write a recursive version of ??????????() 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 0 < ?? ? 255, and 0 < ?? ? 255, and returns the result of ??????????(??, ??) in $v1. Your deliverable should be the pseudo C and the assembly level function

Given code file:

#####################################################################################

# Functional Description: Main program to test Factorial function # Enter a negative number to terminate run

#####################################################################################

.data

.align 2

prompt:   .asciiz "\n\n Give me a value for \"N\" : "

msg: .asciiz " N factorial is: "

bye: .asciiz " \n *** Good-Bye ***"

.text

main: addiu $sp, $sp, -8 #Allocate space

mloop:

li $v0, 4

la $a0, prompt

syscall

li $v0, 5 #Get value for N

syscall

bltz $v0, quit

sw $v0, 0 ($sp)

jal Fac # Call factorial

li $v0, 4 # Print message

la $a0, msg

syscall

lw $a0, 4($sp) #Get result

li $v0, 1

syscall #Print factorial

b mloop

quit:

addiu $sp, 8 # Deallocate space

li $v0, 4 la $a0, bye

syscall li $v0, 10

syscall

#####################################################################################

# Functional Description: Recursive Factorial Fac (N: in, N! :out)

#####################################################################################

Fac:

lw $a0, 0 ($sp)

bltz $a0, Problem

addi $t1, $a0, -13

bgtz $t1, Problem # 13 is largest value we can

# accept

addiu $sp, $sp, -16 # Allocate

sw $ra, 12 ($sp) # Save return address

sw $a0, 8($sp)

slti $t0, $a0, 2 # If N is 1 or 0, then return the value 1

beqz $t0, Go

li $v0, 1

b facret

Go:

addi $a0, $a0, -1 #

sw $a0, 0 ($sp) # Pass N-1 to factorial function

jal Fac # Recursive call

lw $v0, 4($sp) # Get (N-1) ! back.

lw $ra, 12 ($sp)

lw $a0, 8 ($sp)

mult $v0, $a0 # N* (N-1) !

mflo $v0

facret:

addiu $sp, $sp, 16 # Deallocate

sw $v0, 4 ($sp)

jr $ra

Problem:

sw $0, 4 ($sp)

jr $ra

Second give code file:

#####################################################################################

# Functional Description: Main program to test Factorial function # Enter a negative number to terminate run

#####################################################################################

.data

.align 2

.text

main: addiu $sp, $sp, -8 # Allocate space

mloop:

li $v0, 4 # Get value for N

sw $v0, 0 ($sp)

jal Fac # Call factorial

or $v1, $v0, $0

addiu $sp, 8 # Deallocate space

li $v0, 10

syscall

#####################################################################################

# Functional Description: Recursive Factorial Fac (N: in, N! :out)

#####################################################################################

Fac:

lw $a0, 0 ($sp)

addiu $sp, $sp, -16 # Allocate

sw $ra, 12 ($sp) # Save return address

sw $a0, 8($sp)

slti $t0, $a0, 2 # If N is 1 or 0, then return the value 1

eqz $t0, Go

li $v0, 1

b facret

Go:

addi $a0, $a0, -1 #

sw $a0, 0 ($sp) # Pass N-1 to factorial function

jal Fac # Recursive call

lw $v0, 4($sp) # Get (N-1) ! back.

lw $ra, 12 ($sp)

lw $a0, 8 ($sp)

mult $v0, $a0 # N* (N-1) !

mflo $v0

facret:

addiu $sp, $sp, 16 # Deallocate

sw $v0, 4 ($sp)

jr $ra

Solutions

Expert Solution

# Start of Program

The Factorial and Recursive definition of the multiplication

.data

msg_str: .asciiz "Enter any 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 "\n"

.text

main: li $v0, 4 # system call code for print_string

la $a0, str1 # address of str1

syscall # print str1

#We can 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

# We can 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)

#PERFORM 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


Related Solutions

Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Write a program factor that prints the prime factors of a specified non-negative integer. The integer...
Write a program factor that prints the prime factors of a specified non-negative integer. The integer will be provided as a command-line argument. You may assume that the integer will be greater than 1 and less than or equal to the largest signed integer on your platform (that is, you may use an int).
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
8. Definition: A set A is finite if there exists a non-negative integer c such that...
8. Definition: A set A is finite if there exists a non-negative integer c such that there exists a bijection from A to {n ∈ N : n ≤ c}. (The integer c is called the cardinality of A.) (a) Let A be a finite set, and let B be a subset of A. Prove that B is finite. (Hint: induction on |A|. Note that our proof can’t use induction on |B|, or indeed refer to “the number of elements...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.   b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT