Author Topic: Deleting file using int 21h  (Read 9552 times)

Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Deleting file using int 21h
« on: August 11, 2011, 12:09:32 AM »
Code: [Select]
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]

Quote
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:
Quote
test.asm:7: error: invalid combination of opcode and operands

This is line 7:
Quote
mov ds, 0

What happened?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Deleting file using int 21h
« Reply #1 on: August 11, 2011, 12:40:20 AM »
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


Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Re: Deleting file using int 21h
« Reply #2 on: August 11, 2011, 03:36:55 AM »
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?

Code: [Select]
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.
Code: [Select]
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.', $
« Last Edit: August 11, 2011, 03:51:55 AM by sledge »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Deleting file using int 21h
« Reply #3 on: August 11, 2011, 03:54:53 AM »
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


Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Re: Deleting file using int 21h
« Reply #4 on: August 11, 2011, 04:31:40 AM »
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 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:

Code: [Select]
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.', $

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Deleting file using int 21h
« Reply #5 on: August 11, 2011, 10:44:53 AM »
change this:
    filename db 'delete.txt'
to this:
    filename db 'delete.txt', 0

Filenames must be ASCIIZ ( nul-terminated ).


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Deleting file using int 21h
« Reply #6 on: August 11, 2011, 02:52:40 PM »
Also, the last $ isn't quoted.

Code: [Select]
filename db 'delete.txt', 0
fail_msg db 'File could not be deleted.', '$'
sucess_msg db 'File removed sucessfully.', '$'

About Bryant Keller
bkeller@about.me

Offline sledge

  • Jr. Member
  • *
  • Posts: 19
Re: Deleting file using int 21h
« Reply #7 on: August 13, 2011, 08:54:34 AM »
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)

Code: [Select]
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!