注册 登录
编程论坛 汇编论坛

读取指定文件里面的字符,运行卡死,求助!!!

发布于 2012-08-14 11:10, 841 次点击
杨季文的80x86书上面的程序T4-6.asm,不知道为什么运行就卡死。直接上代码。

;**************************************************************************
;program name: show_txt.asm
;function: show a text file
;author: frank
;date: 8-14-2012
;**************************************************************************

EOF = 1AH

DATA    SEGMENT
FNAME   DB 'TEST.TXT', 0                     ;file's name
ERROR1  DB 'File not found', 07h, 0          ;message
ERROR2  DB 'Reading error', 07h, 0           ;message
BUFFER  DB ?                                 ;buffer zore for one byte

DATA    ENDS

;**************************************************************************

CODE    SEGMENT

        ASSUME  CS:CODE,DS:DATA

 START: mov     ax,data
        mov     ds,ax                        ;set data segment register

        mov     dx,offset FNAME
        mov     ax,3d00h                     ;open file for reading
        int     21h
        jnc     OPEN_OK                      ;open success

        mov     si,offset ERROR1
        call    DMESS                        ;show message when operation failed
        jmp     OVER

OPEN_OK:mov     bx,ax                        ;save file

  CONT: call    READCH                       ;read a character from file
        jc      READERR                      ;turning when read failed
        cmp     al,eof                       ;judge if read character for file end
        jmp     CONT                         ;continue

READERR:mov     si,offset ERROR2
        call    DMESS                        ;show massage for failed reading

TYPE_OK:mov     ah,3eh                       ;close file
        int     21h

  OVER: mov     ah,4ch                       ;ending program
        int     21h


;subroutine message: read character

READCH  PROC
        mov     cx,1                         ;set number for byte of reading
        mov     dx,offset BUFFER             ;set burrer zero address
        mov     ah,3fh                       ;set function call's number
        int     21h                          ;read
        jc      READCH2                      ;error for reading
        cmp     ax,cx                        ;judge if file is ending
        mov     al,EOF                       ;if file ended, set file endiing character
        jb      READCH1                      ;file ended, turning
        mov     al,BUFFER                    ;if file isn't end, reading next
READCH1:clc
READCH2:ret
READCH  ENDP

;subroutine DMESS: show a string whose ending character is "0"
;entrance parameter: si=string's start address
;export parameter: none

DMESS   PROC
DMESS1: mov     dl,[si]
        inc     si
        or      dl,dl
        jz      DMESS2
        mov     ah,2
        int     21h
        jmp     DMESS1
DMESS2: ret
DMESS   ENDP

;subroutine: PUTCH:  

PUTCH   PROC
        push    dx
        mov     dl,al
        mov     ah,2
        int     21h
        pop     dx
        ret
PUTCH   ENDP

CODE    ENDS
        END     START

;**************************************************************************
11 回复
#2
zklhp2012-08-14 11:33
不懂帮顶
#3
2012-08-14 11:33
自己的帖子,自己坐沙发吧。坐等各位解答。
#4
2012-08-14 11:34
回复 楼主 bcshuke
额,不是吧。斑竹,你也不会?不要忽悠我呀。
#5
zklhp2012-08-14 11:34
DOS方面的东西 我不懂 不过感觉这个程序写的不错 呵呵
#6
zklhp2012-08-14 11:40
  CONT: call    READCH                       ;read a character from file
        jc      READERR                      ;turning when read failed
        cmp     al,eof                       ;judge if read character for file end
        jmp     CONT                         ;continue

怀疑是个错的程序 因为这里是死循环
#7
2012-08-14 11:41
回复 4楼 bcshuke
谢谢斑竹夸奖,这个程序基本上是照抄书上的例子的。当然,也有我自己的后期加工哈。
#8
2012-08-14 11:49
回复 5楼 zklhp
斑竹正解!你找到问题啦。Bingo!
正确的代码如下所示:

;**************************************************************************
;program name: show_txt.asm
;function: show a text file
;author: frank
;date: 8-14-2012
;**************************************************************************

EOF = 1AH

DATA    SEGMENT
FNAME   DB 'TEST.TXT', 0                     ;file's name
ERROR1  DB 'File not found', 07h, 0          ;message
ERROR2  DB 'Reading error', 07h, 0           ;message
BUFFER  DB ?                                 ;buffer zore for one byte

DATA    ENDS

;**************************************************************************

CODE    SEGMENT

        ASSUME  CS:CODE,DS:DATA

 START: mov     ax,data
        mov     ds,ax                        ;set data segment register

        mov     dx,offset FNAME
        mov     ax,3d00h                     ;open file for reading
        int     21h
        jnc     OPEN_OK                      ;open success

        mov     si,offset ERROR1
        call    DMESS                        ;show message when operation failed
        jmp     OVER

OPEN_OK:mov     bx,ax                        ;save file

  CONT: call    READCH                       ;read a character from file
        jc      READERR                      ;turning when read failed
        cmp     al,EOF                       ;judge if read character for file end
        jz      TYPE_OK                      ;yes,turn
        call    PUTCH                        ;show character which readed
        jmp     CONT                         ;continue

READERR:mov     si,offset ERROR2
        call    DMESS                        ;show massage for failed reading

TYPE_OK:mov     ah,3eh                       ;close file
        int     21h

  OVER: mov     ah,4ch                       ;ending program
        int     21h


;subroutine message: read character

READCH  PROC
        mov     cx,1                         ;set number for byte of reading
        mov     dx,offset BUFFER             ;set burrer zero address
        mov     ah,3fh                       ;set function call's number
        int     21h                          ;read
        jc      READCH2                      ;error for reading
        cmp     ax,cx                        ;judge if file is ending
        mov     al,EOF                       ;if file ended, set file endiing character
        jb      READCH1                      ;file ended, turning
        mov     al,BUFFER                    ;if file isn't end, reading next
READCH1:clc
READCH2:ret
READCH  ENDP

;subroutine DMESS: show a string whose ending character is "0"
;entrance parameter: si=string's start address
;export parameter: none

DMESS   PROC
DMESS1: mov     dl,[si]
        inc     si
        or      dl,dl
        jz      DMESS2
        mov     ah,2
        int     21h
        jmp     DMESS1
DMESS2: ret
DMESS   ENDP

;subroutine: PUTCH:  

PUTCH   PROC
        push    dx
        mov     dl,al
        mov     ah,2
        int     21h
        pop     dx
        ret
PUTCH   ENDP

CODE    ENDS
        END     START

;**************************************************************************
#9
有容就大2012-08-14 11:52
回复 6楼 zklhp
业余的水平就是专业!
#10
2012-08-14 11:53
没想到这么快就搞定啦。再次感谢斑竹,结贴啦!
只有本站会员才能查看附件,请 登录
#11
zklhp2012-08-14 13:22
以下是引用有容就大在2012-8-14 11:52:17的发言:

业余的水平就是专业!

貌似也少有人专业搞这个
#12
有容就大2012-08-14 13:30
回复 11楼 zklhp
我很想搞 不过火候没到 。
1