The C library

The C library is four files of programs/lib, built into terse (terse build --write-lib <dir> writes them out): the start code, the linker script, and terse.h with terse.c, which give every call of the machine as a C function. This article describes them, then the hello program in assembly, without the library, and in C. How they are compiled and linked is in "Building a program", the article before this one.

The library

What terse.h provides, by area:

There is no other C library and no compiler runtime. Avoid floating point and 64-bit division, which need runtime routines that are not there (the link fails), or bring the routines yourself: crates/terse-core/tests/softfloat/softfloat.c is a tested IEEE double add, subtract, multiply and divide in integer C.

The hello program

programs/hello is written in assembly, hello.S, without the library: it shows how bare the machine is. Its build.sh assembles it with the same flags, links it alone with terse.ld, and packs it with one RAM page and the terminal (--device 2:0). It does this:

A test compares its screen after 30 frames with expected-30-frames.txt.

The same in C

programs/hello/hello.c is the whole hello program in C. Its core, typing and Esc, is this:

#include "terse.h"

int main(void)
{
    terse_event e;
    term_color(15, 4);
    term_puts(" hello");
    term_cmd(TERM_CLEAR_EOL);
    term_color(7, 0);
    term_goto(0, 2);
    term_puts("> ");
    for (;;) {
        terse_wait_frame();
        while (terse_event_pop(&e)) {
            if (e.type == EV_TEXT) {
                uint32_t cp = e.a | e.b << 8
                    | (uint32_t)(e.c & 0x1F) << 16;
                term_put_cp(cp > 0xFFFF ? 0xFFFD : cp);
            } else if (e.type == EV_KEY_DOWN) {
                if (e.a == KEY_ENTER)
                    term_puts("\n> ");
                else if (e.a == KEY_ESCAPE)
                    return 0;
            }
        }
    }
}

Next: Rust programs.