读取输入文件并创建多个输出文件以存储数据

时间:2018-11-26 18:49:46

标签: assembly x86 dos

我对汇编语言不熟悉,我的任务是要求我将文件名和数字作为输入(例如:data.txt 100),并将该文件分成多个文件,每个文件存储100个字符(因此,如果我的数据.txt具有520个字符,它将创建6个新文件,名称为data1 data2 ...第6个文件将具有20个字符。

当前,我有一个应用程序,该应用程序使用输入文件的名称,并在其循环时创建新文件(在循环结束之前要求辅助文件的名称)

我面临的问题是我无法使文件自动更改名称(data1 data2 data3 ...),也无法使辅助输入成为要读取的字符数量(所以它是当前设置为1)

任何类型的智慧都受到高度赞赏

我的代码:

.model small

.stack 100h

.data

handle      dw ? 
handle2     dw ? 

filename    db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
            db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).

filename2   db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
            db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).

prompt1 db 13,10,"ENTER FILE NAME HERE: $" 
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $" 

mess1       db ' I WIN! $'                                               

buf         db ?

.code

main:           
mov ax, @data       ; set up addressability of data
mov ds, ax

;DISPLAY MESSAGE.
lea dx, prompt1            ; load and print the string PROMPT
mov ah, 9
int 21h      

;CAPTURE FILENAME FROM KEYBOARD.                                    
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h                

;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0      ;CLEAR CH TO USE CX. 
inc cx         ;TO REACH CHR(13).
add si, cx     ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.            

;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0   ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax                      ; save file handle
f1:
;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2            ; load and print the string PROMPT
mov ah, 9
int 21h      

;CAPTURE FILENAME FROM KEYBOARD.                                    
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h                

;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0      ;CLEAR CH TO USE CX. 
inc cx         ;TO REACH CHR(13).
add si, cx     ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.            

;CREATE FILE.
mov ah, 3ch         ; dos service to create file
mov cx, 0    ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax                      ; save file handle

;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.

reading:
;READ ONE BYTE.
mov ah, 3FH
mov bx, handle
mov cx, 1           ;HOW MANY BYTES TO READ.
mov dx, offset buf  ;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  eof              
;WRITE BYTE TO THE SECOND FILE.           
mov ah, 40h                         ; write to 
mov bx, handle2                     ; file
mov dx, offset buf                  ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
jmp f1 ;REPEAT PROCESS.
eof:
;CLOSE FILES.           
mov ah, 3Eh                         ; close file
mov bx, handle                      ; which file
int 21h 
mov ah, 3Eh                         ; close file
mov bx, handle2                     ; which file
int 21h 

mov ah, 4ch
int 21h

end main

1 个答案:

答案 0 :(得分:0)

.model small

.stack 100h

.data

handle      dw ? 
handle2     dw ? 

filename    db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
            db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).

filename2   db  26        ;MAX NUMBER OF CHARACTERS ALLOWED (25).
            db  ?         ;LENGTH (NUMBER OF CHARACTERS ENTERED BY USER).
            db  26 dup(0) ;CHARACTERS ENTERED BY USER. END WITH CHR(13).

prompt1 db 13,10,"ENTER FILE NAME HERE: $" 
prompt2 db 13,10,"ENTER A SECONDARY FILE NAME: $" 
prompt3 db 13,10,"ENTER LETTER COUNT: $"

mess1       db ' I WIN! $'                                               

buf         db ?

input  db 30 dup ('$')
n      dw ?
count  dw ?
count2 dw ?
output db 30 dup ('$')
msj    db 13,10,'The number is = $'

.code

main:           
mov ax, @data       ; set up addressability of data
mov ds, ax

;DISPLAY MESSAGE.
lea dx, prompt1            ; load and print the string PROMPT
mov ah, 9
int 21h      

;CAPTURE FILENAME FROM KEYBOARD.                                    
mov ah, 0Ah
mov dx, offset filename ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h                

;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0      ;CLEAR CH TO USE CX. 
inc cx         ;TO REACH CHR(13).
add si, cx     ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.            

;OPEN FILE TO READ FROM IT.
mov ah, 3DH
mov al, 0   ;READ MODE.
mov dx, offset filename + 2
int 21h
mov handle, ax                      ; save file handle

jmp w1

f1:
;READ ALL BYTES FROM FIRST FILE AND WRITE THEM TO SECOND FILE.
mov count2, 0
reading:


mov ah, 3FH
mov bx, handle

xor dx, dx
mov dx, offset buf

mov cx, 1          ;HOW MANY BYTES TO READ.
mov dx, offset buf  ;THE BYTE WILL BE STORED HERE.
int 21h             ;NUMBER OF BYTES READ RETURNS IN AX.
;CHECK EOF (END OF FILE).
;push cx
cmp ax, 0  ;IF AX == 0 THEN EOF.
je  eof

cmp count2, 0
jne writ
    ;DISPLAY MESSAGE FOR SECOND FILE.
lea dx, prompt2            ; load and print the string PROMPT
mov ah, 9
int 21h      

;CAPTURE FILENAME FROM KEYBOARD.                                    
mov ah, 0Ah
mov dx, offset filename2 ;THIS VARIABLE REQUIRES THE 3-DB FORMAT.
int 21h                

    ;CAPTURED STRING ENDS WITH CHR(13), BUT TO CREATE FILE WE NEED
;THE FILENAME TO END WITH CHR(0), SO LET'S CHANGE IT.
mov si, offset filename2 + 1 ;NUMBER OF CHARACTERS ENTERED.
mov cl, [ si ] ;MOVE LENGTH TO CL.
mov ch, 0      ;CLEAR CH TO USE CX. 
inc cx         ;TO REACH CHR(13).
add si, cx     ;NOW SI POINTS TO CHR(13).
mov al, 0
mov [ si ], al ;REPLACE CHR(13) BY 0.            

    ;CREATE FILE.
mov ah, 3ch         ; dos service to create file
mov cx, 0    ;READ/WRITE MODE.
mov dx, offset filename2 + 2 ;CHARACTERS START AT BYTE 2.
int 21h
mov handle2, ax                      ; save file handle

writ:
inc count2
;WRITE BYTE TO THE SECOND FILE.
mov ah, 40h                         ; write to
mov bx, handle2                     ; file

mov dx, offset buf                  ; where to find data to write
mov cx, 1 ;LENGTH OF STRING IN CX.
int 21h
mov cx, count
cmp cx, count2
je f1
;cmp ax, count
jmp reading ;REPEAT PROCESS.
eof:

;CLOSE FILES.
mov ah, 3Eh                         ; close file
mov bx, handle                      ; which file
int 21h
mov ah, 3Eh                         ; close file
mov bx, handle2                     ; which file
int 21h
mov ah, 4ch
int 21h












;WORK WITH NUMBER
w1:
;DISPLAY MESSAGE.
mov dx , offset prompt3
mov ah, 9
int 21h 
;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
mov bx , offset input
mov count , 0
l1:
mov ah , 1    
int 21h               ;CAPTURE ONE CHAR FROM KEYBOARD.
mov [bx] , al         ;STORE CHAR IN STRING.
inc bx 
inc count
cmp al , 13
jne l1                ;IF CHAR IS NOT "ENTER", REPEAT.           

dec count             ;NECESSARY BECAUSE CHR(13) WAS COUNTED.


;CONVERT STRING TO NUMBER. 
mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
add bx,  count        ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
mov bp, 0             ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
mov cx, 0             ;PROCESS STARTS WITH 10^0.
l2:      
;GET CURRENT POWER OF 10.
cmp cx, 0
je  first_time        ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
mov ax, 10
mul cx                ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
mov cx, ax            ;CX == 10^CX.
jmp l22               ;SKIP THE "FIRST TIME" BLOCK.
first_time:    
mov cx, 1             ;FIRST TIME 10^0 = 1.
l22:    
;CONVERT CURRENT CHAR TO NUMBER.   
dec bx                ;BX POINTS TO CURRENT CHAR.
mov al , [bx]         ;AL = CURRENT CHAR.
sub al , 48           ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
mov ah, 0             ;CLEAR AH TO USE AX.
mul cx                ;AX * CX = DX:AX. LET'S IGNORE DX.
add bp , ax           ;STORE RESULT IN BP.    
;CHECK IF THERE ARE MORE CHARS.    
dec count
cmp count , 0
jne l2
push bp
pop count
jmp f1
end main

这是有类似问题的人的答案,这是我找到的解决方案。应用程序将根据所选大小将文件拆分为多个单独的文件。到目前为止,它并不是最有效的,因为每个字母都是单独读取的,但是它可以完成此工作,并且可以拆分图片文件,并使用Windows命令将其还原回原始图片。

第一个输入需要您要拆分的文件的名称(例如data.txt或picture.png) 第二个是您希望放在单个文件中的字符数量(最多35,000个,请小心,因为将具有10000个字符的文件分成小块可能会创建数百个文件) 输入到x的第三个是新创建的文件名(例如data1,data2.txt,data3.png ...等等,它将要求输入文件名,直到第一个文件到达末尾,因此将100个字母文件除以1个字符将询问您输入100个名称)。

祝您好运,并祝您编程愉快:)