Search code examples
assemblymasmfibonacciirvine32

Assembly print to file from array


I need this program to write out a file with the first 47 values of the Fibonacci series calculated. My procedure correctly displays the 47 entries using procedures included from a provided library however it does not print them out to the file.

Im fairly sure that its storing the array in the esi, but my fib.bin file only has one entry and its not the beginning of the array. I'm fairly certain all I need to do is use the esi, but I cant figure it out, Thanks in advance.

TITLE Fibonacci Numbers                     (FibNums.asm)

INCLUDE Irvine32.inc

.data
fileHandle DWORD ?
filename BYTE "fib.bin",0

FIB_COUNT = 47
array DWORD FIB_COUNT DUP(?)

.code
main PROC

    ;Creates the file
    mov  edx,OFFSET filename
    call CreateOutputFile
    mov  fileHandle,eax

    ;Generates the array of values
    mov esi,OFFSET array
    mov ecx,FIB_COUNT
    call generate_fibonacci

    ;Write out to file
    mov eax,fileHandle
    mov edx,OFFSET array
    mov ecx,SIZEOF array
    call WriteToFile

    ;Close the file
    mov eax,fileHandle
    call CloseFile


    exit
main ENDP

;--------------------------------------------------
generate_fibonacci PROC USES eax ebx ecx
;
;Generates fibonacci values and stores in an array
;Receives: ESI points to the array, ECX = count
;Returns: Nothing
;---------------------------------------------------

    mov eax,1
    mov ebx,0

L1: add eax,ebx
    call    WriteDec
    call Crlf

    mov [esi],eax
    xchg    eax,ebx
    loop L1
    ret
generate_fibonacci ENDP

END main

Solution

  • You have to increment esi:

    ...
    L1: add eax,ebx
        call WriteDec
        call Crlf
    
        mov [esi], eax
        xchg eax, ebx
        add esi, 4          ; move forward 4 Bytes (4*8 bits) = 1 dword (1*32 bits)
        loop L1
    ...