Using 3Dh causes interrupt to only return "Acces Denied"
Asked Answered
P

1

7

Configuration :

MS-DOS 16 BIT (writing in a .asm file, then compiling them with TASM and TLINK)

Windows 7 x64

I've made a simple program in Assembly that should only OPEN a file and write a text to it.
Here is the code to it:

assume cs:code, ds:data
data segment
    fileName db "input.txt", 0 ; We assure it is an ASCIIZ(ero) file.
    toWrite db "Hello World!", "$"
data ends

code segment
  writeToFile:
    ; pentru functia 3Dh
    mov AH, 3Dh
    mov AL, 0h
    mov dx, offset fileName
    int 21h
    ret


  start_program:
    mov ax, data
    mov ds, ax  
    CALL writeToFile
    mov ax, 4c00h
    int 21h
code ends

end start_program

I used TurboDebugger to see what happens. Strangely, it always puts in AX value 0005 meaning Access Denied

Everything I could find on the internet for searching ASSEMBLY access denied open file was about DLL's and that did not help.

I've tried anything, from restarting my program to opening dosbox "As an administrator". Sadly, nothing worked and I am out of ideas.
What's also strange is, that a friend of mine said that after activating his windows 10, everything worked just fine.

What is the reason for getting only "access denied"? I mention that I was able to create, delete and close files, but I cannot open them.

Primate answered 7/1, 2017 at 14:36 Comment(5)
AX being 5 doesn't mean access denied by itself, only if CF is set to indicate an error. Otherwise it's just your file handle. So, have you checked value of CF?Glottalized
Nope, because untill now I worked with functions that did not require CF and forgot about it. Yeah, you are right! CF is 0. Thanks. I shall delete this post.Primate
It was a well asked question though (not many we see), too bad it has a trivial answer :)Glottalized
You are also passing 0 in the AL register, which is the access mode. 0 means "read only", which isn't going to help you write to the file!Carmelitacarmelite
Thank you as well, Cody Gray! I've been stucked to that problem for half hour!Primate
P
3

For proper operation your writeToFile procedure needs to

  • open the file with an access mode that allows subsequent writing
  • check the CF returned by DOS to see if everything went fine

What I noticed is that you terminate the text that you'll be writing in this file with a "$". I wonder if you know that the DOS function to actually write the file only works with a specified length in CX and not with any kind of delimiter. You could have other valid reasons for this "$" character -;

writeToFile:
    mov  ax, 3D01h    ; 01h=WriteAccess
    mov  dx, offset fileName
    int  21h
    jc   NOK
    mov  bx, ax       ; Handle
    mov  dx, offset toWrite
    mov  cx, 12       ; Length of "Hello World!"
    mov  ah, 40h
    int  21h
    jc   NOK
    cmp  ax, cx
    jne  NOK
NOK:
    ret

Where you place the NOK label and what you do there entirely depends on how much effort you want to spent in dealing with errors returned by DOS. Here in this very simple program, you might just return from the call and have the program terminate.

Philbert answered 8/1, 2017 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.