filetest.asm

USE16

ORG     100h


;; define system calls

exit    =0

r       =0

w       =1

writes  =9

dos     =21h

fcreate =3Ch

fopen   =3Dh

fclose  =3Eh

fread   =3Fh

fwrite  =40h


;; create a file

MOV     AH,fcreate      ;system call

MOV     CX,0            ;file attributes

MOV     DX,filepath     ;file name

INT     dos


;; open the file for writing and store the file handle

MOV     AH,fopen        ;system call

MOV     AL,w            ;write mode

MOV     DX,filepath     ;file name

INT     dos

MOV     [filehandle],AX ;move file handle to address [filehandle]


;; write to the open file

MOV     AH,fwrite

MOV     BX,[filehandle] ;use file handle from address [filehandle] below

MOV     CX,14           ;write 14 bytes

MOV     DX,fileinput    ;14 byte long message

INT     dos


;; close the file

MOV     AH,fclose

INT     dos


;; open the file for reading

MOV     AH,fopen

MOV     AL,r

MOV     DX,filepath

INT     dos

MOV     [filehandle],AX


;; read from the file

MOV     AH,fread

MOV     BX,[filehandle]

MOV     CX,14

MOV     DX,fileoutput

INT     dos


;; write file content to console

MOV    AH,writes

MOV    DX,fileoutput

INT    dos


;; quit

MOV     AH,exit

INT     dos


;; file path

filepath:

DB      'testfile.txt',0

fileinput:

DB      'I am a file.',13,10

filehandle:

DB      255,255

fileoutput:

DB      'fourteen bytes' ;a reservation the same size as the file contents

DB      '$'



 © Andrew Brehm 2016