2012-09-20 59 views
0

我正在嘗試使用CreateOutputFile,WriteToFileCloseFile Irvine32程序向磁盤文件寫入雙字數組。這是我的代碼。將數組寫入磁盤文件的彙編程序

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 

我每次調試完後,一個文本文件創建成功,但它原來是在文本文件中的一些無法識別碼。我認爲它應該是以十六進制顯示的數組。

我真的不知道我犯了什麼錯誤。請幫幫我!謝謝!

+1

我不明白你爲什麼會想到它是「以十六進制」,因爲你不顯示任何代碼來做到這一點。在Kip Irvine的包含文件中可能會有一些東西 - 在它周圍(或RTFM)。如果沒有,這不是很難做... –

+0

我同意弗蘭克。谷歌自己的電影,數字; sbb al,69h; DAS;訣竅,這會讓你更容易。 – Pyjong

回答

0

這是一個老問題,但仍然要查看您在文件中編寫的數據,您需要使用HEX Editor工具查看文件的HEX代碼。我試着調試你的代碼,並用十六進制編輯查看它,你的數組在那裏。 您可以從這裏下載Hexedit:http://www.hexedit.com/ 或使用任何其他工具,這將允許您以十六進制模式查看文件。

1

「無法識別的代碼」是188個字節,表示CPU內部格式中稱爲「DWORD」的47個值。該文件是array DWORD 47 DUP(?)的內存轉儲。對於人們可讀的格式,例如十進制字符串,它們必須在Fibonacci循環(L1)內逐個進行轉換,或者使用新循環(梯形圖在下面顯示爲L2)。 WinApi包含一個可用作轉換例程的函數:wsprintf。由於Irvine32庫聲明瞭這個函數,所以可以在沒有其他情況下使用它。

例子:

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 
相關問題