lodsb tasm outputs only last letter from the file

不打扰是莪最后的温柔 提交于 2020-01-16 01:01:12

问题


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.

Updated 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   

回答1:


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.

Second, where is si initialized?

Third, why do you compare it against 0?

Fourth, why two jumps?

jmp ending ; you want this to be je
jmp loop

And the function charget writes a character, but is named as if it were reading it?

But even if you would have the je to branch, where would the 0come from? You said in your file is abc, so there will be no 0 in your buffer, unless you explitcitly put it there.

update

What you must do is the following:

  1. open the file. done
  2. read the file. done
  3. preserve the number of bytes you just read.
  4. close the file. done
  5. point to the buffer where you read it into.
  6. loop through that buffer until you have printed N characters, where N is the number of bytes you preserved from the read.

update 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


来源:https://stackoverflow.com/questions/20500169/lodsb-tasm-outputs-only-last-letter-from-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!