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
7

Opening a File

Introduction

An FCB names a file, but nothing has checked that the file exists. Before the BDOS will read a file it must open it: find the name in the directory and fill in the rest of the FCB from the entry.

Function 15

Put the FCB's address in DE, 15 in C, and CALL 5. What comes back in A:

  • FFh - not found: the name is not on that drive, or its entry is deleted.
  • 0, 1, 2 or 3 - found. The directory is read 128 bytes (four entries) at a time, and the number says which of the four held the file's entry. You will rarely want it; what matters is that it is not FFh.

So test with CP 0FFH, never OR A: a file found in the first place of four gives 0, and OR A would call that a failure.

When the open succeeds, the BDOS has copied the entry into the FCB: byte 12 the extent, byte 15 the record count, bytes 16-31 the block numbers. It also sets byte 14 to 80h for its own use. Leave bytes 12-35 alone while the file is open: they are the BDOS's notes.

The Code

O opens a file and shows A, and the FCB as the BDOS left it. NEEDDOS does nothing yet - it simply returns - but every command that calls the BDOS calls it first; in Part four it will matter.

After the line JP Z,CMDF, add:

        CP 'O'
        JP Z,CMDO

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

; O name: open the file; show what the BDOS says and the FCB after.
CMDO:   CALL NEEDDOS
        LD DE,FCB
        CALL GETNAME
        JP C,WHAT
        LD DE,FCB
        LD C,15
        CALL 5
        PUSH AF
        LD HL,OPENM
        CALL PRINT
        POP AF
        PUSH AF
        CALL PHEX
        CALL NEWLINE
        POP AF
        CP 0FFH
        LD HL,NOTFM
        JP Z,DONE
        LD HL,FCB
        LD BC,32
        CALL SHOWBLK
        JP MAIN

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

; NEEDDOS: back to MAIN with a message if there is no DOS to call.
NEEDDOS:

After the line NEEDDOS:, add:

        RET

After the line FCB: DEFS 36, add:

OPENM:  DEFB 'OPEN: ',0
NOTFM:  DEFB 'NOT FOUND',13,10,0

Run it as in S6, on a copy of PRIMER.DSK, and type:

O 1:POEM.TXT
O 1:ROOMS.DAT
O POEM.TXT
Q

What you should see

> O 1:POEM.TXT
OPEN: 00
0000 02 50 4F 45 4D 20 20 20 .POEM
0008 20 54 58 54 00 00 80 03  TXT....
0010 01 00 00 00 00 00 00 00 ........
0018 00 00 00 00 00 00 00 00 ........
> O 1:ROOMS.DAT
OPEN: 01
0000 02 52 4F 4F 4D 53 20 20 .ROOMS
0008 20 44 41 54 00 00 80 80  DAT....
0010 02 00 03 00 04 00 05 00 ........
0018 06 00 07 00 08 00 09 00 ........
> O POEM.TXT
OPEN: FF
NOT FOUND
>

POEM.TXT: record count 3 (byte 15), one block, block 1 - the same as its directory entry (edisc.py entries PRIMER.DSK shows it). ROOMS.DAT is in the next entry, so A is 1; its first extent holds 80h - 128 - records in blocks 2 to 9. And O POEM.TXT, with no drive, looked on the current drive - drive 0, the system disc - where there is no POEM.TXT.

Change One Thing

  • Open 1:ROOMS.DAT again, then look at its directory with edisc.py entries. There are two entries for it. Which one did the open copy into the FCB?
  • Change CP 0FFH after the open to OR A. Which of the three opens above would now say NOT FOUND?

Summary

Function 15 opens a file: FFh means not found, anything else found. The open fills the FCB from the directory entry, and from now on the FCB is how the BDOS keeps track of the file.

Next

S8 reads the opened file, 128 bytes at a time.

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.