c-汇编x86从任何文件中读取并转义所有特殊字符,并以字节为单位获取文件大小



我有学校的作业。我必须读取任何大小为128KB的文件,并将其内容写入屏幕。

我使用函数3Dh打开特定文件,然后使用函数3Fh读取文件。我使用32KB的缓冲区。

我现在面临的问题很少。

  1. 有一个59KB.txt文件,里面有一些书中的文字,还有我的一些代码

当我想得到以字节为单位的文件大小时,它运行良好,结果是正确的。
当我想打印文件It的内容时,会将所有内容打印到文件中出现"$"字符的位置。所以我需要以某种方式转义所有特殊字符,因为"$"用于打印整个文件和任何文件。

  1. 具有380KB.csv文件

当我打印它时,它打印得很好,整个文件,全部为380KB
但是,当我想获得大小时,它只返回2186B。当我在过程结束时没有关闭文件并一次又一次地调用这个过程时,它总是以字节为单位返回大小,即2186B(4372、6558等)的倍数

  1. 我从以前的.csv文件复制了126KB到另一个

再次打印是可以的(没有'$'字符)
当我得到大小时,它返回64063B,所以再次出现错误的结果。

这是我的程序。

buffsiz equ 32768                   ;buffer size =32KB
fnsize  equ 255                     ;filename size =255
data    segment
maxlen  db  fnsize                  ;max length of file name
len     db  ?                       ;length of filename
file    db  fnsize  dup (?)         ;file name
filesiz dd  ?                       ;dword variable of file size
buffer  db  buffsiz dup ('$')       ;32KB buffer
        ;...
data    ends
getcont proc                        ;get content of file procedure
        mov ah,3dh                  ;open file function
        mov al,0                    ;read-access bit
        call forout                 ;just bring 0 char on the end of filename
        mov dx,offset file          ;"move filename" to dx
        int 21h
        mov bx,ax                   ;move filehandler from ax to bx
buffIn: prntstr buffer              ;print content of buffer (in first iteration it is whole set to '$'
        mov ah,3fh                  ;read from file
        mov cx,buffsiz              ;how much bytes it should read from file (32768)
        mov dx,offset buffer
        int 21h
output: xchg ax,bx                  ;exchange values in ax and bx
        mov buffer[bx],'$'          ;after last read byte put '$' into buffer
        xchg ax,bx                  ;exchange registers back for next iteration
        cmp ax,0                    ;if there was no read byte stop loop
        jnz buffIn                  ;if was go to next iteration
        mov ah,3Eh                  ;close file
        int 21h
        ret
getcont endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
getsize proc
        mov word ptr[filesiz],0      ;put zero into filesize variable (dword)
        mov word ptr[filesiz]+2,0
        mov ah,3dh                  ;same as in getcont procedure
        mov al,0                    
        call forout
        mov dx,offset file          
        int 21h     
        mov bx,ax                   
bufflp: mov ah,3fh                  
        mov cx,buffsiz              
        mov dx,offset buffer
        int 21h
        add word ptr[filesiz],ax    ;add number of bytes read into filesiz variable - not certain in this
        cmp ax,0                    ;if there was no byte read end loop
        jnz bufflp                  ;if was go to next iteration
        prntstr nl                  ;new line
        prntstr velkost             ;print string about file size operation
        xor dx,dx                   ;clear ax and dx registers
        xor ax,ax
        mov ax,word ptr[filesiz]    ;move low word from filesiz(dword) variable to ax
        mov dx,word ptr[filesiz]+2  ;move high word from filesiz to dx to get filesiz=dx:ax
        call prntint                ;call procedure to print decimal number on output
        prntchr ' '                 ;print space
        prntchr 'B'                 ; print Byte unit char
        mov ah,3Eh                  ;close file
        int 21h
        ret
getsize endp

使用TASM程序集x86。

我在您提交的代码中发现了这些问题:

mov buffer[bx],'$'          ;after last read byte put '$' into buffer

您应该将缓冲区放大1个字节。现在,当32768字节被读取时,您正在通过缓冲区写入这个$

add word ptr[filesiz],ax    ;add number of bytes read into filesiz variable

前一行不会更新dword变量filesiz!使用以下

add word ptr[filesiz],ax
adc word ptr[filesiz]+2,0

ps。你永远不会检查DOS是否报告错误。访问文件时不应忽视这一点!

最新更新