I need help printing 5 Fibonacci numbers per line. The user is asked how may numbers would they want printed and if the user indicates they want 15 numbers printed then it should look like this:
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
I have the Fibonacci numbers printed out correctly but can't get it to print 5 numbers per line then make a new line and print the next 5 numbers. I tried implementing a for loop and I put the code as comments. I know I need to divide the counter value (which it's initial value is 1) 5 which is counter value/5. If the remainder is not zero then it should increment. Once the counter value is greater than the requested Fibonacci number then the loop terminates.
I'm using the kip irvine library and it's MASM for x86 processors. Please help!
INCLUDE Irvine32.inc
;defined constant for upper limit of Fibonacci terms
FIBSTERM = 47
.data
caption db "Fibonacci Numbers", 0 ;Message Box (Do something incredible for me!)
HelloMsg BYTE "Welcome to Fibonacci Number for Fun!", 0dh, 0ah
BYTE "Click OK to Begin", 0
title1 BYTE " Fibonacci Numbers", 0
title2 BYTE " Programmed by Riam S. Kidd", 0
rules1 BYTE "Enter the number of Fibonacci terms to be displayed", 0
rules2 BYTE "Give the number as an integer in the range [1 .. 46].", 0
askName BYTE "What's your name? ", 0
userName BYTE 33 DUP(0) ;string to be entered by user
greetUser BYTE "Nice to meet you, ", 0
askFibnum BYTE "How many Fibonacci terms do you want? ", 0
invalidNum BYTE "That's an invalid number. Please enter a number in the range of 1 and 46." ,0
goodBye BYTE "Have a Nice Day! Good-Bye ", 0
exclaim BYTE " ! ", 0
fibsNum DWORD ? ;get number of Fib terms to display from user
dNum DWORD ?
remainNum DWORD ?
.code
main PROC
;Message Box Display
mov ebx,OFFSET caption
mov edx,OFFSET HelloMsg
call MsgBox
;Display title 1: Fibonacci Numbers
mov eax, green + (black*16) ;sets text color to green
call SetTextColor
mov edx,OFFSET title1 ;set up for call to WriteString
call WriteString
call Crlf
call Crlf
;Display title 2: programmer's name
mov eax, white + (black*16) ;sets text color back to white
call SetTextColor
mov edx,OFFSET title2
call WriteString
call Crlf
call Crlf
;Display instructions line 1
mov edx,OFFSET rules1
call WriteString
call Crlf
;Display instructions line 2
mov edx,OFFSET rules2
call WriteString
call Crlf
call Crlf
;Get user name
mov edx, OFFSET askName
call WriteString
mov edx, OFFSET userName
mov ecx, 32
call ReadString
;Greet user by name
mov edx, OFFSET greetUser
call WriteString
mov edx, OFFSET userName
call WriteString
call Crlf
;get number of Fib terms to display from user
input:
mov edx,OFFSET askFibnum
call WriteString
call ReadInt
mov fibsNum, eax
call Crlf
;validate user input
decide:
mov eax, fibsNum
cmp eax, FIBSTERM ;check condition that input is not greater than 47
jge greater ;out of bounds input
jl initial ;go to intial if Fib terms are acceptable
greater:
mov edx, OFFSET invalidNum ;displays message if user input is invalid and reminds user to enter correct number within range
call WriteString
call Crlf
call Crlf
jmp input ;prompts user to start again
call Crlf
;initializing Fibonacci sequence -- if this is not in place it doesn't calculate correctly
initial:
mov eax, white + (green*16); white on green background -- highlights Fibonacci printout
call SetTextColor
mov ebx, 1 ; initial setup
mov edx, 0
mov ecx, fibsNum ; get number of Fib
;loop calculating and printing Fibonacci sequence
L1:
mov eax, ebx
add eax, edx
mov ebx, edx
mov edx, eax
call WriteDec ;print out Fibonacci numbers
mov al, TAB ;horizontal tab
call WriteChar ;write the tab displays it aligned
loop L1 ;subtract 1 from ecx, if ecx !=0, goes to L1. reach 0 terminates loop
mov eax, white + (green*16) ;white on green background
call SetTextColor
;Put code as comment because it won't make new line after 5 characters.
;I tried but failed to make it work.
;L2:
;mov eax, 1 ;define counter variable has initial value of 1.
;cdq
;mov ebx, 5 ; divide by 5
;div ebx
;inc eax ; remainder incremented if != 0
;mov dNum, eax
;mov remainNum, edx
;cmp remainNum, 0 ;if remainer is not equal to 0 then counter is incremented
;cmp eax, fibsNum ;checks if ebx (counter is greater than fibsNum. If it is then loop terminates. If less then loops.
;jle L2
mov eax, white + (black*16) ; white on black background -- goes back to default color and background
call SetTextColor
call Crlf
call Crlf
;Farewell Message
mov edx, OFFSET GoodBye
call WriteString
mov edx, OFFSET userName
call WriteString
mov edx, OFFSET exclaim
call WriteString
call Crlf
call Crlf
exit ; exit to operating system
main ENDP
END main