Question: 1 Recursion in Assembly Programming Lab # 5 CEG 3 3 1 0 / 5 3 1 0 : Computer Organization PURPOSE In this lab
Recursion in Assembly
Programming Lab #
CEG : Computer Organization
PURPOSE
In this lab you will learn how to implement recursive subroutines in assembly.
ASSIGNMENT
You will be implementing recursive subroutines in assembly. A common exercise in recursive
programming is displaying the Fibonacci Sequence. The Fibonacci sequence is defined as:
Fn Fn Fn
Where, F and F
For example, the first numbers in the Fibonacci Sequence are:
F F F F F F F
Notice how F F F and how F F F
You will have to implement a recursive subroutine that returns the desired number in the Fibonacci
Sequence. An example output of your completed lab should look like the following:
Please enter a number n:
F
Another example is:
Please enter a number n:
F
This can be accomplished by implementing a recursive function, with a properly implemented general
and base case. The general case would be Fn Fn Fn and the base cases are F and F
Notice, in order to calculate Fn you must call Fn and Fn this will be implemented using
recursion. For a value of n your subroutines will be call each other in this manner:
fibonacci calls fibonacci fibonacci
fibonacci calls fibonacci fibonacci
fibonacci returns to fibonacci and fibonacci
fibonacci returns to fibonacci
fibonacci returns to fibonacci
fibonacci returns to main
main displays F to the user
For reference, to implement this in C code, the fibonacci function would look like:
int fibonacciint n
ifn
return n;
else
return fibonaccin fibonaccin;
IMPLEMENTATION
Write your assembly code in the labasm file provided:
Implement the main function in assembly.
Implement the fibonacci function as an LC assembly subroutine. This function must be
recursive, that is it calls itself until the base case is reached.
Maintain a proper runtime stack. This runtime stack should allow for multiple fibonacci
subroutine calls, passing inputs to each function call, and returning outputs properly without
losing any data or corrupting data.
GRADING
Main takes in a user input for the value n
points
The fibonacci subroutine has a properly implemented general case Fn Fn Fn
points
The fibonacci subroutine has properly implemented base cases F and F
points
The first fibonacci subroutine call returns the correct output to main from n to n
points
The runtime stack is properly utilized and the total result of Fn is stored in mains return value
location at x
points
Your assembly program outputs the Fibonacci sequence number n correctly as formatted below:
Please enter a number n:
F
points
Total
points
For this question i implemented it and got a code but this stucks at the point where i give a number for input
i will give my code here please check and get back to me whether the code is right or not
ORIG x
; Prompt user for input
LEA R PROMPT ; Load address of the prompt message
PUTS ; Display the prompt
GETC ; Read input character from user
OUT ; Output the character for confirmation
ADD R R # ; Move character to R
; Convert ASCII to integer
LD R ASCIIOFFSET ; Load the ASCII offset constant
NOT R R ; Compute
ADD R R # ; Complete s complement to get
ADD R R R ; Convert ASCII code to integer R
; Call Fibonacci subroutine
JSR FIBONACCI
; Store result in memory location x
LEA R RESULTLOC
STR R R #
; Display the result
LEA R RESULTMSG ; Load address of result message
PUTS ; Display result message
; Convert result from integer to ASCII
ADD R R R ; Convert result from integer to ASCII R
OUT ; Output the result
HALT
FIBONACCI
ST R SAVER ; Save return address
ST R SAVER ; Save Rinput value
; Base case: if n
LD R ZERO ; Load zero
ADD R R R ; Compare input with
BRz BASECASEZERO
; Base case: if n
LD R ONE ; Load one
ADD R R R ; Compare input with
BRz BASECASEONE
; General case: Fn Fn Fn
ADD R R # ; Compute n
JSR FIBONACCI ; Recursive call for Fn
ADD R R # ; Save result of Fn in R
LD R SAVER ; Restore original input value
ADD R R # ; Compute n
JSR FIBONACCI ; Recursive call for Fn
ADD R R R ; Add results of Fn and Fn
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
