lodsb tasm仅输出文件中的最后一个字母 [英] lodsb tasm outputs only last letter from the file
问题描述
我不知道问题出在哪里,我正在尝试将文件中的所有字符输出到屏幕上,但是不知何故,它仅输出最后一个字符. 例如,如果我的test.txt文件中包含 abc 行,则输出仅向我们显示 c . 在这种情况下,我应该如何正确使用lodsb? 不要介意数据部分中不必要的行,我只是粘贴了所有代码.
I don't know where is the problem here, i'm trying to output all the character from file to the screen, but somehow it prints only the last one. For example if my test.txt file has a line abc in it, the output shows us only c. How should i use lodsb properly in this situation? Don't mind some unnecessary lines in data section, i just pasted not the all code.
更新的代码:
.model small
.stack 100h
.data
filename db 'test.txt',0
filename1 db 'temp.txt',0
error_open db 'impossible to open this file$',13,10
error_read db 'impossible to read this file$',13,10
handle dw 0
handle1 dw 0
buffer db 21 dup 0
counter dw 0
.code
mov dx, @data
mov ds, dx
mov dx, offset filename
mov al, 2
mov ah, 3dh
int 21h
mov handle, ax
jc erroropening
readbyte_1_file:
inc counter
mov dx, offset buffer
mov bx, handle
mov cx, 1
mov ah, 3fh
int 21h
jc errorreading
cmp ax, cx
jne close_1_file ;bytes actually read
jmp readbyte_1_file
close_1_file:
mov bx, handle
mov ah, 3eh
int 21h
mov si, offset buffer
put_char:
lodsb
mov dl,al
call putchar
dec counter
cmp counter, 0
je ending
jmp put_char
erroropening:
mov dx, offset error_open
mov ah, 9
int 21h
jmp ending
errorreading:
mov dx, offset error_read
mov ah, 9
int 21h
jmp ending
ending:
mov ax, 4c00h
int 21h
putchar proc
mov ah, 2
int 21h
RET
putchar endp
END
推荐答案
close_1_file:
mov bx, handle
mov ah, 3eh
int 21h
loop:
lodsb
mov dl,al
call charget
cmp si, 0
jmp ending
jmp loop
首先,您真的不应该像指令一样命名一个标签.
First of all, you really shouldn't name a lable like an instruction.
第二,si
在哪里初始化?
第三,为什么将它与0进行比较?
Third, why do you compare it against 0?
第四,为什么要跳两次?
Fourth, why two jumps?
jmp ending ; you want this to be je
jmp loop
函数charget
写入一个字符,但被命名为好像正在读取它吗?
And the function charget
writes a character, but is named as if it were reading it?
但是即使您要分支je
,0
将从何而来?您说文件中的文件是abc
,因此除非您明确地将它放在其中,否则缓冲区中将没有0
.
But even if you would have the je
to branch, where would the 0
come from? You said in your file is abc
, so there will be no 0
in your buffer, unless you explitcitly put it there.
更新
您必须执行的操作如下:
What you must do is the following:
- 打开文件. 完成
- 读取文件. 完成
- 保留您刚刚读取的字节数.
- 关闭文件. 完成
- 指向您将其读入的缓冲区.
- 循环访问该缓冲区,直到您打印出N个字符为止,其中N是您从读取中保留的字节数.
- open the file. done
- read the file. done
- preserve the number of bytes you just read.
- close the file. done
- point to the buffer where you read it into.
- loop through that buffer until you have printed N characters, where N is the number of bytes you preserved from the read.
更新2
您仍然还有一个问题.当然,您将每个字符存储在一行中,否则您将在同一位置读取每个字符.
You still have one problem left. Of course you store each character in a row, otherwoise you read each single character in the same place.
mov ax, counter
mov dx, offset buffer
add dx, ax
inc counter
这篇关于lodsb tasm仅输出文件中的最后一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!