0

I am working on a program in 16b assembly that simply creates a file with a filename that I type myself in runtime and then closes it. The problem is - the file isn't created. Here is my code:

org 100h
mov ah, 9
mov dx, prompt
int 21h

mov ah, 0ah
mov dx, filename
int 21h

mov ah, 3ch
mov cx, 0000h
mov dx, filename+2
int 21h

mov word [filehandle], ax

mov bx, [filehandle]
mov ah, 3eh
int 21h


mov ah, 4ch
int 21h


prompt  db  "Filename:",10,13,"$"
filename db  10
         db  0
         times 11 db "$"
filehandle dw 0

As you can see I am using buffered input to read the filename. Unfortunately the file is not created. It works well if I hardcode the filename, like this:

...

mov ah, 3ch
mov dx, filename
int 21h

...

filename db "test.txt"

but that's not what I want to achieve. What is the reason behind this code not working? I am using NASM.

EDIT: The problem is solved, @rkhb 's solution worked.

Tomek
  • 147
  • 1
  • 1
  • 11

1 Answers1

0

Int 21h / 3Ch expects an ASCIIZ-string, i.e NULL terminated, but Int 21h / 0Ah gives you 0Dh (ENTER) as termination. So you must change 0Dh into 00h:

...
mov ah, 0ah
mov dx, filename
int 21h

movzx di, byte [filename+1]
add di, filename+2
mov byte [di], 0

mov ah, 3ch
mov cx, 0000h
mov dx, filename+2
int 21h
...
rkhb
  • 14,159
  • 7
  • 32
  • 60