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
18

Writing a Sector

Introduction

Reading is safe. Writing a sector is not: there is no file, no directory, no DOS between you and the disc. Write the wrong 512 bytes to the wrong place and you have damaged the disc. Which is exactly why it is worth learning on a copy.

Call 163

Call 163 is call 162 the other way round. The same scratch-pad bytes say which sector, and FB53h-FB54h where the 512 bytes come from. The Z flag says whether it worked; on failure, A holds the error number. When the call returns, the sector is on the disc.

In MAME it works on an .mfi only; on a .dsk it fails with error 1, bad data.

Change, Then Write

DISKMON's sector buffer still holds the last sector S read. K offset text puts text into it at an offset (one space after the number, then the text exactly) and shows the first 128 bytes; M d t s writes the buffer to a sector. So: read the directory sector, put a new name over an old one, and write it back.

The Code

After the line JP Z,CMDL, add:

        CP 'K'
        JP Z,CMDK
        CP 'M'
        JP Z,CMDM

After the last line of BLKSEC (RET), add:

; K offset text: put the text into the sector buffer at offset.
CMDK:   CALL GETNUM
        JP C,WHAT
        LD DE,SECBUF
        ADD HL,DE
        EX DE,HL
        LD HL,(LPTR)
        LD A,(HL)               ; one space, then the text exactly
        CP ' '
        JR NZ,CMDK1
        INC HL
CMDK1:  LD A,(HL)
        OR A
        JR Z,CMDK2
        LD (DE),A
        INC HL
        INC DE
        JR CMDK1
CMDK2:  LD HL,SECBUF
        LD BC,128
        CALL SHOWBLK
        JP MAIN

; M d t s: write the sector buffer to drive d, track t, sector s.
CMDM:   CALL GETDTS
        JP C,WHAT
        CALL SETPAD
        RST 8
        DEFB 163
        JP NZ,DISCERR
        LD HL,WROTEM
        JP DONE

Run it on a fresh work.mfi made from PRIMER.DSK (S10):

S 1 2 0
K 1 VERSE
M 1 2 0
Q

POEM.TXT's entry is the first: its name is bytes 1-8. K 1 VERSE puts VERSE over POEM - five letters over four, and the name's other three bytes were spaces already.

What you should see

> K 1 VERSE
0000 00 56 45 52 53 45 20 20 .VERSE
0008 20 54 58 54 00 00 00 03  TXT....
0010 01 00 00 00 00 00 00 00 ........
0018 00 00 00 00 00 00 00 00 ........
0020 00 52 4F 4F 4D 53 20 20 .ROOMS
0028 20 44 41 54 00 00 00 80  DAT....
0030 02 00 03 00 04 00 05 00 ........
0038 06 00 07 00 08 00 09 00 ........
0040 00 52 4F 4F 4D 53 20 20 .ROOMS
0048 20 44 41 54 01 00 00 48  DAT...H
0050 0A 00 0B 00 0C 00 0D 00 ........
0058 0E 00 00 00 00 00 00 00 ........
0060 00 44 49 53 4B 4D 4F 4E .DISKMON
0068 20 43 4F 4D 00 00 00 10  COM....
0070 0F 00 00 00 00 00 00 00 ........
0078 00 00 00 00 00 00 00 00 ........
> M 1 2 0
WRITTEN
>

Then quit MAME, start it again with the same work.mfi, and:

0:DIR 1:
1: VERSE   .TXT : ROOMS   .DAT
1: DISKMON .COM
30k Size, 158k Free, 190k Total
0:

XtalDOS has never heard of VERSE.TXT - and lists it.

Change One Thing

  • Instead of the name, change the extension: K 9 DOC. What will DIR 1: list? Which BDOS function in S11 would have done the same job?
  • Write the same buffer to M 1 2 1. What will D 1 show now - and why is that a damaged directory?

Summary

Call 163 writes a sector from memory. With it DISKMON changed a file's name by editing the directory sector itself.

Next

Part four takes the DOS away: what the Einstein does when it starts, and a disc that starts DISKMON.

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.