我为MS-DOS编写程序,它会反转文件中的所有字符串,该路径作为程序行参数发送。当我在Turbo Dubugger中运行程序时它可以工作,但是当我执行它时它会冻结。但它怎么可能呢?请帮忙。感谢)
;filelab.asm
;THIS PROGRAM REVERSES FILE'S STRINGS
;PASS FILE NAME TO COMMAND LINE
;by Nikita Kunevich
;Belarus 2014
;All rights reserved
;PROBLEM - if only 1 char in str
.model tiny
.code
org 100h
start:
mov cl,cs:0080h ;filename length +1
cmp cl,1
jle cmd_error
dec cl
mov bl,0082h ;filename adress
add bl,cl
mov byte ptr [bx],0 ;writing 0 to the end of filename
mov dx,0082h
mov ax,3D02h ;open file for read/write
int 21h ;ax contains file identifier
jc read_error ;if file reading error
mov bx,ax ;write identifier to bx
xor cx,cx
xor ax,ax
mov di,offset str_buf
LOCAL
@@fread_loop: ;si contains string offset
mov cl,1h ;number of bytes to read
mov ah,3Fh ;DOS file read func
mov dx,di
add dx,si
int 21h
push ax ;saving number of readen characters
push di
add di,si
mov dl,byte ptr[di]
cmp dl,0Dh ;EOS (if 0Ah we shouls pass through)
pop di
jne str_noend ;if not EOS skip this lines
test si,si
jz skip_rev ;if si = 0 skip reverse
call reverse
mov dx,2 ;CRLF pass
call pass_char
CRLF:
add si,2
add word ptr [filepos+2],si
jnc skip_rev
add word ptr [filepos],1
skip_rev:
test si,si
jnz not0Dh
mov dx,1
call pass_char
jmp CRLF
not0Dh:
xor si,si
pop ax
jmp @@fread_loop
str_noend:
pop ax
cmp ax,cx ;looking for EOF
jl reverse_last
inc si
jmp @@fread_loop
reverse_last:
test si,si
jz file_exit
call reverse ;EOF found
file_exit:
;----------------------------FILE EXIT----------------------------
mov ah,3Eh ;close file, bx contains identifer
int 21h
;----------------------------END----------------------------------
jmp exit
fsize_error:
mov ah,09h
mov dx,offset fsize_er_mes
int 21h
jmp exit
cmd_error:
mov ah,09h
mov dx,offset cmd_er_mes
int 21h
jmp exit
read_error:
mov ah,09h
mov dx,offset error_mes
int 21h
exit:
mov ax,4c00h
int 21h
;--------------------DATA----------------------------
fsize_er_mes db "File is too big",0Dh,0Ah,'$'
access_er_mes db "Access denied",0Dh,0Ah,'$'
cmd_er_mes db "Cmd is empty!",0Dh,0Ah,'$'
error_mes db "Can't read the file",0Dh,0Ah,'$'
filepos dd 0
str_buf db 100h dup (?)
;--------------------END-----------------------------
pass_char proc
mov al,1 ;CRLF pass
xor cx,cx
mov ah,42h
int 21h
ret
pass_char endp
;reverse proc
reverse proc
cmp si,1
je one_num
push si ;loop var - right border
add si,di
dec si
push di ;loop var - left border
;reversing string
L1:
mov al,byte ptr[di]
mov ah,byte ptr[si]
mov [di],ah
mov [si],al
dec si
inc di
cmp si,di
jae L1
pop di
pop si
;writing reversed string back to file
;changing file offset
one_num:
mov al,0 ;move to the beginning of the string
mov dx,word ptr [filepos+2] ;in the file
mov cx,word ptr [filepos]
mov ah,42h
int 21h
mov ah,40h
mov cx,si ;number of bytes to write
mov dx,di
int 21h
ret
reverse endp
end start
答案 0 :(得分:1)
答案很简单 - 正如杰斯特所说 - 我必须在第一次阅读之前将SI归零。我天真的想法,如果它在调试器中等于零,它总是等于零是错误的。