使用fread从文本文件中读取内容并在程序集x86中将其打印在屏幕上

时间:2016-01-08 13:20:32

标签: assembly x86 intel

我尝试从文本文件中读取一个字符串并将其打印在屏幕上,但我设法只打印第一个字符而我不知道如何修复它。我试图将格式更改为%s,但如果我这样做,我会得到一个空白屏幕。谢谢你的帮助。这是我的代码:

.data
tfile1 db "text.txt", 0
read   db "rb", 0
write  db "wb",0
format db "%c", 0
buffer db 0
.code
start:
push offset read          ;put read binary operation on stack
push offset tfile1        ;put text.txt on stack
call fopen                ;open text.txt
add ESP, 8                ;clear stack
mov esi, eax              ;save pointer to the stack
push esi                  ;stream
push 1                    ;count
push 1                    ;size
push offset buffer
read_loop:
call fread  
test eax,eax              ;check if eax=0
jz close_tfile
xor eax, eax              ;clear eax 
mov al, buffer            ; copy buffer in al
push eax                  ; put eax on stack
push offset format        ; put format on stack
call printf               ; print from text.txt
add esp, 8                ;clear stack
jmp read_loop
close_tfile:
add esp, 16               ; clear stack from fread
push esi                  ;stream   
call fclose
add esp,4 
push 0
call exit
end start

0 个答案:

没有答案