user1686449
user1686449

Reputation: 1

Assembly program that writes an array to a disk file

I'm trying to write an doubleword array to a disk file using CreateOutputFile, WriteToFile and CloseFile Irvine32 procedures. Here is my code.

INCLUDE Irvine32.inc

.data

     count = 45 
     BUFFER_SIZE = 188
     filename BYTE "Fibonacci.txt",0
     fileHandle DWORD ?
     array DWORD 47 DUP(?)
     num1 = 1
     num2 = 1
     temp1 DWORD ?
     temp2 DWORD ?
.code

     main PROC
     mov edx,OFFSET filename
     call CreateOutputFile

     mov fileHandle,eax
     mov esi,0
     mov array[esi],num1
     mov eax,array[esi]
     mov temp1,eax
     add esi,4 
     mov array[esi],num2
     mov eax,array[esi]
     mov temp2,eax
     add esi,4
     mov ecx, count

L1:

     mov eax,0
     mov ebx,0
     mov eax,temp1
     mov ebx,temp2
     add eax,ebx
     mov array[esi],eax
     mov temp1,ebx
     mov temp2,eax
     add esi,4
     loop L1

     mov eax,fileHandle
     mov edx,OFFSET array
     mov ecx,BUFFER_SIZE
     call WriteToFile

     mov eax,fileHandle
     call CloseFile
     exit
main ENDP

END main

After I debugging it every time, a text file is created successfully, but it turns to be some unrecognizable codes in the text file. I think it should have been the array shown in hexadecimal.

I really don't know where I make mistakes. Please help me! Thanks!

Upvotes: 0

Views: 1903

Answers (2)

rkhb
rkhb

Reputation: 14409

The "unrecognizable codes" are 188 bytes that represent 47 values in the CPU internal format called "DWORD". The file is a memory dump of array DWORD 47 DUP(?). For a human readable format, e.g. decimal strings, they have to be converted - one by one inside the Fibonacci loop (L1) or with a new loop (ladder is shown below as L2). The WinApi contains a function which can be used as conversion routine: wsprintf. Since the Irvine32 library declares this function, it can be used without further circumstances.

Example:

INCLUDE Irvine32.inc

.data

    count = 45
    BUFFER_SIZE = 188
    filename BYTE "Fibonacci.txt",0
    fileHandle DWORD ?
    array DWORD 47 DUP(?)
    num1 = 1
    num2 = 1
    temp1 DWORD ?
    temp2 DWORD ?

    decimalstring BYTE 16 DUP(0)    ; String for WriteFile
    fmt BYTE "%u",13,10,0           ; Format string for wsprintf ("%u\r\n")

.code

main PROC

    mov edx,OFFSET filename
    call CreateOutputFile

    mov fileHandle,eax
    mov esi,0
    mov array[esi],num1
    mov eax,array[esi]
    mov temp1,eax
    add esi,4
    mov array[esi],num2
    mov eax,array[esi]
    mov temp2,eax
    add esi,4
    mov ecx, count

L1:
    mov eax,0
    mov ebx,0
    mov eax,temp1
    mov ebx,temp2
    add eax,ebx
    mov array[esi],eax
    mov temp1,ebx
    mov temp2,eax
    add esi,4
    loop L1

    mov ecx, LENGTHOF array     ; Number of elements (DWORD's) in array
    mov esi, 0                  ; First index
L2:
    push ecx                    ; Preserve loop counter

    ;convert number to string
    push array[esi]             ; Argument for format string
    push OFFSET fmt             ; Pointer to format string ("%d")
    push OFFSET decimalstring   ; Pointer to buffer for output
    call wsprintf               ; Irvine32.inc / Smallwin.inc / User.lib / User.dll
    mov ecx, eax                ; Length of the stored string into ECX for WriteToFile
    add esp, (3*4)              ; CCALL calling function! Adjust the stack.

    mov eax, fileHandle
    mov edx, OFFSET decimalstring
    call WriteToFile

    pop ecx                     ; Restore loop counter
    add esi, 4                  ; Next DWORD
    loop L2

    mov eax,fileHandle
    call CloseFile
    exit
main ENDP

END main

Upvotes: 1

Uzair Khan
Uzair Khan

Reputation: 67

This is an old question but still, to view the data you wrote on the file you need to view the HEX code of the file by using a HEX Editor tool. i tried debugging your code and viewed it with hex edit and your array was there. You can download Hexedit from here : http://www.hexedit.com/ or use any other tool that will allow you to view file in hex mode.

Upvotes: 0

Related Questions