Question
i need a complete working code and also screenshot of the output HAS TO BE DONE IN MARS COMPUTER ARCHITECTURE CLASS Lab 4: Factorial Function
i need a complete working code and also screenshot of the output
HAS TO BE DONE IN MARS
COMPUTER ARCHITECTURE CLASS
Lab 4: Factorial Function
Objectives
Implement a function that computes factorials recursively. The skills you practice in this lab include 1) using stack in a function, 2) implementing recursive functions, 3) using MIPS multiplication instructions, 4) dealing with data items larger than 32 bits.
Description
Implement a function that compute factorial recursively. The functions interface is
int Factorial2(unsigned int n, unsigned int *p)
The function takes one integer (n) as input and computes n!. The function can compute n! that can be represented by 64 bits (NOT only by 32 bits!.) The results are stored in the buffer specified by p. p[0]is the higher half of the result and p[1] the lower half (so big endian!). For example, if n = 0, then p[0] = 0 and p[1] = 1 (because 0! = 1). If n is larger than 1, Factorial2 first calls itself (recursively) to compute (n 1)!, and then computes n! = (n 1)! * n. Remember that the result stored in the buffer is a 64-bit data item. So your code will multiply a 64-bit number with a 32-bit number. Read the pseudo code listed on the next page carefully.
The function returns 1 on success and 0 on error. If the computation is done successfully, the code prints two 32-bit values in hexadecimal and also in decimal if the result is small.
You need to save some registers in your function (and you can save only the registers you have to save according to the calling convention). It is more convenient to save n on the stack, so you can use the value when doing the multiplication.
The techniques you used in the last lab and the experiences you gained there should be very helpful in this lab.
Your calculator may not be able to handle large values. Here are some results you can check.
17! = 0001 437E EECD 8000
20! = 21C3 677C 82B4 0000
Again, template files are provided. There are two files. One file is the printf function and the other is the one you will work on. Put them (and ONLY these two files) in a directory. When you assemble the files, make sure you have checked Settings/Assemble all files in directory. You can run the template files. The program asks you to input an integer and call an empty function to compute n!. The empty function returns 0 (indicating error).
Pseudo-code for computing factorial recursively
int Factorial2(unsigned int n, unsigned int *p)
{ # $a0 is n, $a1 is p
# One exit, so you only write code for restoring stack once. Allocate space on stack and save registers
if (n == 0) {
p[0] = 0 and p[1] = 1 # 0! = 1
goto Exit1
}
r = Factorial2(n 1, p) # Compute (n-1)!
if (r == 0) goto Exit0; # error t0 = p[0], t1 = p[1] # load (n-1)!
(t2, t3) = t1 * n # multiply with lower half
(t4, t5) = t0 * n # multiply with higher half if
(t4 != 0) goto Exit0; # overflow error
t6 = t2 + t5 # compute new higher half
if (there is an overflow) goto Exit0; # error
p[0] = t6, p[1] = t3
Exit1: rv = 1;
goto Exit;
Exit0: rv = 0; Exit: Restore saved registers and stack
Return rv;
}
// how 64-bit by 32-bit multiplication works: // overflow if t4 is not 0 or there is an overflow when doing t2 + t5.
// t0 t1 // x n // -------------------
// t2 t3 // + t4 t5
// ------------------- // t6 t3
----------------------------------------------------------
# Recursive Factorial function .data #data segment name: .asciiz "CSE3666: Lab n: Your Name(Your NetID) " msg0: .asciiz "%d! cannot be computed. " msg1: .asciiz "%d! = (%x %x) " msg2: .asciiz "%u in decimal. " .align 2 buffer: .space 8 # reserve space for two words n_in: .space 4 # get input from the user .text # Code segment .globl main # declare main to be global main: la $a0, name jal printf # call printf function li $s0, 49 li $v0, 5 #read an integer syscall move $s0, $v0 #save the user input la $s1, buffer # buffer #call Factorial2 move $a0, $s0 #save n to $s0 move $a1, $s1 jal Factorial2 bne $v0, $zero, l_ok # 0 is returned la $a0,msg0 # $a0 := address of message 1 move $a1, $s0 # the number jal printf j Exit l_ok: la $a0, msg1 move $a1, $s0 lw $a2, 0($s1) lw $a3, 4($s1) jal printf lw $a2, 0($s1) bne $a2, $0, Exit la $a0, msg2 lw $a1, 4($s1) jal printf Exit: li $v0,10 # System call, type 10, standard exit syscall # ...and call the OS # Factorial2(n, p) # Saved registers # $ra, $a0, $a1 (do not assume $a1 is not changed) Factorial2: li $v0, 0 # always returns 0 here jr $ra
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started