Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i want to know whether my code is correct and also need to know whether the code is right? 5 points The fibonacci subroutine has
i want to know whether my code is correct and also need to know whether the code is right?
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
here it prompts for input after typing the input nothing happens
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
BRnzp RESTORER ; Return from subroutine
BASECASEZERO
LD R ZERO ; F
BRnzp RESTORER ; Return from subroutine
BASECASEONE
LD R ONE ; F
BRnzp RESTORER ; Return from subroutine
RESTORER
LD R SAVER ; Load return address from stack
LD R SAVER ; Restore saved R
RET ; Return from subroutine
; Data
PROMPT STRINGZ "Please enter a number n: ; Prompt message
RESULTMSG STRINGZ Fn ; Result message
ASCIIOFFSET FILL # ; ASCII code for
INPUT BLKW # ; Storage for input
ZERO FILL # ; Constant for base case
ONE FILL # ; Constant for base case
SAVERBLKW # ; Storage for saving R
SAVERBLKW # ; Storage for saving R
RESULTLOC FILL x ; Address to store the result
END
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