Writing KnightOS programs in C

As of kernel 0.7.0, KnightOS supports userspace programs written in C. Currently, C support is very experimental. Support is provided via kcc and scas. kcc is our fork of sdcc and serves as our C compiler. scas is our new assembler and linker, which is compatible with the ASxxxx syntax that SDCC uses, as well as the classical KnightOS syntax. There are several limitations on C in KnightOS right now:

We hope to reduce some of these problems as C support matures, but consider yourself warned. Here’s what a simple “Hello world” program looks like in C for KnightOS:

#include <system.h>
#include <keyboard.h>
#include <display.h>
#include <corelib.h>

void main(void) {
    SCREEN *screen;
    load_library("/lib/core");
    get_lcd_lock();
    screen = screen_allocate();
    screen_clear(screen);
    draw_window(screen, "C Demo", 0);
    draw_string(screen, 2, 8, "Hello C world!");
    screen_draw(screen);
    flush_keys();
    wait_key();
}

If you’re feeling adventerous enough to give it a try, install kcc and scas and then use knightos init --template=c your_project to get started. To get help and to help us improve C support, join #knightos on irc.libera.chat.