I think we've strayed from what was suggested.
;; Assuming ES == DS, and CLD has been done
mov di,offset [random]
mov cx,64000 / 2
mov ax,0
rep stosw
When you overwrite a file, you should probably round up the length to
the next full allocation cluster size (often 4KB, but check!), so that
you get rid of any data remaining in that tail.
If I understand it, I would like to write over the ( file + the remaining bytes in the partially used cluster)
So if cluster size is 4 Kb x 1024 = 4096 bytes
So if the file is 8000 bytes in size, then are there 192 bytes left to be overwritten too ?
2 clusters x 4096 = 8192 bytes allocated for the file - 8000 = 192
open_it:
mov ax,716Ch ; create file
xor cx,cx ; file attributes
; file access modes (bits) BX register
; 000 read-only
; 001 write-only
; 010 read-write
; 100 read-only, do not modify file's last-access time
; Bitfields for Windows95 long-name open action: DX Register
; Bit(s) Description (Table 01781)
; 0 open file (fail if file does not exist)
; 1 truncate file if it already exists (fail if file does not exist)
; 4 create new file if file does not already exist (fail if exists)
; Note: the only valid combinations of multiple flags are bits 4&0 and 4&1
mov bx,00000001b ; access mode - write only
mov dx,00000001b ; open file
lea si,file_name + 2 ; sets the file name
int 21h
mov [handle],ax ; Save file handle
jnc short get_size ; No errors, go on
no_file:
mov dx,offset not_there; Get error message
jmp error ; and go display/exit
get_size:
mov ax,4202h ; Set file pointer
mov bx,[handle] ; for this file
xor cx,cx ; relative to end of file
xor dx,dx ; offset 0 bytes
int 21h
jnc save_size
err2:
mov dx,offset emsg2 ; Get error message
jmp error ; and go display/exit
save_size:
mov word ptr [file_size],ax ; Save low word of file size
mov word ptr [file_size + 2],dx ; Save high word
mov ax,4200h ; Move file pointer
mov bx,[handle] ; for this file
xor cx,cx ; relative to beginning of file
xor dx,dx ; offset 0 bytes
int 21h
jc err2 ; Errors: go handle
next_bunch:
mov cx,64000 ; Assume 64,000 bytes or more
; left to do
sub word ptr [file_size],cx ; Is there ? - subtract it
sbb word ptr [file_size + 2],0 ; from saved file size
jae wipe ; There were 64,000 bytes or
; more left
mov cx,word ptr [file_size] ; Get number of bytes left
add cx,64000 ; back CX (undo subtraction)
wipe:
mov ah,40h ; Write file
mov bx,[handle] ; Handle
lea dx,random ; Write the random bytes
int 21h
jnc check_size ; No errors, go on
err3:
mov dx,offset emsg3 ; Get appropriate error message
jmp error ; and go display/exit
check_size:
cmp ax,cx
jnz err3
cmp ax,64000 ; Full 64,000 bytes written,
je next_bunch ; yes, go check for more
jmp SHORT $+2 ; short delay cuz sometimes
; file isn't renamed in a cmd.exe or command.com environment
;
mov bx,[handle] ; close file
mov ah,3eh
int 21h