News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Fibonacci numbers

Started by Pro32, March 04, 2005, 03:54:50 AM

Previous topic - Next topic

Pro32

Does anyone have examples of a program or algorithm that calculate fibonacci numbers?

sluggy

Each step in the fibonacci series is the sum of the two numbers preceding it, like this:

0, 1, 1, 2, 3, 5, 8, 13.....

This should do it (once again, straight of the top of my head :U ):


   mov eax, 0
   mov ebx, 1
   mov ecx, <number of steps> + 1

loopStart:
   push ebx
   add ebx, eax
   <output your number from ebx>
   pop eax
   dec ecx
   jnz loopStart


We don't usually answer homework type questions, but because your tutor is not really teaching you well....
   

Pro32

Thank-You for the code. I'm not going to use your code. This will give me an idea to rewrite it the way I want it. I'm trying to write it with the mul (multiply instruction). Yes my teacher isn't teaching, doesn't mean I can't learn it myself with the help of this community. I understand that you usually don't give help to students, but I don't see why you can't give help to those who shows effort. Thanks again.

Quote from: sluggy on March 04, 2005, 06:36:00 AM
Each step in the fibonacci series is the sum of the two numbers preceding it, like this:

0, 1, 1, 2, 3, 5, 8, 13.....

This should do it (once again, straight of the top of my head :U ):


   mov eax, 0
   mov ebx, 1
   mov ecx, <number of steps> + 1

loopStart:
   push ebx
   add ebx, eax
   <output your number from ebx>
   pop eax
   dec ecx
   jnz loopStart


We don't usually answer homework type questions, but because your tutor is not really teaching you well....
   

mnemonic

Quote from: Pro32 on March 04, 2005, 05:13:50 PM
[...]
I understand that you usually don't give help to students, but I don't see why you can't give help to those who shows effort. Thanks again.

That's terribly wrong.
What sluggy meant is that the people here won't do any homework for anyone being just lazy. It doesn't have anything to do with whether you're student or not. In general homework is for learning/training purposes and should be solved on your own. But you can only solve your homework if your teacher teached you something. Since he didn't do that you're free to ask in here and help is granted for you.
Just don't get it wrong :wink
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

sluggy

Quote from: Pro32 on March 04, 2005, 05:13:50 PMI'm trying to write it with the mul (multiply instruction).
Huh? What do you mean? Fibonacci numbers are the SUM, not the PRODUCT, of the preceding numbers. Which means it is wrong to use the MUL instruction to try to calculate them.

Pro32

He wants me to calculate the Fibonacci series. Something about the value will exceed 225, therefore I need to use the form of the mul instruction.

Quote from: sluggy on March 04, 2005, 08:38:56 PM
Quote from: Pro32 on March 04, 2005, 05:13:50 PMI'm trying to write it with the mul (multiply instruction).
Huh? What do you mean? Fibonacci numbers are the SUM, not the PRODUCT, of the preceding numbers. Which means it is wrong to use the MUL instruction to try to calculate them.

roticv

You would not be using mul for fib number I think.

mnemonic

Quote from: Pro32 on March 05, 2005, 02:13:58 AM
He wants me to calculate the Fibonacci series. Something about the value will exceed 225, therefore I need to use the form of the mul instruction.

I agree with the others, that just doesn´t make any sense...
Also I can´t understand what it has to do with the value exceeding 225. Is that some kind of magic number?
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way


sluggy

Ahh, now i understand more.

Pro32,
the series exceeding the value 225 does not change at all how you calculate it. If you are going to be requesting stuff here, you need to be more precise with your request. If your tutor is telling you to use a multiplication method, but then not giving you the formula, then either he is teaching incorrectly and should have his ass fired, or you are not listening in class  :naughty: In any case, this sort of thing can't go on, otherwise we will be doing the whole course for you. If the tutor is not teaching you properly, then do something about it.

If it is you tutor's intention that you show you can calculate the series, and you print out the first n numbers to prove it, then the simple addition that i showed will do the trick. If however you are supposed to print out the number at n position in the series, then that is where the multiplication method will help you immensely as you can calculate an arbitrary value without having to generate the whole series.

And don't think i am grumpy at you personally. As a programmer i get annoyed when someone gives me an inspecific spec, and then tells me the answer i give them is wrong  ::) :wink

Pro32

Yes I agree he should get his ass fired. But nothing can be done. Past students have tried but failed. Your right, I emailed him with the problem and he said it was his mistake.



Quote from: sluggy on March 05, 2005, 10:56:58 PM
Ahh, now i understand more.

Pro32,
the series exceeding the value 225 does not change at all how you calculate it. If you are going to be requesting stuff here, you need to be more precise with your request. If your tutor is telling you to use a multiplication method, but then not giving you the formula, then either he is teaching incorrectly and should have his ass fired, or you are not listening in class  :naughty: In any case, this sort of thing can't go on, otherwise we will be doing the whole course for you. If the tutor is not teaching you properly, then do something about it.

If it is you tutor's intention that you show you can calculate the series, and you print out the first n numbers to prove it, then the simple addition that i showed will do the trick. If however you are supposed to print out the number at n position in the series, then that is where the multiplication method will help you immensely as you can calculate an arbitrary value without having to generate the whole series.

And don't think i am grumpy at you personally. As a programmer i get annoyed when someone gives me an inspecific spec, and then tells me the answer i give them is wrong  ::) :wink

teaz14

I must be in the same class as pro32. This is a nightmare! The teacher is not teaching us. He's giving partial information and expecting us to be able to figure it out on our own. His instructions are very ambiguous:

"limit the loop to 15 iterations.Since the list will contain entries which exceed 255 in value, you will need to use a form of the mul instruction which allows for this"

I have no idea why we would need to use the mul instruction or what the 255 value has to do with the mul instruction. The code for the fibonacci sequence should be fairly straight forword using addition, but the mul instruction and the value 255? I'm lost :dazzled: Any assistance would be greatly appreciated.

sluggy

The only importance about the number 255 is that it is the maximum number that will fit into a byte. Using the code i posted above will totally negate any effect this may have had because i did everything in DWORDs. You really should point out to your tutor that he is talking crap in this case. If you do it in front of the class then it should be amusing  :bdg

teaz14

My instructor acknowledged  that the mul instruction was a mistake. It's not needed since the numbers in the Fibonacci series are added. With this restriction lifted I was able to successfully complete the program :U Thanks to everyone for assisting :bg