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
  • 1
    @JeremyP: Aren't the file number and the device number two different things? – TeaRex Mar 30 '20 at 13:35

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.


If you have activated a logical file as the current output channel (using CHKOUT or $FFC9), you reset both active input and output channels to default using CLRCHN a.k.a. CLRCH, $FFCC.

After that, BSOUT $FFD2 will print to the screen.

This will not have closed the file, it is still open. You can re-activate it again with CHKOUT and eventually close it with CLOSE, $FFC3.

There is no need to use any of the bus-related functions such as TALK or LISTEN: they are the wrong abstraction level here. They come in handy if you want to talk to a disk drive or printer but not use the file administration for it.

Another answer confuses the device number for the screen (which is 3) with logical file numbers. If you open a file, you basically create a mapping from a LFN (logical file number) to a device number (and possibly a secondary address, but those are too far out of the scope of printing to the screen).

There are various books available about machine language programming on the 64, such as "(Compute's) Programming the Commodore 64, The definitive guide" by Raeto Collin West. I have "Programming the PET/CBM" by the same author and it is very thorough.

Here on archive.org are some more.

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.