Assembly Disc Access Primer, a TatungBytes course: DISKMON reading the hidden sector IDs of the course's own practice disc, on a rainbow sunburst
← Back to Modules
module
8

Reading a File, Record by Record

Introduction

The file is open. Now DISKMON reads it - the way Blackwater Hall reads its text, and the way every program under the DOS reads a file: a record of 128 bytes at a time.

Where the Bytes Go: the DMA Address

A read does not hand you the bytes in registers. It puts all 128 of them in memory, at the DMA address. When a program starts, the DMA address is 0080h; function 26, with an address in DE, moves it. DISKMON points it at its own RECBUF, at 8A00h, before it reads.

Function 20: the Next Record

Function 20, with the FCB in DE, reads the next record of the file into the DMA address and moves on. A comes back 0 each time a record was read, and 1 when there are no more - nothing is written to the buffer then. You do not have to think about extents: when one ends, the BDOS finds the next by itself.

A text file's last record is filled out with 1Ah bytes after the text, so TYPEOUT stops printing at the first 1Ah.

The Code

T name: open the file (OPENIT gives up with NOT FOUND if it cannot), set the DMA address, then read and print until A is not 0 or the text ends, and count the records.

After the line JP Z,CMDO, add:

        CP 'T'
        JP Z,CMDT

After the last line of CMDO (JP MAIN), add:

; T name: type a text file, a record at a time, through the BDOS.
CMDT:   CALL NEEDDOS
        CALL OPENIT
        LD DE,RECBUF            ; reads go to RECBUF
        LD C,26
        CALL 5
        LD HL,0
        LD (COUNT),HL
CMDT1:  LD DE,FCB
        LD C,20                 ; read the next record
        CALL 5
        OR A
        JR NZ,CMDT2             ; not 0: the end of the file
        LD HL,(COUNT)
        INC HL
        LD (COUNT),HL
        LD HL,RECBUF
        LD BC,128
        CALL TYPEOUT
        JR NC,CMDT1             ; carry: a 1Ah ended the text
CMDT2:  CALL NEWLINE
        LD HL,(COUNT)
        CALL PDEC
        LD HL,RECSM
        JP DONE

; OPENIT: name from the line into FCB, open it; to MAIN if that fails.
OPENIT: LD DE,FCB
        CALL GETNAME
        JP C,WHAT
        LD DE,FCB
        LD C,15
        CALL 5
        CP 0FFH
        RET NZ
        LD HL,NOTFM
        JP DONE

After the last line of PDEC (JP PUTC), add:

; TYPEOUT: print BC bytes from HL as text, stopping at 1Ah (carry set).
TYPEOUT: LD A,(HL)
        CP 1AH
        SCF
        RET Z
        CALL PUTC
        INC HL
        DEC BC
        LD A,B
        OR C
        JR NZ,TYPEOUT
        RET                     ; carry clear: no 1Ah yet

After the line NOTFM: DEFB 'NOT FOUND',13,10,0, add:

COUNT:  DEFW 0
RECSM:  DEFB ' RECORDS',13,10,0

Run it on a copy of PRIMER.DSK:

T 1:POEM.TXT

What you should see

> T 1:POEM.TXT
THE DISC
Forty tracks go round and round,
ten sectors on each one;
the directory shows what can be found,
and blocks say where they've gone.

Ask the DOS and it will look
the name up for you, line by line;
or read the sectors like a book
and all the work is yours and mine.

3 RECORDS
>

Change One Thing

  • Take out the function 26 call in CMDT. Where will the records go now, and what will T print?
  • POEM.TXT is three records. If TYPEOUT did not stop at 1Ah, what would you see after the last line?

Summary

Function 26 says where records go; function 20 fetches the next one, with A = 0 while there are more and 1 at the end. DISKMON can show any text file on a disc.

Next

S9 stops reading in order and jumps straight to any record.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.