1

How would I write a program that will display the first 24 values in the Fibonacci series in assembly language?

If anyone could help me I would greatly appreciate it, I'm confused with the code in assembly.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • What kind of assembly language? – Greg Mar 22 '11 at 19:39
  • Please try to provide more details of what you have tried and what you are confused about instead of simply asking for an answer to what seems to be a homework problem. – quarkdown27 Mar 22 '11 at 19:39

4 Answers4

1

Well, you do it pretty similarly to the way you would in most other languages, something like this:

for loop counter = 1 to 24 do
    next_number = fibonacci(previous, previous2)
    print(next_number)
    previous2 = previous
    previous = next_number

The obvious differences from other languages include:

  1. In this case, your "variables" will all probably be in registers.
  2. You'll probably have to write your own code to convert and print a number.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

I left two blank spaces because the code depends on the system where it's going to run (you didn't specify the compiler and operating system).

I haven't tested the code but I think it will work.

    mov eax, 0         ; first number
    mov ebx, 1         ; second number
                       ; edx will contain the third number (eax + ebx )

    mov ecx, 24 - 2    ; print 24 numbers (don't count the first
                         and second because they are printed in the begining)

    mov edx, eax
    call print_number  ; print the first number

    mov edx, ebx
    call print_number  ; print the second number
fibo:
    mov edx, eax
    add edx, ebx       ; edx = eax + ebx

    call print_number

    ; now we have the third number in edx
    ; eax = 1st, ebx = 2nd, edx = 3rd
    ; to prepare eax and abx for the next iteration, shift the values to the right
    ; eax = 2nd, ebx = 3rd, edx = ?
    mov eax, ebx
    mov ebx, edx

    loop fibo

    ; TO DO: exit program

print_number:
    ; TO DO: edx contains the number, print it
    return

Hope it helps.

Javi R
  • 2,320
  • 1
  • 17
  • 21
  • `edx` is normally a call-clobbered register. If you want to be able to use `printf` or something in `print_number`, you have to assume it will clobber `eax`, `ecx`, and `edx`, but preserve the other registers. So don't use `ecx` for your loop counter either, and [never use the slow `loop` instruction](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) unless you're optimizing for code-size. – Peter Cordes Mar 25 '18 at 07:47
0

A number in Fibonacci series is the sum by the two numbers preceding it. To keep it simple you can store these numbers in an array with the first two element set to 1. esi and edi could point to n-1 and n-2, so fibonacci(n) = [esi] + [edi]] right? In pseudocode it looks like:

fibonacci    DWORD    24 dup (?)

esi = fibonacci(0)       // those are pointers to elements!
edi = fibonacci(1)

for x = 2 to 23
    fibonacci(x) = [esi] + [edi]
    esi += 4             // if you don't like DWORDs change this
    edi += 4
end loop

you can keep x in the ecx register and fibonacci(x) in the eax register.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • You always have `eax` = the last value you stored; you don't need to reload it. And you only need one pointer because addressing modes allow an offset. If you're going to reload at all (instead of keeping values in registers and only storing), you want something like this loop body: `add eax, [edi-8]` / `mov [edi], eax` / `add edi, 4`. Before that block runs, `[edi-4] = eax=Fib(x-1)`, `[edi-8] = Fib(x-2)`. – Peter Cordes Mar 25 '18 at 08:09
-1

try this code it would work as per your requirements this answer makes use of a loop running for 24 times and the label looped is next which first takes the data from ax and bx and then adds it up these all functions are repeated for 24 times till the loop is completed.

   data segment
    org 0000h
    arr dw 17h dup(0000h)
    data ends
    code segment
    assume cs:code, ds:data
    start:  mov ax,data
        mov ds,ax
        mov cx,0018h
        lea si,arr
        inc si
        mov [si],01h
    next:   mov ax,[si-1]
        mov bx,[si]
        add ax,bx
        inc si
        mov [si],ax
        loop next
        mov ah,4ch
        int 21h
     code ends
    end start
    end
sam yo
  • 41
  • 7
  • You only use 1-byte array elements, so this overflows long before Fib(24). – Peter Cordes Mar 25 '18 at 07:31
  • A little more explanation would make this answer more useful to vastly more useres – Kind Stranger Mar 25 '18 at 07:31
  • @KindStranger: Not that much more useful; it's pretty inefficient (reloading instead of using the value in a register, and the [slow `loop` instruction](https://stackoverflow.com/q/35742570)) and doesn't add much to existing answers here. [My answer on another question](https://stackoverflow.com/questions/32659715/assembly-language-x86-how-to-create-a-loop-to-calculate-fibonacci-sequence/32661389#32661389) already has a complete and well-commented function to store the first `n` Fibonacci numbers into an array, where `n` is passed in a register instead of being a compile-time constant. – Peter Cordes Mar 25 '18 at 07:54
  • @PeterCordes if the answer has more explanation, it makes it more underastandable to someone like me who has zero understanding of assembly so perhaps i can work out if it is eve slightly useful or not. This was a comment for the answerer. – Kind Stranger Mar 25 '18 at 07:57
  • @KindStranger: Did you get here from the new-user review queue? The code in this answer isn't terrible, but [Fib(24) = 46368](http://www.wolframalpha.com/input/?i=Fib(24)), and is the largest Fibonacci number that doesn't overflow a 16-bit integer. So 16-bit code can solve the question easily with 16-bit registers, but not 8. I'd remove my downvote if it at least worked, but wouldn't upvote because I don't think it's a *good* solution, especially with no text explaining why they chose to write it that way. Anyway, I think the question is a duplicate of the one I answered. – Peter Cordes Mar 25 '18 at 08:03
  • @PeterCordes, no. I'm not saying you're wrong or that the answer is correct in any way, I just think the answer could be improved with some explanation irrespective of its correctness. – Kind Stranger Mar 25 '18 at 08:49
  • @KindStranger: yes, I agree with that. Answers should never be code-dumps with no text. I commenting to give you a summary of what I thought about its quality (and whether it was a useful addition to this question, or to SO in general), since you said you don't know asm. TL:DR: spending time improving this answer would probably be of limited value, even though there's much room for improvement. Maybe this question was supposed to be about obsolete 16-bit x86, though, given the Fib(24) cutoff. I had been thinking Fib(24) was larger, and needed 32-bit, in my earlier comments. – Peter Cordes Mar 25 '18 at 08:54
  • @Sam: Welcome to Stack Overflow. posting code-dumps on old questions is not very useful. If you have something new to add to an old question, at least include text explaining what your answer is showing (e.g. storing values to an array instead of printing them inside the loop) so people seeing your answer will know whether or not your code might be what they're looking for. Also explain why you implemented it the way you did, and *always* comment your code to show how it works. This is not optional in assembly language, where you can't use descriptive variable names, just registers. – Peter Cordes Mar 25 '18 at 08:57