Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 3Dh causes interrupt to only return "Acces Denied"

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.

like image 306
SnuKies Avatar asked Jan 07 '17 14:01

SnuKies


1 Answers

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.

like image 181
Sep Roland Avatar answered Oct 20 '22 16:10

Sep Roland