filetest.mac

;; Tell the assembler that this is z80 code 

.Z80


;; Define system calls to be used

cwrite EQU 2h ;argument goes in E

cwrites EQU 9h ;argument address goes in DE

fopen EQU 0Fh;FCB address goes in DE

fclose EQU 10h;FCB address goes in DE

freadr EQU 14h;FCB address goes in DE

fwriter EQU 15h;128 byte record at DMA address

fmake EQU 16h;FCB address goes in DE

setdma EQU 1Ah;address goes in DE


;; Zero page constants

reset EQU 0h

bdos EQU 5h ;call number goes in C

fcb EQU 5Ch;first FCB

srecord EQU 7Ch;record index for sequential access

tlength EQU 80h;length of command tail

ctail EQU 81h;command tail


;; Copy file name to FCB

LD HL,name;source

LD DE,fcb;destination

LD BC,12;count

LDIR;load, decrement, repeat

;; Create the file

LD C,fmake;create a file

LD DE,fcb;use FCB

CALL bdos


;; Open the file

LD C,fopen;open a file

LD DE,fcb;use FCB

CALL bdos


;; Set the DMA address

LD C,setdma ;set DMA

LD DE,msg;use address msg

CALL bdos


;; Write a record to the file

LD C,fwriter ;write a record to file

LD DE,fcb

CALL bdos


;; Reset the DMA address

LD C,setdma

LD DE,result

CALL bdos


;; Set the record index back to 0

LD HL,srecord

LD (HL),0

;; Read the same record from the file

LD C,freadr ;read a record from file

LD DE,fcb

CALL bdos

;; Close the file

LD C,fclose ;close a file

LD DE,fcb

CALL bdos


;; Output the result string

LD C,cwrites ;write a string

LD DE,result

CALL bdos

;; Exit program

JP reset


;; Ctrl-Z-terminated string to write to file

msg: DB "Hello, file.$",26


;; Some room for the line to be read into to check

result: DB "              "

;; File name and extension with drive letter (0 for current)

name: DB 0,"FILE    TXT" ;must be upper case

;; End source file

END

 © Andrew Brehm 2016