Question: Program 1 0 : Factorials The two standard recursion formulas used in teaching programming skills are the Fibonacci series and factorials. We will write a
Program : Factorials
The two standard recursion formulas used in teaching programming skills are the Fibonacci series and factorials. We will write a program that calculates and prints the number to stdout. In the next program, we will build the Fibonacci program.
An easy recursion method for factorials is to define a reentrant function that has the following behavior. When called, the function saves the count or input value then subtracts one from the count. If the reduced count is greater than then call itself again with the reduced count. Otherwise on the function returns to the caller function with a result of When the callee function returns, take the result and multiply it with the saved count or input value. Return the product value to the caller function.
In the main template, we see that our itoa code and nullwrite code will be used again. We will also need the pow macro. Before we blindly throw these code macros together, let's spend the time to rewrite our macros into proper functions: itoa, pow, and nullwrite, with frames that we can call whenever needed and without worry of loosing register values upon calling. We are basically building a primitive static library and using it in the linking phase.
Before building the final executable file, create a makefile that will automatically assemble the source files and link the object files together.
Below is an additional guide in the form of a chart of recursive calls.
start factorial factorial factorial factorial factorial factorial factorial factorial factorial itoa product pow pow pow pow pow pow pow pow pow pow pow nullwrite string Coding Template
main.s
Program : Factorials A program that calculates and prints the number to stdout. Suggested Register Usage: w: total w: current number Macros for common stack operations Examples: using push and pop to preserve the link register and frame pointer push lr fp pop lr fp push operand macro push xn xn stp xnxnspendm pop operand macro pop xn xn ldp xnxnspendm Factorial main routine Factorial beginning number equ maxnum, global start text start: create a frame for main and preserve LR and FP registers on the stack prepare and call the factorial function with maxnum as argument in X call the itoa function to convert the integer result to a char string call the nullwrite function to write the string to the screen restore the LR and FP registers from the stack frame exit back to the operating system Factorial function subroutine factorial: create a new frame and preserve the LR and FP registers on the stack push the caller's argument Xrecursion value onto the stack frame test recursion value for ; on branch to factexit prepare to call factorial function again with value minus load the caller's argument recursion value from the stack frame multiply the called function's returned value X with the argument factexit: restore the LR and FP registers from the stack frame return back to the caller function data outstr: fill bytes or character spaces for holding the string buffer
makefile
# Define the object files you need for the final executable OBJS main.o itoa.o # The next line uses which is a wildcard char to refer to all s and o files o : s # $ source file, $@ output file as $g o $@ # build the main executable main: $OBJS ld o main $OBJS
Expected Output
username@test:~cslabprog$ ls itoa.s main.s makefile write.s username@test:~cslabprog$ make as o itoa.o itoa.s as o main.o main.s as o write.o write.s ld o main itoa.o main.o write.o username@test:~cslabprog$ main username@test:~cslabprog$ echo $ username@test:~cslabprog$
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
