0

my microproccessor lab work asks me

Write a code that prints the first twenty “Fibonacci Numbers” to any register. (You don’t have to print these numbers to screen and you must use loop structure)

And I wrote this simple code in emu8086 emulator:

name "fibo-series"

org 100h

mov ax, 1
mov bx, 1


mov cx, 9

fibo: add ax, bx
      add bx, ax
loop fibo


ret

And it works well, I mean it prints all I want. But when I looked at other examples of fibonacci series on internet the code samples are much bigger than mine. Did I do something wrong? I mean is this enough for my situation? I want your opinions. Thanks.

Maksat Yalkabov
  • 548
  • 1
  • 8
  • 19
  • 2
    You didn't do anything wrong. But most programs are larger because they want to print (e.g. to screen) or store (to disk or in an array) the values they found. Most people don't read registers. ISTM that this program does exactly what the assignment asked, though. – Rudy Velthuis Mar 15 '16 at 10:21
  • 1
    A lot of times Fibonacci is used as an example to teach recursion. It's a bad example, IMO, because it's *much* faster to compute iteratively. A recursive implementation needs "memoizing" to cache recurring value to be in the same O(n) complexity class as a simple obvious loop. Well done for writing code that doesn't suck! I wrote an answer a while ago [with optimized loops that store the Fibonacci results into an array](http://stackoverflow.com/a/32661389/224132). The text is kind of messy and long, but the code is good IMO and might make interesting reading :) – Peter Cordes Mar 16 '16 at 04:47

0 Answers0