x86 Assembly(NASM) - 打开文件以读取和打印内容

时间:2014-01-22 11:05:00

标签: x86 nasm

我正在尝试从文件中读取一些文本并将其打印到控制台,但我根本无法使其正常工作。它不打印任何东西...... (我的文件只有5个字的文字) CreateFile调用似乎工作正常。 ReadFile调用返回0,所以这可能就是问题所在。

任何帮助都会非常感激。

    global  _main
extern  _printf
extern  _ReadFile@20
extern  _WriteFile@20
extern  _GetStdHandle@4
extern  _CreateFileA@28
    extern  _ExitProcess@4

section .data
    fmt:        db '%d', 0Dh, 0Ah, 0    ; newline and NULL terminated
    access:     dd 0x10000000
    filename:   db 'ThisIsATestFile.txt', 0

    buflen:     dd 2048     ; Size of our buffer to be used for read
    dwBytesRead:dw 0

section .bss
    buffer:     resb 2048   ; A 2 KB byte buffer used for read

section .text
_main:
; CreateFile@28 - OpenFile
xor eax, eax
push eax            ; 7 param - Handle
push 128            ; 6 param - FILE_ATTRIBUTE_NORMAL
push 4              ; 5 param - OPEN_ALWAYS
push eax            ; 4 param - Default security
push eax            ; 3 param - Do not share
push DWORD [access] ; 2 param - Generic All
push filename       ; 1 param - FileName
call _CreateFileA@28

    mov ebx, eax    ; copy the handle into ebx

; ReadFile@20
xor eax, eax
push eax
push dwBytesRead
push buflen
push buffer
push ebx    ; handle
call _ReadFile@20

mov esi, eax    ; get the amount of bytes read

; GetHandle - Output
push -11
call _GetStdHandle@4
mov ebx, eax    ; copy the handle into ebx

; WriteFile@20
xor eax, eax
push eax
push eax
push esi
push buffer
push ebx
call _WriteFile@20




; ExitProcess(0)
    push    0
    call    _ExitProcess@4

非常感谢你帮助Ruud。奇迹般有效 :) 我最后用ReadFile和WriteFile做了这样的事。

; ReadFile@20
xor eax, eax
push eax
push dwBytesRead
push DWORD [buflen]
push buffer
push ebx    ; handle
call _ReadFile@20

mov esi, eax

; GetHandle - Output
push -11
call _GetStdHandle@4
mov ebx, eax    ; copy the handle into ebx


; WriteFile@20
add esi, DWORD [dwBytesRead]    ; copy the length into esi

xor eax, eax
push eax
push eax
push esi
push buffer
push ebx
call _WriteFile@20

1 个答案:

答案 0 :(得分:0)

替换

push buflen

通过

push DWORD [buflen]

您应该在那里传递值,而不是参考。