Search code examples
assemblynumbersfibonaccitasm

Printing fibonacci number in TASM assembly


In university we started learning TASM assembly language. The point is, I need help because I'm completely lost on my assignment. I need to scan a number from keyboard and print fibonacci's n-number. Example: you type in 6, program prints out 8.

Here's my unfinished code:

.model small
.stack 100h

.data 
    number db 255

.code
start:
    mov dx, @data
    mov ds, dx
    mov al, 0
    mov bl, 1

.readline:
    mov ah, 0Ah
    mov dx, offset number
    mov cx, dx;
    int 21h

.fibonacci:
    add al, bl
    mov dl, al
    mov al, bl
    mov bl, dl

    loop .fibonacci
    int 21h
.writeln
    mov ah, 9
    mov dx, dl
    int 21h


end start   

After this, I just don't know what should I do or if there are any mistakes made. Could anyone give me advice what I need to do next?


Solution

  • Here is a PDF manual with some code examples needed to implement user input and output.

    Below is a snippet of untested code to give you an idea of how to calculate a Fibonacci sequence without recursion.

        mov  cx, 6
        mov  ax, 0    ;a = 0
        mov  bx, 1    ;b = 1
    fib:
        mov  dx, ax 
        add  dx, bx   ;sum = a + b
        mov  ax, bx   ;a = b
        mov  bx, dx   ;b = sum
        loop fib
    

    Take note that you will have to set the Count register (CX) to the given user input.

    Also keep in mind that you are using 32 bit registers, so the biggest Fibonacci number you will be able to store in the register is 46th.