During the course of studying ASM in, I used BT to publish a large film and sent it to me via QQ. Looking at the file transfer progress, I suddenly wrote the following small program to copy the file.
; Program: copy the file program, automatically rename
Note: Because dos does not support file names with more than 8 characters, the program cannot be copied because the program has eight file names and one cannot be copied.
_ Data Segment
Mess dB 'please, input file name. ', 0dh, 0ah, 24 h
FNF dB 0dh, 0ah, 'file not found. ', 07 h, 0dh, 0ah, 24 h
FCE dB 0dh, 0ah, 'file copying error. ', 07 h, 0dh, 0ah, 24 h
Fre dB 0dh, 0ah, 'file reading error. ', 07 h, 0dh, 0ah, 24 h
Fwe dB 0dh, 0ah, 'file writting error. ', 07 h, 0dh, 0ah, 24 h
Fcdb 0dh, 0ah, 'file copyed succeed. ', 0dh, 0ah, 24 h
Fbuffer dB 128; File Name Buffer
DB?
Fname dB 128 DUP (?)
Fnum_s DW? ; Source file number
Fnum_d DW? ; New file number
Buffer dB 512 DUP (?) ; Read/write buffer
_ DATA ends
;
_ Text Segment
Assume Cs: _ text, DS: _ DATA
Start: mov ax, _ DATA
MoV ds, ax
PUSH DS
Pop es
Full0: Lea dx, mess
MoV ah, 9
Int 21 h
CLD; fill 0, clear the last input file name
MoV CX, 128; the maximum file name is 128 characters, including carriage return characters
MoV Al, 0
Lea Di, fname
Rep stosb
;-------------
Lea dx, fbuffer; input file name
MoV ah, 0ah
Int 21 h
;------------------
MoV BL, [fbuffer + 1]; Replace the last carriage return of the input file name with 0, because the created file name cannot contain invisible characters
Xor bh, BH
MoV Si, offset fname
Add Si, BX
MoV byte PTR [Si], 0
;---------------------
Lea dx, fname; open the source file in read-only mode
MoV ax, 3d00h
Int 21 h
JNC open; opened successfully
Lea dx, FNF; failed, display prompt information
MoV ah, 9
Int 21 h
JMP full0; Re-Input
;--------------
Open: mov fnum_s, ax; open successfully. Save the source file number.
Rebs: mov BL, [fbuffer + 1]; all characters (including '.') from '.' To odh '.')
Xor bh, BH
MoV Si, offset fname
Add Si, BX
Dec Si; Si points to the first grid of odh
Inc bx; BX points to the last grid of odh
MoV [fbuffer + 1], BL; saved to prepare for the next modification of file name
;---------------
Xor cx, CX; CX is the counter. It calculates a total of several characters (including '.') from '.' To odh '.')
MoV Di, Si
Sourch: CMP byte PTR [di], '.'; from the back to the front to find '.'
Je ext; find
Dec di; not found. Find in the previous format
INC CX
JMP sourch
Ext: Inc CX; after it is found, CX will include '.' only after it is added '.'
MoV Di, Si
Add Di, 1; Di points to the last cell of Si
STD
Rep movsb; start execution and remove the filename suffix (including '.')
;-------------------------------
MoV Al, '1'; change '.' To '1'
Testf: PUSH AX
MoV [di], Al
Lea dx, fname
MoV ax, 3d00h; open the new file name in read-only mode to determine whether the file name already exists
Int 21 h
JC exist; open failed, that is, the file name does not exist. You can create a file with this file name.
MoV BX, ax; exists. Close the opened file.
MoV ah, 3eh
Int 21 h
Pop ax
INC al; '1' to '2'
CMP Al, '9' + 1; if the file name exceeds '9', the file name suffix is changed to a new one.
Jae rebs
JMP testf; if not, continue to judge whether the file name already exists.
Exist: mov CX, 0
Lea dx, fname
MoV ah, 3ch; create common new file
Int 21 h
JNC save; created successfully. Save the new file number.
Lea dx, FCE; failed, error message displayed
MoV ah, 9
Int 21 h
JMP full0; Re-Input
Save: mov fnum_d, ax; Save the new file number
MoV ax, 3d01h; open the new file in write-only mode
Int 21 h
;---------------------
Readf: mov BX, fnum_s; obtain the source file number
MoV CX, 512; read 512 characters
MoV dx, offset Buffer
MoV ah, 3fh
Int 21 h
JNC writef; write if the read is successful
Lea dx, Fre; failed. The error message is displayed. close the file and end the program.
MoV ah, 9
Int 21 h
JMP exit
;--------------------------
Writef: mov BX, fnum_d; get the new file number
MoV CX, ax; write files according to the actual number of words read
MoV ah, 40 h
Int 21 h
JNC cmpcx; write successful
Lea dx, fwe; is unsuccessful. The error message is displayed. close the file and end the program.
MoV ah, 9
Int 21 h
JMP exit
Cmpcx: cmp cx, 512; determines whether the actual number of words read is 512
Je readf; if it is 512, it indicates that the end of the file has not been read.
;
Lea dx, FCS; displays 'copied successfully'
MoV ah, 9
Int 21 h
;
Exit: mov ah, 3eh; close the file
Int 21 h
MoV BX, fnum_s
MoV ah, 3eh
Int 21 h
MoV ax, 4c00h
Int 21 h
;
_ Text ends
End start
Source code 3.47kb, generated EXE 1.52kb, which is a major advantage of assembly, haha.
How does csdn insert code without the Assembly option? Instead of C #