Skip to content
Toolchain part 2 - Assembler

Toolchain part 2 - Assembler

2026-11-28·
Bart Pleiter

The previous post described the assembly language of the FPGC. This post is about the assembler: the program that turns that language into the words the CPU runs. It is a small program, and works using a simple two-pass approach to handle labels.

A small assembler

The assembler, b32asm, is written in Python. It is about 650 lines, split over three parts:

  • a table of the instruction set: every instruction type, its fields, and which bits they take in the word
  • the assembler itself: reading the lines, and turning them into words
  • the command that reads a file and writes the binary image

It is written as a few plain functions, without much structure on top. This is on purpose: at some point, the FPGC should be able to assemble its own programs, with an assembler written in C that runs on the FPGC itself (as it did in previous FPGC versions). A simple design in Python is easy to work on now, and as long as I try to limit object-oriented code, it will be easy to translate to C later.

The output is a binary image: every word of the program in order, four bytes each, lowest byte first. There is no header or anything else in the file: the first four bytes are the first instruction.

The problem with labels

Turning one line into one word is mostly a lookup: find the instruction in the table, put each operand in its field. The difficulty is labels. Take this part of the hello world from the previous post:

Next:
    readbu 0 r1 r2
    beq r2 r0 Done              ; the label Done comes later
    write UART_TRANSMIT r0 r2
    add r1 1 r1
    jump Next
Done:
    jump BOOT_ROM

The beq has to contain the distance to Done. But when the assembler reads the beq, it has not seen Done yet, so it does not know where it is.

There are a few ways to solve this:

  • Two passes: read the whole source once to find the address of every label, and then a second time to make the words. This is what most assemblers do.
  • One pass with fixups: make the words in one go, leave a hole for every label that is not known yet, and fill in the holes at the end. Fast, as the source is read only once, which mattered when it was read from a tape.
  • More passes: on x86, a jump to a nearby label can be a short instruction of 2 bytes, and to a far one a longer one of 5 or 6 bytes. But which one it is changes the addresses of everything after it, which can change other jumps again. So x86 assemblers repeat until nothing changes anymore.

On B32P4, every instruction is one word, so the address of every line is known without knowing any label. That makes two passes always enough, and two passes is the easiest to implement, so that is what b32asm does.

Pass 1

The first pass reads each line, and turns it into statements of exactly one word each:

  • The comment is removed (except a ; inside a string, which is just a character).
  • A label gets the address of the next statement, and adds no word.
  • .equ stores the constant’s value, and from then on the name is replaced by that number.
  • A pseudo-instruction becomes the real instructions it stands for: load32 one or two, addr2reg always two.
  • .ds becomes a data word per four characters.

Each statement gets its address: the base address plus four times the number of statements before it. For the hello world, the result is:

address   statement                    labels
0x00      load    Text[15:0] r1
0x04      loadhi  Text[31:16] r1
0x08      readbu  0 r1 r2              Next = 0x08
0x0C      beq     r2 r0 Done
0x10      write   0xFFFF8000 r0 r2
0x14      add     r1 1 r1
0x18      jump    Next
0x1C      jump    0x04000000           Done = 0x1C
0x20      .dw     'h' 'e' 'l' 'l'      Text = 0x20
0x24      .dw     'o' ',' ' ' 'w'
0x28      .dw     'o' 'r' 'l' 'd'
0x2C      .dw     '\r' '\n' 0 0

This means that a constant must be defined before it is used. load32 becomes one instruction when the high 16 bits of its number are 0, and two otherwise. If the number were a constant that is defined further down, pass 1 would not know how many words the load32 takes, and so it would not know the address of anything after it. Requiring constants to come first keeps it at two passes.

For the same reason, addr2reg is always two instructions, even when the high half of the address is 0. The address of a label is only known after pass 1, so pass 1 cannot decide to leave the second instruction out.

Pass 2

In the second pass, every label is known, and each statement becomes its word. The fields of the instruction are put in their place, as described in the instruction set table:

write 0xFFFF8000 r0 r2

  bits     31:28    27:12               11:8   7:4    3:0
  field    WRITE    constant            A      B      size
  value    1101     1000000000000000    0000   0010   0000
                                                        = 0xD8000020

A small detail in this one: the 16-bit constant field holds 0x8000, not 0xFFFF8000. The CPU sign-extends the constant: it copies its highest bit into the upper 16 bits, so 0x8000 becomes 0xFFFF8000 again. The assembler accepts both ways of writing it.

For a label, what goes in the word depends on the instruction. A branch or a jump gets the distance from itself to the label: the beq at 0x0C to Done at 0x1C is +16, and jump Next at 0x18 is -16. The addr2reg instructions get the two halves of the full address of Text.

Pass 2 also checks that each value fits in its field. A branch has 16 bits for its distance, so it reaches 32 KiB back or forward. A jump has 27 bits and reaches 64 MiB, the whole SDRAM. A number that does not fit is an error, not something the assembler quietly cuts off.

Errors

When something is wrong, the assembler stops at the first error, with the file, the line and what is wrong:

error: hello.asm:12: unknown label 'Nxt'

Many assemblers continue after an error to report as many as possible. For programs of a few hundred lines, stopping at the first one is simpler, and fixing one error at a time is fast enough.

Next steps

The assembler gives a binary image of the program on the PC. The next post is about how that image gets into the SDRAM of the FPGC.

Last updated on