Classic Computer Magazine Archive COMPUTE! ISSUE 65 / OCTOBER 1985 / PAGE 10

Readers Feedback

The Editors and Readers of COMPUTE!

ML Disk Routine
I need a machine language routine that opens, writes, and properly closes a disk file on a Commodore disk drive.
Rick Elwell

Since we're asked this type of question often, here's a short example that writes a 20-character sequential file to disk, and works with any Commodore computer and disk drive except the 128 in CP/M mode. You'll need a machine language assembler to enter this program. The explanatory comments after the semicolons are, of course, optional:

      LDA   #3       ;Set file number,
      TAY            ;secondary address
      LDX   #8       ;and device number,
      JSR   $FFBA    ;call SETLFS routine.
      LDA   #10      ;Set filename length,
      LDX   #<NAME   ;low byte of filename
      LDY   #>NAME   ;and its high byte,
      JSR   $FFBD    ;call SETNAM routine.
      JSR   $FFC0    ;Call OPEN routine.
      LDX   #3       ;Set file number,
      JSR   $FFC9    ;call CHKOUT routine.
      LDX   #0       ;X is a counter.
WRITE LDA   CHARS,X  ;Get a byte,
      CMP   #255     ;look for end marker,
      BEQ   EXIT     ;quit when found.
      JSR   $FFD2    ;Call CHROUT routine,
      INX            ;bump counter,
      JMP   WRITE    ;write entire text.
EXIT  LDA   #3       ;Set file number,
      JSR   $FFC3    ;call CLOSE routine.
      JSR   $FFCC    ;Call CLRCHN routine.
      RTS
NAME  .ASC "0:FILE,S,W"
CHARS .ASC "THIS IS A TEST FILE."
      .BYT 255

    Though there are other ways to get the job done, it's usually simplest and most reliable to use the computer's built in routines. The SETLFS routine ($FFBA) sets the logical file number, device number, and secondary address, and SETNAM ($FFBD) sets the filename. The filename prefix 0: designates drive 0 and the suffix ,S,W designates a sequential file opened for writing. Different suffixes are used for other operations for instance, the suffix ,S,R would prepare the program to read this file.
    After OPEN ($FFC0) opens the file, CHKOUT ($FFC9) sets it for output (writing). CHKIN ($FFC6) would be used here if you wanted to set the file for input (reading). The file is written one byte at a time with CHROUT ($FFD2). Use CHRIN ($FFCF) or GETIN ($FFE4) to input bytes when reading a file. After the write is complete, CLOSE ($FFC3) closes the file and CLRCHN ($FFCC) restores the system to normal, reenabling keyboard input and screen output. You should always CLOSE every disk file individually. Don't try to use CLALL ($FFE7) as a shortcut: It may create a poison (unclosed) file on the disk.