0

I want to generate the 1st N fibonacci numbers in x86 asm using the Irvine Library. That means whenever I will give N = 5, it should print : 0,1,1,2,3, But whenever I run it suppose with N =5 then it gives output as : 0,1,2,3,4.

Below is my program

TITLE MASM Irvine
INCLUDE Irvine32.inc
.data
    message1 BYTE "The first ",0
    message2 BYTE " numbers in the Fibonacci sequence are the following.",0
    N DWORD 10
    intialVal WORD 0,1

.code
main PROC

    call Clrscr ; clears screen

    mov edx,OFFSET message1
    call WriteString            ; display message1
    mov eax, N
    call WriteDec               ; display num
    mov edx,OFFSET message2
    call WriteString            ; display message2
    call Crlf                   ; new line

    mov eax,0
    mov bx, [intialVal]
    mov cx, [intialVal + 2]
StartLoop:
    mov edx,0       ;setting edx to 0 every time
    add dx,bx       ;adding bx to dx
    add dx,cx       ;adding cx to dx
    call WriteDec   
    call Crlf       ;trying to print dx
    mov bx,cx       ;moving numbers. so now cx goes to bx
    mov cx,dx       ;dx goes to cx 
    inc eax         ;increment eax (as a counter)
    cmp eax,N       ;compare to variable N
    jne StartLoop   ;if eax = N exit loop, else jump to start of loop

    exit
main ENDP
END main
Jester
  • 56,577
  • 4
  • 81
  • 125
  • 1
    `WriteDec` takes the number to print in `eax` not `edx`. So you are printing the counter. PS: any reason why you use 16 bit registers? – Jester Oct 27 '20 at 23:20
  • So would I have to have the contents inside the ```eax``` register? No reason, I'm a sophmore in college and our prof. kind of just through this program at us so i've been trying to research everything on my own – thetacoman Oct 27 '20 at 23:31
  • 1
    @Jester THANK YOU. That sentence was all i needed and i figured it out. Thanks a bunch :D – thetacoman Oct 27 '20 at 23:40

0 Answers0