Question

I have here a TASM code and it does not append the new string when I run the program again. I would like to thank us2012 for helping me reach this "state".

; This example program creates a file and then writes to it.
.model small
.stack

.data 
CR equ 13
LF equ 10

StartMessage DB "This program creates a file called NEW.TXT"
         DB ,"on the C drive.$"

EndMessage DB CR,LF,"File create OK, look at file to"
       DB ,"be sure.$"

WriteMessage  DB "An error has occurred (WRITING)$"
OpenMessage   DB "An error has occurred (OPENING)$"
CreateMessage DB "An error has occurred (CREATING)$"

WriteMe  DB "HELLO, THIS IS A TEST, HAS IT WORKED?",0
FileName DB "new.txt",0 ; name of file to open 
Handle   DW ?   ; to store file handle 

.code 
START:
mov ax,@data    ; base address of data segment
mov ds,ax   ; put it in ds
mov dx,offset StartMessage 
mov ah,09h 
int 21h 

mov si,offset FileName  ; put offset of filename in dx 
xor cx,cx       ; clear cx - make ordinary file
mov ah,6Ch      ; function 3Ch - create a file
mov al, 0
int 21h         ; call DOS service
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h


jc CreateError      ; jump if there is an error



mov si,offset FileName  ; put offset of filename in dx
mov al,2        ; access mode -read and write
mov ah,3Dh      ; function 3Dh - open the file
int 21h         ; call dos service



jc OpenError        ; jump if there is an error



mov Handle,ax       ; save value of handle 
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h



mov dx,offset WriteMe   ; address of information to write 
mov bx,Handle       ; file handle for file
mov cx,38       ; 38 bytes to be written
mov ah,40h      ; function 40h - write to file
int 21h         ; call dos service

jc WriteError       ; jump if there is an error
cmp ax,cx       ; was all the data written?
jne WriteError      ; no it wasn't - error!

mov bx,Handle       ; put file handle in bx 
mov ah,3Eh      ; function 3Eh - close a file
int 21h         ; call dos service

mov dx,offset EndMessage 
mov ah,09h 
int 21h 

ReturnToDOS:

mov ax,4C00h        ; terminate program 
int 21h 

WriteError:
mov dx,offset WriteMessage 
jmp EndError

OpenError:
mov dx,offset OpenMessage 
jmp EndError

CreateError:
mov dx,offset CreateMessage 

EndError:
mov ah,09h 
int 21h 
mov ax,4C01h 
int 21h 

END START

If I use 3Ch, the program works but it does not append to the end of the file. By using 6Ch instead of 3Ch, this says that it does not truncate an existing file to zero bytes but whenever I run the code, there was an error in creating the file (no file was created). Please help me fix the code. Thank you very much!

EDIT 2:

; This example program creates a file and then writes to it.
.model small
.stack

.data 
CR equ 13
LF equ 10

StartMessage DB "This program creates a file called NEW.TXT"
         DB ,"on the C drive.$"

EndMessage DB CR,LF,"File create OK, look at file to"
       DB ,"be sure.$"

WriteMessage  DB "An error has occurred (WRITING)$"
OpenMessage   DB "An error has occurred (OPENING)$"
CreateMessage DB "An error has occurred (CREATING)$"

WriteMe  DB "HELLO, THIS IS A TEST, HAS IT WORKED?",0
FileName DB "new3.txt",0 ; name of file to open 
Handle   DW ?   ; to store file handle 

.code 
START:
mov ax,@data    ; base address of data segment
mov ds,ax   ; put it in ds
mov dx,offset StartMessage 
mov ah,09h 
int 21h 

mov si, offset FileName     ; put offset of filename in dx 
xor cx,cx       ; clear cx - make ordinary file
mov ah,6Ch      ; function 3Ch - create a file
mov bx, 3
mov dx, 12h
int 21h         ; call DOS service

jc CreateError      ; jump if there is an error

mov ah,3Eh      ; function 3Eh - close a file
int 21h         ; call dos service


mov dx, offset FileName     ; put offset of filename in dx
mov al,2        ; access mode -read and write
mov ah,3Dh      ; function 3Dh - open the file
int 21h         ; call dos service

jc OpenError        ; jump if there is an error

mov Handle,ax       ; save value of handle
mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h

mov dx,offset WriteMe   ; address of information to write 
mov bx,Handle       ; file handle for file
mov cx,38       ; 38 bytes to be written
mov ah,40h      ; function 40h - write to file
int 21h         ; call dos service

jc WriteError       ; jump if there is an error
cmp ax,cx       ; was all the data written?
jne WriteError      ; no it wasn't - error!

mov bx,Handle       ; put file handle in bx 
mov ah,3Eh      ; function 3Eh - close a file
int 21h         ; call dos service

mov dx,offset EndMessage 
mov ah,09h 
int 21h 

ReturnToDOS:

mov ax,4C00h        ; terminate program 
int 21h 

WriteError:
mov dx,offset WriteMessage 
jmp EndError

OpenError:
mov dx,offset OpenMessage 
jmp EndError

CreateError:
mov dx,offset CreateMessage 

EndError:
mov ah,09h 
int 21h 
mov ax,4C01h 
int 21h 

END START
Was it helpful?

Solution

Look at this part:

mov dx,offset FileName  ; put offset of filename in dx 
xor cx,cx       ; clear cx - make ordinary file
mov ah,6Ch      ; function 3Ch - create a file
mov al, 0
int 21h         ; call DOS service

and at the relevant documentation:

AX = 6C00h
BL = open mode as in AL for normal open (see also AH=3Dh)
...
BH = flags
...
CX = create attribute (see #01769).
DL = action if file exists/does not exist (see #01770).
DH = 00h (reserved).
DS:SI -> ASCIZ file name

Return:
CF set on error AX = error code (see #01680 at AH=59h/BX=0000h)
CF clear if successful AX = file handle
CX = status (see #01768) 

The function expects si and not dx to have the offset of the file name.

You don't set bx.

You don't set dx.

You don't check for errors.

You don't save the file handle.

Look at this part:

mov bx, Handle
xor cx, cx
xor dx, dx
mov ah, 42h
mov al, 02h
int 21h

Handle is uninitialized at this point because you never saved the handle into this variable.

Now, here:

mov dx,offset FileName  ; put offset of filename in dx
mov al,2        ; access mode -read and write
mov ah,3Dh      ; function 3Dh - open the file
int 21h         ; call dos service

You're trying to open the open file again. You need to have it closed before opening. Or, if you've got the handle, just keep working with the file, don't close and reopen it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top