Simulating copy command of DOS in 8086 Assembly language

Program Description

Simulating copy command of DOS in 8086 assembly language This program shows an implementation of copy command of DOS in 8086 Assembly language.
The program expects two arguments:

copy1 file1 file2

Here copy1 is the name of the program. file1 is the existing file from which we wish to copy data to file2. The program assumes that file2 does not exist. If it exists,it simply overwrites it without any warning.

1)The program first reads in the file names from the command line via PSP.
2)It then opens the first file,i.e. file1 and stores its handle in a variable.
3)Create a new file for copying the data, file2.Exit the program if error occurs.
4)Open file2,if error opening file, exit the program.
5)Read data from file1 in a buffer (here buffer is of size 100 bytes) and write in file2,untill end of file is reached. If at any point error occurs, exit the program immediately.
6)Close both the files,when writing is complete.

This program is a very basic implementation of copy command and can only do one thing.It cannot accept file names with spaces, and very limited error checking has been done.

;   Simulating "copy" command of DOS in 8086 Assembly language

.model small

;macro for printing messages to the terminal
print macro m
lea dx,m
mov ah,09h
int 21h

;data segement

file1 db 20 dup(0)       ;stores name of file1(file from which data is to be copied)
file2 db 20 dup(0)       ;stores name of file2(new file to which data is to be copied)
fhandle1 dw ?            ;stores handle of file1
fhandle2 dw ?            ;stores handle of file2  
count dw ?               
buff db 100,0,100 dup('0')    ;buffer for storing data temporarily

;message indicating status and errors

namesread db 10,13, "File names read$"
fileopen db 10,13, "File opened successfully$"
fileclose db 10,13, "File closed$"
filecopy db 10,13, "File copied successfully$"
filecreated db 10,13, "File created successfully$"
FileOpenError db 10,13, "Could not open file$"
FileCloseError db 10,13, "Could not close file$"
FileReadError db 10,13, "Could not read file$"
FileCreateError db 10,13, "Could not create file$"

;code segment

       mov ax,@data
	   mov ds,ax
;**********************************reading file name (file1)***********************************
	   mov si,82h        
	   lea di,file1 
read1:  mov al,es:[si]
        cmp al," "       ; compare with space
		je next          ; unless space character is found,continue saving characters 
		mov [di],al      ; to file1
		inc di
		inc si
		jmp read1       ;continue untill space character is found 
next:   inc si          ;skip space character
        lea di,file2    
;********************************reading name of second file***********************************
read2:  mov al,es:[si]
        cmp al,0dh       ;jump on enter 
        je readcomplete
        mov [di],al
        inc di
        inc si

        jmp read2

        ;file names successfully read upto this point
        print namesread
;*******************************opening file 1 for reading content*******************************
open1:  mov ah,3dh   ;int 21 function for opening file
        mov al,00    ;read only mode
		lea dx,file1 ; pointer to file1
		int 21h
		jnc open_success  ;if file opened successfully,no carry is on success
		print FileOpenError  ;print error message and exit the program
		jmp exit
open_success: mov fhandle1,ax    ;save file handle
              print fileopen     ;print success message

;***********************************creating file 2***********************************************	  
	mov ah,3ch       ;int 21h function for creating new file
	xor cx,cx       
	lea dx,file2
	int 21h
	jnc create_success  ;if file created,no carry is on success
	print FileCreateError  ;if file not created,print error message and exit
	jmp exit
     ;if file created successfully
create_success:  print filecreated  
;****************************************open new file in write mode*******************************
    mov ah,3dh    ;int 21h function for opening a file
    mov al,01h    ;write only mode
    lea dx,file2  ;data to be written to file2
	int 21h
	jnc open_success2	   ;if file opened,jmp to label mentioned
	print FileOpenError    ; exit on error
	jmp exit
open_success2: mov fhandle2,ax   ;save the file handle
               print fileopen 

	;both files opened at this point in correct modes		   
	;now we need to read data in buffer and write it to new file

;************************************reading data from file 1************************************
fread:  mov ah,3fh       ;int 21h function for reading data from a file
        mov bx,fhandle1  ;read from file1
		mov cx,100
		lea dx,buff
		int 21h
		jc read_error ; if read error occurs,carry is set. jmp on error
		cmp ax,0000   ;if end of file reached
		je close      ;will close both files and exit

;*****************************now write the bytes to another file*********************************
		mov count,ax   ;number of bytes read
		mov ah,40h         ;int 21h function for writing to a file
        mov bx,fhandle2    ;write in file2
        mov cx,count       ;number of bytes to write
		lea dx,buff        ;buffer from which data is written to file
		int 21h
		jmp fread         ;continue reading and writing untill end of file1 is reached
read_error:  print FileReadError   ;print read error message
             jmp exit


;****************************************closing the files********************************************	
              mov bx,fhandle1  
              mov ah,3eh        ;int 21h function for closing the file
              int 21h			                
              jc close_error	;jump if error closing the file	 

              mov bx,fhandle2   ;close file2 
              mov ah,3eh        ;function for closing the file
              int 21h
              jc close_error    
              print fileclose
			  jmp exit        ;exit the program
close_error:  print FilecloseError		
	          jmp exit        ;exit the program
        mov ah,4ch      ;exit from the program
        int 21h

        end start


Here, abc.txt is the source file and the xyz.txt is the destination file

comments powered by Disqus