NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: sledge on August 11, 2011, 12:09:32 AM
-
org 100h
mov dx, msg
mov ah, 9
int 21h
mov ds, 0
mov dx, filename
mov ah, 41h
int 21h
mov ah, 4ch
int 21h
msg db 'Deleting file', '$'
filename db 'C:\\nasmdoc.txt', '$'
What I was hoping to do with this code is, to delete the file which name was placed at [filename]
INT 21,41 - Delete File
DS:DX = pointer to an ASCIIZ filename
So I set DX with filename's adress.
But then when compiled, dos gave me:
test.asm:7: error: invalid combination of opcode and operands
This is line 7:
mov ds, 0
What happened?
-
You used an invalid combination of opcodes and operands! :)
Segment registers are not quite like "General Purpose Registers". We can't "mov" an "immediate" number into 'em. We can "mov" another register or memory into 'em. We can "push something"/"pop segreg". But not "mov ds, xxx". Why? Ask Intel. But in this case, ds is already set to the segment where "filename" can be found. You don't want to change it - just delete the line.
This isn't C (the language, not the drive), so I don't think you'll want the double '\' in the filename. (terrible choice of file to delete!!! :) )
Best,
Frank
-
I see. So, I changed the code and still after running it, nothing happens. The file meant to be removed is still there.
What could have gone wrong?
org 100h
mov dx, filename
mov ah, 41h
int 21h
mov ah, 4Ch
int 21h
filename db 'C:\delete.txt', '$'
edit: I added some messages to the code so the user knows wheter the delete function worked or not.
org 100h
mov dx, filename
mov ah, 41h
int 21h
jnc sucess_message
fail_message:
mov dx, fail_msg
mov ah, 9
int 21h
jmp end
sucess_message:
mov dx, sucess_msg
mov ah, 9
int 21h
end:
mov ah, 4Ch
int 21h
filename db 'C:\delete.txt', '$'
fail_msg db 'File could not be deleted.', '$'
sucess_msg db 'File removed sucessfully.', $
-
Filename supposed to be zero-terminated? (the $-terminated nonsense is just for int 21h/9, AFAIK) Possibly an "attribute mask" in cl? 0? or 0FFh? I don't think that applies here...
(check carry-flag for error...)
http://www.ctyme.com/intr/rb-2797.htm
Best,
Frank
-
I dont know if the JC was right to be placed there, I read on this link http://stanislavs.org/helppc/int_21-41.html (http://stanislavs.org/helppc/int_21-41.html) that CF is set if some error happened to occur, that's why I based the result message on the carry flag.
I've done some changes on my code but still havent managed to delete the file... I'll keep trying.
Here's what it looks like now:
org 100h
mov dx, filename
mov ah, 41h
int 21h
jc fail
sucess:
mov dx, sucess_msg
jmp next
fail:
mov dx, fail_msg
next:
mov ah, 9
int 21h
mov ah, 4Ch
int 21h
filename db 'delete.txt'
fail_msg db 'File could not be deleted.', '$'
sucess_msg db 'File removed sucessfully.', $
-
change this:
filename db 'delete.txt'
to this:
filename db 'delete.txt', 0
Filenames must be ASCIIZ ( nul-terminated ).
-
Also, the last $ isn't quoted.
filename db 'delete.txt', 0
fail_msg db 'File could not be deleted.', '$'
sucess_msg db 'File removed sucessfully.', '$'
-
It wasnt working because I was using a $-terminated string instead of a 0-terminated one (ASCIIZ), and also one of the declared messages hadnt the last $ quoted, thanks to all your help I could fix that.
Here's my new code 8)
org 100h
mov ah, 9
mov dx, msg1
int 21h
mov ah, 41h
mov dx, filename
int 21h
jnc sucess
fail:
mov dx, msg_fail
jmp next
sucess:
mov dx, msg_sucess
next:
mov ah, 9
int 21h
mov ah, 4Ch
int 21h
filename db 'delete.txt', 0
msg1 db 'Deleting file... ', '$'
msg_fail db 'Could not delete file, sorry!', '$'
msg_sucess db 'File deleted sucessfully!', '$'
Next I'll try something like having the user to type the file that he wants to be deleted!
Thanks!