Rimanio
Rimanio

Reputation: 11

How to create text file and write data into it using TASM

I have to write a program in assembly (using TASM for compiling) which will create a text file and write data into it. It should be something like a log file that would contain information about user activities.

Below is the code of a small sample program to give you a better understanding of what I mean. Once the program ran, it should create a text file and write there a message "Program began its work". If the user pressed "1" it would write a message "User pressed 1" into a text file. If the user presses "2" it would write "User pressed 2" into a text file. If the user presses "3" it would write "Program finished its work" into a text file. Each message in the text file should start from a new line.

.model small

.data

msg_start db "Press 1 or 2",0dh, 0ah,"Press 3 to exit the program$"

msg_1 db "You pressed 1" ,0dh, 0ah,"Press any key to continue$"
msg_2 db "You pressed 2" ,0dh, 0ah,"Press any key to continue$"

.code
.startup


start:

        ;Clearing screen
        MOV AH, 0 
        MOV AL, 2 
        INT 10H 
        
        ;Start message output
        mov ax, seg msg_start
        mov ds, ax
        mov dx, offset msg_start
        mov ah, 9h
        int 21h
        
        ;Ask user to press the key
        mov ah, 1h
        int 21h
                
        ;Comparing pressed key
        cmp al, 31h ;Pressed "1"
        je label_1  ;Jump to "label_1"
        cmp al, 32h ;Pressed "2"
        je label_2  ;Jump to "label_2"
        cmp al, 33h ;Pressed "3"
        je label_3  ;Jump to "label_3"
        
        jmp start ;Pressed any other key
   

label_1:

        ;Clearing screen
        MOV AH, 0 
        MOV AL, 2 
        INT 10H 
        
        ;Message output
        mov ax, seg msg_1
        mov ds, ax
        mov dx, offset msg_1
        mov ah, 9h
        int 21h
        
        ;Ask user to press the key
        mov ah, 1h
        int 21h

        cmp al, 33h ;Pressed "3"
        je label_3  ;Jump to "label_3"

        jmp start ;Jumping to start label


label_2:


        ;Clearing screen
        MOV AH, 0 
        MOV AL, 2 
        INT 10H 
        
        ;Message output
        mov ax, seg msg_2
        mov ds, ax
        mov dx, offset msg_2
        mov ah, 9h
        int 21h
        
        ;Ask user to press the key
        mov ah, 1h
        int 21h

        cmp al, 33h ;Pressed "3"
        je label_3  ;Jump to "label_3"

        jmp start ;Jumping to start label

label_3:

        ;Exit the program
        mov ah, 4ch
        mov al, 01
        int 21h

end

Upvotes: 0

Views: 755

Answers (1)

vitsoft
vitsoft

Reputation: 5775

When you want to work with files, you'll need to employ some services of operating system. DOS functions are served by INT 21h. Look for functions with FILE in their name (forget the obsolete FCB file functions).

Manipulation with disk files requires file handle, which is an identifier of internal object established by OS when the file is created. Handle can be obtained from DOS with function CREATE FILE

.data
FileName DB "Rimanio.log",0
.text
   MOV AH,3Ch  ; Function CREATE OR TRUNCATE FILE.
   SUB CX,CX   ; 0=Normal file attributes.
   MOV DX, OFFSET FileName ; Offset of file name.
   INT 21h     ; Invoke DOS function. 
   JC Error    ; Go to report if error occured.
   MOV BX,AX   ; Save the handle to BX.

With the handle of just created file it's easy to write a string to it, using WRITE. Write should be terminated with CLOSE FILE, this will flush the written contents to the file and update its size, time and other attributes. You can then check it in console window with type Rimanio.log.

Upvotes: 2

Related Questions