我想将文件中的字节复制到字符串

时间:2022-04-11 20:28:15

The code below shows a piece of my program which can copy bytes (one byte at a time) from one file to another. What should i do to make it copy bytes from file and save it into the 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

;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:

1 个解决方案

#1


You need a pointer to the string, use the pointer to store the byte where the pointer points to, then move the pointer forward, here is the code :

你需要一个指向字符串的指针,使用指针存储指针指向的字节,然后向前移动指针,这里是代码:

    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.
    inc  si                 ;MOVE POINTER TO NEXT POSITION.

    jmp reading             ;REPEAT PROCESS.

eofp:

Notice how, at the beginning, SI points the the string. After checking EOF we store the read byte in [ SI ], then increment SI (here we appreciate the difference between SI and [ SI ] ).

注意SI在开始时如何指向字符串。在检查EOF之后,我们将读取字节存储在[SI]中,然后增加SI(这里我们理解SI和[SI]之间的差异)。

But a second string is not necessary. We already have DX pointing to "buffer", move it before the label "reading", and, after reading, let's increase it (it's necessary to intialize DX before "reading" or it will save all the read bytes in the same position):

但是第二个字符串不是必需的。我们已经让DX指向“缓冲区”,在标签“读取”之前移动它,并且在读取之后,让它增加它(在“读取”之前必须初始化DX,否则它将在同一位置保存所有读取的字节) :

    mov dx, offset buffer   ;THE BYTE WILL BE STORED HERE.  <==========================
reading:

;READ ONE BYTE.
    mov ah, 3FH             ;read from the file
    mov bx, handle          ;normal file
    mov cx, 1               ;HOW MANY BYTES TO READ.
    int 21h                 ;NUMBER OF BYTES READ RETURNS IN AX.

    inc dx                  ;POSITION FOR NEXT BYTE. <================================

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

    jmp reading             ;REPEAT PROCESS.

eofp:

To make the process more efficient you can read bytes not one at a time but two at a time, or ten by ten.

为了提高处理效率,您可以一次读取一个字节,一次读取两个字节,或一次读取10个字节。

As Gondil said, this will work for small files, less than 64Kb. For bigger files you would need to allocate memory (but that's for another question).

正如Gondil所说,这适用于小于64Kb的小文件。对于较大的文件,您需要分配内存(但这是另一个问题)。

#1


You need a pointer to the string, use the pointer to store the byte where the pointer points to, then move the pointer forward, here is the code :

你需要一个指向字符串的指针,使用指针存储指针指向的字节,然后向前移动指针,这里是代码:

    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.
    inc  si                 ;MOVE POINTER TO NEXT POSITION.

    jmp reading             ;REPEAT PROCESS.

eofp:

Notice how, at the beginning, SI points the the string. After checking EOF we store the read byte in [ SI ], then increment SI (here we appreciate the difference between SI and [ SI ] ).

注意SI在开始时如何指向字符串。在检查EOF之后,我们将读取字节存储在[SI]中,然后增加SI(这里我们理解SI和[SI]之间的差异)。

But a second string is not necessary. We already have DX pointing to "buffer", move it before the label "reading", and, after reading, let's increase it (it's necessary to intialize DX before "reading" or it will save all the read bytes in the same position):

但是第二个字符串不是必需的。我们已经让DX指向“缓冲区”,在标签“读取”之前移动它,并且在读取之后,让它增加它(在“读取”之前必须初始化DX,否则它将在同一位置保存所有读取的字节) :

    mov dx, offset buffer   ;THE BYTE WILL BE STORED HERE.  <==========================
reading:

;READ ONE BYTE.
    mov ah, 3FH             ;read from the file
    mov bx, handle          ;normal file
    mov cx, 1               ;HOW MANY BYTES TO READ.
    int 21h                 ;NUMBER OF BYTES READ RETURNS IN AX.

    inc dx                  ;POSITION FOR NEXT BYTE. <================================

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

    jmp reading             ;REPEAT PROCESS.

eofp:

To make the process more efficient you can read bytes not one at a time but two at a time, or ten by ten.

为了提高处理效率,您可以一次读取一个字节,一次读取两个字节,或一次读取10个字节。

As Gondil said, this will work for small files, less than 64Kb. For bigger files you would need to allocate memory (but that's for another question).

正如Gondil所说,这适用于小于64Kb的小文件。对于较大的文件,您需要分配内存(但这是另一个问题)。