Wednesday, February 17, 2010

CS401

[org 0x0100]
jmp start
filename1: times 128 db 0 ; 1st file name

filename2: times 128 db 0 ; 2nd file name

handle1: dw 0 ; handle for 1st file

handle2: dw 0 ; handle for 2nd file

buffer1: times 1000 db 0 ; buffer for 1st file

errorMSG: db 'file could not be opened$'

success: db 'Successfully copied$'

start: mov ch, 0

mov cl, [0x80] ; command tail length in ex

dec cx ; leave the first space

mov di, 0x82 ; start of command tail in di

mov al, 0x20 ; space for parameter separation

cld ; auto increment mode

repne scasb ; search space

je next ; if found, proceed

jmp problem ; proceed to error printing

next: push cx ; save origional cx

mov si, 0x82 ; set si to start of param

mov cx, di ; set di to end of param

sub cx, 0x82 ; find param size in cx

dec cx ; excluding the space

mov di, filename1 ; set di to space for filename1

rep movsb ; copy filename there

mov byte [di], 0 ; terminate filename with 0

pop cx ; restore original cx

inc si ; go to start of next filename

mov di, filename2 ; set di to space for filename2

rep movsb ; copy filename here

mov byte [di], 0 ; terminate filename with 0

mov ah , 0x3d ; service 3d - open file

mov al, 0 ; read-only mode

mov dx, filename1 ; address of filename

int 0x21 ; DOS service

jnc open ; if no error, proceed for file2

jmp problem ; proceed to error printing

open: mov [handle1], ax ; save handle for first file

push cx ; save cx register

mov cx, 0 ; open file with normal attributes

mov ah, 0x3c ; service 3c - create file

mov dx, filename2 ; address of filename

int 0x21 ; DOS service

pop cx ; restore ex register

jnc store ; if no error, proceed

jmp problem ; proceed to error printing

store: mov [handle2], ax ; save handle for second file

readloop: mov ah, 0x3f ; service 3f - read file

mov bx, [handle1] ; handle for file to read

mov cx, 1000 ; number of bytes to read

mov dx, buffer1 ; buffer to read in

int 0x21 ; DOS service

jnc write ; if no error, proceed

jmp problem ; proceed with error printing

write: test ax, ax ; are zero bytes read?

jz succeeded ; yes, operation completed

mov ah, 0x40 ; else, service 40 - write file

mov bx, [handle2] ; handle for file to read

push cx ;save cx register

mov dx, buffer1 ; buffer to write

int 0x21 ; DOS service

jc problem ; error in writing?

pop cx ; restore cx

cmp ax, cx ; more bytes to read?

je readloop ; yes, start with next byte



succeeded:

mov dx, success ; select success message

jmp exit



problem:

mov dx, errorMSG

exit: mov ah, 9 ; service 9 - output message

int 0x21 ; DOS service

mov ah, 0x3e ; service 3e - close file

mov bx, [handle1] ; handle of file1 to close

int 0x21

mov ah, 0x3e ; service 3e- close file

mov bx, [handle2] ; handle of file2 to close

int 0x21

mov ax, 0x4c00 ; terminate program

int 0x21 ; DOS service

No comments: