Skip to content
Toolchain part 1 - Assembly language

Toolchain part 1 - Assembly language

2026-11-28·
Bart Pleiter

The FPGC can now run programs from SDRAM. All those programs, from the boot ROM to the memory test, were written in assembly, but so far I have not explained the language they are written in. This post starts a new series about the toolchain: the tools that turn source code into programs and get them onto the FPGC. The first part is about the language itself.

What an assembly language is

The CPU only understands machine code: 32-bit words, where each group of bits means something. For B32P4, the word 0x03000123 means “add r1 and r2, and put the result in r3”. Writing programs as numbers like this is possible, but it is slow and very easy to get wrong.

An assembly language gives each instruction a name, a mnemonic, and lets you write its operands as text. The same instruction becomes:

add r1 r2 r3

A program called an assembler then turns each line into its word. Almost every line is exactly one instruction, so unlike a C program, you still know exactly what the CPU will do. On top of that, an assembler handles the things that are tedious to do by hand: it remembers the addresses of labels, computes jump distances, and turns text into bytes.

Every CPU has its own assembly language, because every CPU has its own instructions. And even for the same CPU, the syntax is not fixed by the hardware but by the assembler.

The FPGC’s own syntax

Because I am making every part of the FPGC myself, I also define its assembly language. Since I am probably the only person to ever write software for this CPU, I chose a syntax that I found logical and easy to work with. Most modern languages have other definitions of logical and do things like setting the destination register before the source registers, so mine looks a bit different. Part of this is inspired by the assembly language we had to use in the university course on processors I took. The core of the syntax has not changed much, and only extensions have been added over time:

  • Operands are separated by spaces, without commas.
  • The sources come first, and the destination register last: add r1 r2 r3 reads as “add r1 and r2 into r3”, or “r1 + r2 = r3”.
  • Reads and writes are written as offset, base and data: write 4 r1 r2 writes r2 to the address r1 + 4.
  • A label is alone on its line, and a comment starts with ;.

RISC-V would write add x3, x1, x2, while I would write add r1 r2 r3.

A first program

This is the hello world that I used in the previous post. It prints a line over the UART, and then jumps back to the boot ROM:

; Prints a line over the UART, and returns to the boot ROM.

.equ UART_TRANSMIT 0xFFFF8000
.equ BOOT_ROM 0x04000000

    addr2reg Text r1            ; r1 = the address of the text
Next:
    readbu 0 r1 r2              ; r2 = the next character
    beq r2 r0 Done              ; a 0 ends the text
    write UART_TRANSMIT r0 r2
    add r1 1 r1
    jump Next
Done:
    jump BOOT_ROM

Text:
    .ds "hello, world\r\n"

It shows almost everything the language has:

  • .equ gives a name to a number. Here, the address of the UART’s transmit register and the start of the boot ROM.
  • Next:, Done: and Text: are labels: names for the address of what follows them.
  • addr2reg is a pseudo-instruction: it looks like an instruction, but the assembler turns it into two real ones.
  • .ds puts a string into the program as data.

The rest of this post goes through these parts one by one.

Numbers and characters

Numbers can be written in decimal, hex (0x1F) or binary (0b1010), and with _ between the digits to keep long numbers readable: 0xFFFF_8000. There is no octal. In C, 010 is eight, which is super confusing, so here a leading 0 means nothing and 010 is just ten.

A character in single quotes, like 'L', is the number of that character. The boot ROM uses this to check for its load command: load 'L' r2 is easier to read than load 0x4C r2 ; "L", which is what it was before.

Labels and jumps

A label gives a name to an address, so you never have to count instructions yourself. A label can be used before the line that defines it, like Done in the hello world: the assembler figures out the address.

What the assembler puts in the instruction depends on the instruction. B32P4 has two kinds of jumps:

  • An absolute jump goes to an address: “go to 0x04000000”.
  • A relative jump goes a distance from itself: “go back 16 bytes”.

A jump to a label always becomes a relative jump, and so do the branches. In the hello world, jump Next becomes “go back 16 bytes”, no matter where the program is in memory. This makes a program position-independent: it works at any address, without changing a single word. Only a jump to a number, like jump BOOT_ROM, is the absolute jump, because there the address itself is the point. Note that addr2reg (see below) currently still block full position-independence, as it generates absolute addresses, I might revise this later and make it use savpc or similar techniques to compute addresses relative to the program counter.

Position-independence is not needed yet, as every program runs from address 0. But it will be useful later, when an operating system loads programs at different addresses. With an OS, multiple programs need to be able to run in memory at different addresses, since I will not be implementing a TLB and virtual memory.

Pseudo-instructions

Every B32P4 instruction is 32 bits, and some of those bits are needed for the opcode and the registers. So an instruction cannot hold a full 32-bit number: the constant of an ALU instruction is 16 bits. To get any 32-bit number into a register, it takes two instructions: load puts the low 16 bits in a register, and loadhi the high 16 bits.

Writing these pairs by hand is tedious, so the assembler has a few pseudo-instructions:

WrittenBecomes
load32 0x12345678 r1load 0x5678 r1 and loadhi 0x1234 r1, or only the load if the high half is 0
addr2reg Text r1the same, with the two halves of the address of Text
nopan instruction that does nothing

Real CPUs do the same: RISC-V has li (load immediate), which its assembler turns into a lui and an addi, and ARM has movw and movt for the two halves.

Data and strings

Data goes into the program with .dw, one 32-bit word per value. .ds puts a string, four characters per word, the first in the lowest byte:

Text:
    .ds "hello!"

address    byte 0   byte 1   byte 2   byte 3
Text       'h'      'e'      'l'      'l'
Text+4     'o'      '!'      0        0

A string always ends with a zero byte, and is padded with zeros to a whole word. This is the same way C stores strings, which will be useful once the C compiler is added. There are other ways to know where a string ends, like strings that start with their length, but a terminating zero makes the loop simple: read a byte, stop when it is 0. That is exactly what the hello world does with readbu, which reads a single byte.

In the early FPGC versions before it was byte-addressable, I stored one character per word, which made the print loop even simpler, but it takes four times the memory. Now that the CPU has byte reads, packing them is the obvious choice.

There is one thing to keep in mind: data sits in the program exactly where it is written. The CPU does not know the difference between instructions and data, so if it ever gets to a string, it will try to run it. That is why the text in the hello world is after the last jump. At some point I will likely add sections to the assembly language again to separate code and data more explicitly which allow the assembler to reposition it. I skipped that for now as I do not need it yet, but for the C compiler that will probably change.

Constants

.equ NAME value gives a name to a number. This is mostly useful for the addresses of the device registers: write UART_TRANSMIT r0 r2 says what it does, write 0xFFFF8000 r0 r2 does not.

The name equ is short for “equate”, and goes back to the assemblers of the 1960s. You can basically think of it like a C-style #define statement but for assemblers. A constant must be defined before it is used, just like a define in C.

Calling functions

B32P4 has no call instruction. Instead, a function call takes two instructions:

    savpc 8 r15                 ; r15 = the address after the jump
    jump PrintText
    ...                         ; PrintText returns here

PrintText:
    ...
    jumpr 0 r15                 ; back to the caller

savpc saves the address of the instruction itself plus 8 in a register: the address of the instruction after the jump. The function then returns by jumping to that address with jumpr. When a function calls another function, it first pushes r15 on the hardware stack, and pops it back before it returns.

This is how I wrote assembly in the years before the FPGC had a C compiler: r15 for the return address, and the hardware stack to save registers. Other CPUs often have a call instruction: x86’s call pushes the return address on the stack in memory, and RISC-V’s jal puts it in a register, like savpc does here. Doing it with two instructions shows exactly what happens in a call, and the CPU stays simpler. I could add a pseudo-instruction call that expands to these two instructions, but for flexibility of return register and visibility of what happens I am leaving it like this.

Which registers a function uses for its arguments and its result is a convention, not something the CPU or the assembler decides. For hand-written assembly, writing above each function which registers it uses is enough. A full calling convention will be needed once the C compiler is added.

What is not there

The language is small on purpose. There are no expressions like Label + 4, no macros, and a program is always one file. I will only add these features if I need them or if they outweight the added complexity in the Assembler. Supporting multiple files and using a linker is one I am considering for larger assembly-written NES-style games I might want to write once the GPU is in place.

Next steps

With the language defined, the next post is about the assembler: how it turns these lines into the words that the CPU runs.

Last updated on