Creating and save text file in assembly

7.8k views Asked by At

I am trying to create new file using assembler language in MASM 6.11 and DOSBox on Windows 7. I have mount drive C, D, E as folders BIN, BINR and palce where are my programs. When I try to run my program in console it get stuck but when I am debbuging it, using CV command process terminated normally. Here is my code:

.model small
.stack 100h

.data
    NazwaPliku  db  "dane.txt", 0
    UchwytPliku dw  ?
    Napis db "ASSEMBLER"

.code
        ASSUME cs: @code, ds: @data
        mov ax, @data
        mov ds, ax

main PROC
        MOV AH,3CH
        MOV CX,0
        MOV DX,OFFSET NazwaPliku
        INT 21H 

        MOV AH, 3DH
        MOV AL, 0
        MOV DX, OFFSET NazwaPliku
        INT 21H
        mov UchwytPliku, ax

        MOV AH,40H
        MOV BX, UchwytPliku
        MOV DX,OFFSET Napis
        MOV CX, 5
        INT 21H

        mov ah, 4Ch
        mov al, 0
    int 21h
main ENDP

END main

I know that it is very simple program, but I can't get it to work...

1

There are 1 answers

0
Fifoernik On BEST ANSWER

This is your program with corrections

.model small
.stack 100h

.data
    NazwaPliku  db  "dane.txt", 0
    UchwytPliku dw  ?
    Napis db "ASSEMBLER"

.code
        ASSUME cs: @code, ds: @data
main PROC
        mov ax, @data              <- Put in the execution path!
        mov ds, ax                 <- so below "main PROC"

        MOV AH,3CH
        MOV CX,0
        MOV DX,OFFSET NazwaPliku
        INT 21H 
        jc fail                    <- In case DOS failed the operation 

        ;MOV AH, 3DH               <- After a succesful creation, the
        ;MOV AL, 1                 <- file is already opened for normal
        ;MOV DX, OFFSET NazwaPliku <- read and write access.
        ;INT 21H
        ;jc fail

        mov UchwytPliku, ax

        MOV AH,40H
        MOV BX, UchwytPliku
        MOV DX,OFFSET Napis
        MOV CX, 5                  <- "ASSEMBLER" has 9 bytes. Typo ?
        INT 21H
        ;jc fail

fail:
        mov ah, 4Ch
        mov al, 0
        int 21h
main ENDP

END main

It would be best to display a message whenever the operation failed.

If you really need to open the file explicitely then first close it after it was created.

    MOV AH,3CH
    MOV CX,0
    MOV DX,OFFSET NazwaPliku
    INT 21H
    jc fail

    mov bx, ax
    mov ah, 3Eh
    int 21h
    jc fail

    MOV AH, 3DH
    MOV AL, 1
    MOV DX, OFFSET NazwaPliku
    INT 21H
    jc fail
    mov UchwytPliku, ax