Assembly 程序在turbo调试器中运行良好,但在简单执行时会冻结
我正在为MS-DOS编写程序,它反转文件中的所有字符串,路径作为程序行参数发送。当我在TurboDubugger中运行程序时,它会工作,但当我执行它时,它会冻结。但这怎么可能呢?请帮忙。(谢谢)Assembly 程序在turbo调试器中运行良好,但在简单执行时会冻结,assembly,tasm,Assembly,Tasm,我正在为MS-DOS编写程序,它反转文件中的所有字符串,路径作为程序行参数发送。当我在TurboDubugger中运行程序时,它会工作,但当我执行它时,它会冻结。但这怎么可能呢?请帮忙。(谢谢) 答案很简单——正如Jester所说——在第一次阅读之前,我必须将SI归零。我天真的认为,如果它在调试器中等于零,它将始终等于零,这是错误的。在做任何其他事情之前,您应该检查是否达到了EOF。由于您已经这样做了,您可能永远无法检查EOF,因此出现了无限循环。@JesterIt没有帮助它是COM或EXE文件
答案很简单——正如Jester所说——在第一次阅读之前,我必须将SI归零。我天真的认为,如果它在调试器中等于零,它将始终等于零,这是错误的。在做任何其他事情之前,您应该检查是否达到了EOF。由于您已经这样做了,您可能永远无法检查EOF,因此出现了无限循环。@JesterIt没有帮助它是COM或EXE文件?也许堆栈会增长到代码中?而且,在第一次阅读之前,您也需要将si设置为零。@Jester我知道这一点,但我不认为这有那么重要。谢谢,你救了我几个小时的调试!
;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