Answered step by step
Verified Expert Solution
Link Copied!

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?
5 points
The fibonacci subroutine has a properly implemented general case (F(n)= F(n-1)+ F(n-2))
5 points
The fibonacci subroutine has properly implemented base cases (F(0)=0 and F(1)=1)
5 points
The first fibonacci subroutine call returns the correct output to main() from n =0 to n =9
5 points
The runtime stack is properly utilized and the total result of F(n) is stored in mains return value
location at x5013
5 points
Your assembly program outputs the Fibonacci sequence number n correctly as formatted below:
Please enter a number n: 9
F(9)=34
5 points
Total
30 points
here it prompts for input after typing the input nothing happens
.ORIG x3000
; Prompt user for input
LEA R0, PROMPT ; Load address of the prompt message
PUTS ; Display the prompt
GETC ; Read input character from user
OUT ; Output the character (for confirmation)
ADD R0, R0, #0 ; Move character to R0
; Convert ASCII to integer
LD R2, ASCII_OFFSET ; Load the ASCII offset constant (48)
NOT R2, R2 ; Compute -48
ADD R2, R2, #1 ; Complete 2's complement to get -48
ADD R1, R0, R2 ; Convert ASCII code to integer (R0-48)
; Call Fibonacci subroutine
JSR FIBONACCI
; Store result in memory location x5013
LEA R2, RESULT_LOC
STR R0, R2, #0
; Display the result
LEA R0, RESULT_MSG ; Load address of result message
PUTS ; Display result message
; Convert result from integer to ASCII
ADD R0, R0, R2 ; Convert result from integer to ASCII (R0+48)
OUT ; Output the result
HALT
FIBONACCI
ST R7, SAVE_R7 ; Save return address
ST R1, SAVE_R1 ; Save R1(input value)
; Base case: if n ==0
LD R2, ZERO ; Load zero
ADD R3, R1, R2 ; Compare input with 0
BRz BASE_CASE_ZERO
; Base case: if n ==1
LD R2, ONE ; Load one
ADD R3, R1, R2 ; Compare input with 1
BRz BASE_CASE_ONE
; General case: F(n)= F(n-1)+ F(n-2)
ADD R1, R1, #-1 ; Compute n-1
JSR FIBONACCI ; Recursive call for F(n-1)
ADD R2, R0, #0 ; Save result of F(n-1) in R2
LD R1, SAVE_R1 ; Restore original input value
ADD R1, R1, #-2 ; Compute n-2
JSR FIBONACCI ; Recursive call for F(n-2)
ADD R0, R0, R2 ; Add results of F(n-1) and F(n-2)
BRnzp RESTORE_R7 ; Return from subroutine
BASE_CASE_ZERO
LD R0, ZERO ; F(0)=0
BRnzp RESTORE_R7 ; Return from subroutine
BASE_CASE_ONE
LD R0, ONE ; F(1)=1
BRnzp RESTORE_R7 ; Return from subroutine
RESTORE_R7
LD R7, SAVE_R7 ; Load return address from stack
LD R1, SAVE_R1 ; Restore saved R1
RET ; Return from subroutine
; Data
PROMPT .STRINGZ "Please enter a number n: " ; Prompt message
RESULT_MSG .STRINGZ "F(n)=" ; Result message
ASCII_OFFSET .FILL #48 ; ASCII code for '0'
INPUT .BLKW #1 ; Storage for input
ZERO .FILL #0 ; Constant for base case 0
ONE .FILL #1 ; Constant for base case 1
SAVE_R7.BLKW #1 ; Storage for saving R7
SAVE_R1.BLKW #1 ; Storage for saving R1
RESULT_LOC .FILL x5013 ; Address to store the result
.END

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

Explain the concept of institutionalized bias.

Answered: 1 week ago

Question

List behaviors to improve effective leadership in meetings

Answered: 1 week ago