Factorial (Recursive)

Objective

To implement a program that computes the factorial of a number in recursive manner. 

 

Instructions

The program to compute the factorial of a number in a recursive manner and its equivalent low-level language instructions executed by the hardware are given below:

rem It performs division operation of the values contained in two registers and puts the remainder in the first register. 
rcopy Passing variable value back to caller function. 
push It pushes a variable on to the stack. 
pop It pops out the variable out of the stack. 
store It stores the value from the given register to the global or local variable. 
out It prints the output value to the screen. 
neq Check if the values in two registers are different. 
mul It multiplies the values contained in two registers and puts the result in the first register. 
lcopy Local copying- used to copy the value of a variable within the same memory segment. 
load It loads a value of a global or local variable to the given register. 
label Is used to mark the position in the Low-level Language instructions to enable jumping to the marked position. 
in It reads the input from the user and stores it in a variable. 
if_true Checks if the condition evaluated to true. 
if_false Checks if the condition evaluated to be false. 
freturn It returns the control from the called function to the caller. 
fenter It changes the control of execution to the called function. 
fcall It initiates a call to the given function. 
exit Signifies the end of program. 
div It divides the values contained in two registers and puts the result in the first register. 
copy It copies the value of a variable between segments of memory. 
assign It assigns a value to a variable. 

 

Learning Outcomes

  • To able to explain how the factorial of a number is calculated using recursive calls, breaking down the problem into simpler subproblems. 
  • To able to understand concept of recursion and how functions can call themselves. 
  • To be able to recognize the base case in a recursive function, which serves as the termination condition to stop the recursive calls. 
  • To able to trace the execution of the recursive calls for a given input.