You are on page 1of 4

File create and write

.MODEL SMALL
.DATA
Fname db “myfile.txt”,0
Handle dw ?
Msg1 db ‘error’,’$’
Msg2 db ‘success’,’$’
Msg3 db ‘hello hi’,’$’
.CODE
start:
MOV AX,@DATA
MOV DS, AX
Mov cx,0
Mov dx,offset fname
Mov ah,3ch
Int 21h

Jc err

Mov handle,ax
Mov ah,09h
Mov dx,offset msg2
Int 21h
Mov bx,handle
Mov cx,09h
Lea dx,msg3
Mov ah,40h
Int 21h

Jmp k
Err:mov ah,09h
Lea dx, msg1
Int 21h

K:mov ah,4ch
Int 21h

END start
End

Open an existing file, write into that file

.MODEL SMALL
.DATA
Fname db “myfile.txt”,0
Handle dw ?
Msg3 db ‘hello hi’,’$’
.CODE
start:
MOV AX,@DATA
MOV DS, AX
Mov al,1
Mov dx,offset fname
Mov ah,3dh
Int 21h

Mov handle,ax
Mov bx,handle
Mov cx,09h
Lea dx,msg3
Mov ah,40h
Int 21h

mov ah,4ch
Int 21h

END start
End

Read the data into a buffer from existing file.


.MODEL SMALL
.DATA
Fname db “myfile.txt”,0
Handle dw ?
Buff db 15 dup(?)
.CODE
start:
MOV AX,@DATA
MOV DS, AX
Mov al,0
Mov dx,offset fname
Mov ah,3dh
Int 21h

Mov handle,ax
Mov bx,handle
Mov cx,09h
Mov dx,offset buff
Mov ah,3fh
Int 21h

mov ah,4ch
Int 21h

END start
End

You might also like