Headers, Libraries, and Macros

In this chapter we will split away parts of our "Hello World" program into reusable header files and libraries. We will also abstract away our string printing technique into a macro which may be invoked at will, on arbitrary strings. We will then multiply the output of our program tenfold.

Header files and libraries

The prelude to our program—the PRG information and the BASIC program—are going to be the same in many, many programs. Thus, we should put them into a header file to be included later. The .include directive will load a file and insert it as source at the designated point.

A related directive, .require, will include the file as long as it hasn't been included yet elsewhere. It is useful for ensuring a library is linked in.

For pre-assembled code or raw binary data, the .incbin directive lets you include the contents of a binary file directly in the output. This is handy for linking in pre-created graphics or sound data.

If you only wish to include part of a binary file, .incbin takes up to two optional arguments, naming the file offset at which to start reading and the number of characters to read.

As a sample library, we will expand the definition of the chrout routine to include the standard names for every KERNAL routine. Our header file will then .require it.

We'll also add some convenience aliases for things like reverse video, color changes, and shifting between upper case/graphics and mixed case text. We'd feed those to the chrout routine to get their effects.

Since there have been no interesting changes to the prelude, and the KERNAL values are standard, we do not reproduce them here. (The files in question are c64-1.oph and c64kernal.oph.) The c64kernal.oph header is likely to be useful in your own projects, and it is available in the platform/ directory for easy inclusion.