News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Fibinocci integer spacing problem

Started by infoMASM, January 25, 2014, 07:29:01 PM

Previous topic - Next topic

infoMASM

Hello everyone!
I have been able to write code that asks a person to enter the number of Fibonacci terms they want to be displayed and they can only choose from 1 to 46. I have been able to do this but i need to calculate and diplay all the numbers with results displayed 5 terms per line. So when I find 10 Fibonacci numbers I get this:
1   1   2   3   5   8   13   21   34   55
and I need it to look like this:
1   1   2   3   5
8   13   21   34   55

Can anyone help me fix this problem? I think I need a second loop but am not sure how to go along doing this. I am new to MASM so please let me konw if you can help.
Thank you.

CODE:

INCLUDE Irvine32.inc

FIBSTERM = 47

.data
myMssg BYTE "Whaoo Fibonacci",0dh,0ah,0
introMssg BYTE "Hello, this program enters the number of fibinocci numbers you wanna see.",0dh,0ah,0
introMssg2 BYTE "You must enter an integer in the range [1..46]. Have fun!",0dh,0ah,0
askFib BYTE "Enter the number of Fibonacci terms to be displayed:",0dh,0ah,0
findName BYTE "What is your name?",0dh,0ah,0
userName BYTE 33 DUP(0)
greetUser BYTE "Nice to meet you ",0
greetUser2 BYTE " ,you are great!",0dh,0ah,0
invalidNum BYTE "That is an invalid number, please choose in rang eof [1...46]",0dh,0ah,0
exitInfo BYTE "Thank you, hope you enjoyed your Fibinocci numbers!",0dh,0ah,0
exitInfo2 BYTE "Goodbye ",0
exitInfo3 BYTE ", have a great day.",0dh,0ah,0


fibNum DWORD ?
dNum DWORD ?
remainNum DWORD ?
count DWORD 1

.code
main PROC

call Clrscr

;Display my message (name and assignment)
mov edx, OFFSET myMssg
call WriteString
call Crlf

;Display Introduction message and rules for range
mov edx, OFFSET introMssg
call WriteString
mov  edx, OFFSET introMssg2
call WriteString
call Crlf

;Get users name
mov edx, OFFSET findName
call WriteString
mov edx, OFFSET userName
mov ecx, 32
call ReadString
call Crlf

;Greet the user
mov edx, OFFSET greetUser
call WriteString
mov edx, OFFSET userName
call WriteString
mov edx, OFFSET greetUser2
call WriteString
call Crlf

;Get the number of fibinocci the user wants
input:
mov edx,OFFSET askFib
call WriteString
call ReadInt
mov fibNum, eax
call Crlf

;Check to see if they entered appropriate integer
decide:
mov eax, fibNum
cmp eax, FIBSTERM
jge greater
jl initial

greater:
mov edx, OFFSET invalidNum ;displays when incorrect
call WriteString
call Crlf
jmp input
call Crlf

;Start the fibonacci sequence
initial:
mov ebx, 1
mov edx, 0
mov ecx, fibNum

;Loop to calculate and print the Fibonacci sequnce
L1:
mov eax, ebx
add eax, edx
mov ebx, edx
mov edx, eax
call WriteDec
mov al, TAB
call WriteChar
loop L1
call Crlf

L2:



;Exit message to the user
mov edx, OFFSET exitInfo
call WriteString
mov edx, OFFSET exitInfo2
call WriteString
mov edx, OFFSET userName
call WriteString
mov edx, OFFSET exitInfo3
call WriteString
call Crlf

exit
main ENDP

END main

jj2007

Welcome to the Forum :icon14:

;Loop to calculate and print the Fibonacci sequnce
mov esi, 4
L1:
mov eax, ebx
add eax, edx
mov ebx, edx
mov edx, eax
call WriteDec
mov al, TAB
call WriteChar
dec esi
.if Sign?
mov al, 13
call WriteChar
mov al, 10
call WriteChar
mov esi, 5
.endif
loop L1
call Crlf
L2:

Gunther

Hi infoMASM,

welcome to the forum.

Gunther
You have to know the facts before you can distort them.

infoMASM

Thank you all for the wlcome! Honestly really enjoy this website, a lot of great people and a lot of great info.

Thank you for the response on how to fix the problem!!
jj2007 may I ask what exactly is going on with the .if and the rest of the code to fully understand what is happening?

Thank you!

infoMASM

I hate to be annoying but with this code if the user enters 11 as wanting to know the 11 fibonacci numbers it comes out like this:
1   1   2   3   5
8   13  21 34 55   89

when it should be:
1  1  2  3  5
8  13  21  34  55
89

Any advice?

Thank you!

jj2007

Quote from: infoMASM on January 26, 2014, 08:34:12 PM
jj2007 may I ask what exactly is going on with the .if and the rest of the code to fully understand what is happening?

Perhaps you'll find something about .if and Sign? in the Masm documentation? Or you prefer that we read it for you?

FORTRANS

Quote from: infoMASM on January 26, 2014, 08:34:12 PM
jj2007 may I ask what exactly is going on with the .if and the rest of the code to fully understand what is happening?

Hi,

   Generate a listing file.  When you look at the listing, you will
see what code is generated by the directives and macros.  And
that will be exactly what is going on with the rest of the code.

Cheers,

Steve