The routine at 0xffd2 which is called chrout will print a character to some output stream. I would like to use this call to print a message to the screen, but I have a file open for output on a disk. According to the documentation, chrout will send my character there.

How can I direct chrout to the screen? Must I close the file first?


Must I close the file first?

No, not unless you already have all 10 possible file entries used up, It depends more on your intention to write a 'clean' program or just want to hack your way ahead (*1).

How can I direct chrout to the screen?

The compatible way is by setting the device the following output is meant to go to. And setting it back afterwards. It's eventually exactly what the BASIC command CMD does when redirecting output.

The function called is CHKOUT (at $FFC9) with the logical file number to be supplied in X (3 in case of the screen).

*1 - Even though this was intended as a rhetorical question, there is always a chance to go brutal ... err ... use an inflexible low level approach. Here it would be using screen functions directly. Here outputting a char (in A) is $E716.

  • 1
    When calling CHKOUT the device number for the screen is 3. – JeremyP Apr 4 '19 at 8:36
  • @JeremyP Added. (I considered that obvious when already digging that deep) – Raffzahn Apr 4 '19 at 11:15

I haven't tried this myself, but I noted that Commodore Peripheral Bus: Part 2: Bus Arbitration, TALK/LISTEN, under KERNAL Channel I/O API, says that device 3 maps to the screen.

Therefore, it should be possible to use CHROUT with a target device of 3 to print to the screen.

I would not imagine that closing the file is necessary as long as you target the correct device, though you may need to mind system state if you are targetting different devices at the same time. Closing and reopening the file may be the cleaner approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.