我正在尝试将数据从一个文件提取到字符串中,并将其从该字符串中复制到另一个文件中

时间:2015-05-13 15:08:12

标签: assembly

下面的代码显示我想打开一个现有文件并在我的字符串中逐个复制其数据并将该数据从字符串保存到另一个文件但由于某种原因它只是保存我的文件的第一个字符时间与文件大小一样多。有人可以帮助我。

.model small

.stack 100h

.data

file        db 'file.txt',0

file1       db 'file1.txt',0

buffer      db ?                ;data buffer

buffer1     db ?

handle      dw ? 

handle2     dw ?

mystring    db 37

.code

main:

;Housekeeping section

mov ax,@data        ;establish addressability

mov ds,ax           ;to data segment

;Main Process Section

xor ax,ax

xor bx,bx

xor cx,cx

xor dx,dx

call openfile

mov dx,offset file

int 21h

mov handle, ax

call createfile

mov dx,offset file1

int 21h

jc error                ;end program if error

mov handle2,ax          ;save file handle 

 lea  si, mystring       ;SI POINTS TO A STRING.  <======================

reading:

;READ ONE BYTE.

    mov ah, 3FH             ;read from the file

    mov bx, handle          ;normal file

    mov cx, 1               ;HOW MANY BYTES TO READ.

    mov dx, offset buffer   ;THE BYTE WILL BE STORED HERE.

    int 21h                 ;NUMBER OF BYTES READ RETURNS IN AX.

;CHECK EOF (END OF FILE).

    cmp ax,0                ;IF AX == 0 THEN EOF.

    je  eofp

;SAVE BYTE IN STRING.                               <======================

    mov  al, buffer         ;AL = BYTE READ.

    mov  [ si ], al         ;SAVE BYTE IN CURRENT POSITION.



;WRITE BYTE TO THE SECOND FILE.           

    mov ah, 40h                            ; write to 

    mov bx, handle2                        ; file handle

    mov dx, offset mystring                ; where to find data to write

    mov cx, 1                              ;LENGTH OF STRING IN CX.

    int 21h

    inc  si                 ;MOVE POINTER TO NEXT POSITION.

    jmp reading                            ;REPEAT PROCESS.
eofp:


error:

mov ax, 4c00h

int 21h

proc createfile

;creating a file

mov ah,3ch

xor cx,cx

ret

createfile endp

proc openfile

;open file 

mov ah,3Dh

mov al,0            ;read mode    

ret

openfile endp


end main

1 个答案:

答案 0 :(得分:1)

错误是你增加si,指向你存储下一个字节读取位置的指针。但是当您将字节写入文件时,您告诉处理程序从mystring获取si最初指向的字节。

结果是每次都将第一个字节值写入文件,并且在后续文件读取时,字节将写入数据段,但会被忽略。

更容易省去mystringsi,只需将每个字节读入buffer并从那里写入每个字节。即使您通过将mystring替换为mov dx, offset mystring来更正错误,mov dx, si也会在37个字节后溢出。

reading:
    ;READ ONE BYTE.
    mov ah, 3FH             ;read from the file
    mov bx, handle          ;normal file
    mov cx, 1               ;HOW MANY BYTES TO READ.
    mov dx, offset buffer   ;THE BYTE WILL BE STORED HERE.
    int 21h                 ;NUMBER OF BYTES READ RETURNS IN AX.

    ;CHECK EOF (END OF FILE).
    cmp ax,0                ;IF AX == 0 THEN EOF.
    je  eofp

    ;WRITE BYTE TO THE SECOND FILE.
    mov ah, 40h             ;write to
    mov bx, handle2         ;file handle
    mov dx, offset buffer   ;where to find data to write
    mov cx, 1               ;LENGTH OF STRING IN CX.
    int 21h
    jmp reading             ;REPEAT PROCESS.
eofp:

顺便说一下,在调用使用int carry函数21h打开源文件后,您不会检查3Dh