Reputation:
So I want to break the break "The nth term in Fibonacci Series is " string into two:
"The " and "th term in Fibonacci Series is ", print the first string, then print the value of n (say using WriteInt), then print the second string.
But I can't seem to get the right output, any tips on how to break them into two?
This is my output now (let's say I entered 4 as input):
Display Fibonacci Series
Enter number of fibonacci series terms: 4
Fibonacci Series is
0
1
1
2
Sum of Fibnoacci Series = 4
Press any key to continue...
How can I change it to this input that I want:
Display Fibonacci Series
Enter the number in fibonacci sequence number: 4
The 4th sequence in Fibonacci Series is
2
Press any key to continue...
Also right now the formula in this code is fibonacci, how do I change it to new formula like this;
f(n-2)/2 + f(n-1) * 2
This is my code:
INCLUDE Irvine32.inc
.DATA
; insert variables here
dPrevious DWORD 0
dCurrent DWORD 1
dNext DWORD 1
dNumberOfTerms DWORD 0
dSum DWORD 0
msg1 BYTE "Enter the Fibonacci sequence number : ",0 ; A null-terminated string
msg2a BYTE "The ", 0
msg2b BYTE "th term in Fibonacci Series is ", 0
msg3 BYTE "th term in Fibonacci Series is ", WriteString
msg4 BYTE "Error: Fibonacci Series must have at least 2 numbers",0
.CODE
; insert all executable statements here
main PROC
mov edx, OFFSET msgFibonacciSeries ; "Display Fibonacci Series and Sum of Fibonacci Series in Assembly Language "
call WriteString ; Display a null-terminated string.
call Crlf ; Writes an end-of-line sequence to the console window.
mov edx, OFFSET msg1 ; "Enter Number of Fibonacci Series Terms : "
call WriteString ; Display a null-terminated string.
call ReadInt ; Input a 32-bit signed decimal integer
; from the keyboard and save in EAX.
mov dNumberOfTerms, eax
mov ebx,dWords
cmp ebx,1 ; compare EBX and 1
jle Error ; jump if Less than or equal (<=)
mov edx, OFFSET msg2a ; "Fibonacci Series is "
call WriteString
mov edx, OFFSET msg2b
call Crlf ; Writes an end-of-line sequence to the console window.
mov eax,dPrevious
call WriteDec ; Display an unsigned 32-bit integer value
; in EAX in decimal format.
call Crlf
mov ecx,dNumberOfTerms ; ECX initialize with Number Of Terms
sub ecx,2 ; Exclude First Two Terms
cmp ecx,0 ; compare EBX and 0
je showSum ; jump if equal (=)
L1:
mov ebx,dPrevious
add ebx,dCurrent
mov dNext,ebx
mov eax,DNext
cmp ecx,1
jne ffff
;add dSum,eax
ffff:
loop L1
showSum:
jmp next
Error:
mov edx, OFFSET msg4 ; "Error: Fibonacci Series Contain at least 2 number of terms"
call WriteString
cmp eax,1
call Crlf
next:
call WaitMsg ; Displays a message and waits for a key to be pressed.
exit ; The exit statement (indirectly) calls a predefined
; MS-Windows function that halts the program.
main ENDP ; The ENDP directive marks the end of the main procedure.
END main ; The END directive marks the last line of the program to be assembled.
Upvotes: 0
Views: 879
Reputation: 58284
To split your message, make it into two data items:
msg2a BYTE "The ", 0
msg2b BYTE "th term in Fibonacci Series is ", 0
Then in the code:
mov edx, OFFSET msg2a ; "Fibonacci Series is "
call WriteString
; ...
; Code here to write out the value of `n`
; ...
mov edx, OFFSET msg2b
call Crlf
This won't be so elegant if n
is 1 or 2 because it will say, 1th
or 2th
. I will leave it as an exercise to fix that if you want to. You can break your message up further for that if needed. For example:
msg2a BYTE "The ", 0
msg2b BYTE "st", 0
msg2c BYTE "nd", 0
msg2d BYTE " term in Fibonacci Series is ", 0
And adjust according to the value of n
.
As far as modifying the formula from f(n-1) + f(n-2)
to f(n-1)*2 + f(n-2)/2
, locate where where f(n-1)
and f(n-2)
are determined and use the fact that *2
is the same as a single bit shift left, and /2
is the same as a single bit shift right. It's left as another exercise. If you look things up, it should be quite simple. :)
Upvotes: 1