Tag: compiler

A comprehensive small C compiler

A comprehensive small C compiler

Programming image
Image by Gerd Altmann from Pixabay

Once you’ve learnt a fair bit of C, one way to increase your understanding of it is to see how a C compiler works. Unlike most other programming languages, C compilers are generally understandable. There have been a few tiny or small C compilers around and here is another one though as its C111 compatible, it might be more useful than older ones.

The author is well qualified to write this, he’s the author of the llvm lld linker and also an earlier C compiler 8CC. He notes “chibicc can compile several real-world programs, including GitSQLite and libpng, without making modifications to the compiled programs. Generated executables of these programs pass their corresponding test suites.”

Rui is writing a book on the C compiler and this is the subject of the book. It’s not out yet but will be in my must buy list.

Who needs asm? cc65 C compiler

Who needs asm? cc65 C compiler

Commodore Computer
Image by OpenClipart-Vectors from Pixabay

Back in the day 1984-1989, I wrote games in Z80 and 6502 assembly. It would be another 10 years before I learnt C++ and ten years after that for C. But I would have killed to have had a C compiler that generated 6502 code back then. Writing assembly language code is slow and painstaking. You have to remember whats in each register (A,X and Y) and what you are using page 0 locations for.

It takes the same length of time to write and debug 10 lines of 6502 assembler as it does to write 10 lines of C.  But those ten lines of C can do way more than 10 lines of 6502 assembly.

On a 6502 which is an 8 bit CPU with 16-bit addressing, memory goes from 0-65535. It takes two bytes for an address unless it is in the first 256 bytes (0-255) which only needs 1 byte. You can optimise your code by using page 0 for many variables. It only takes 2-byte instructions to fetch them from memory and write them back. The first byte is the instruction, the 2nd the address. Compare that with the rest of memory where instructions are three bytes long with 2-bytes for the address.

CC65 is a C compiler that generates 6502 code. It’s mostly written in C. I’ve added a permanent link to it on the C Codes link page. Pictured is a CBM-64 like the one I used to own.