Assembly
QCPU language reference
QCPU CLI is a tool which includes an assembler, linker and emulator for the QCPU 2 specification.
Often the most efficient way to learn something new is to study and work with examples, so this documentation shows how to use each of QCPU CLI’s (language) features. QCPU CLI is created with the Zig programming language and, besides the compiler itself, it can be compiled without any other dependencies.
For starters, running --help shows the different options you may use:
QCPU CLI
qcpu [option ...] file ...
general options
--version
--help
--docs string
--doptions
--verbose
output options
--binary
--verilog
--virtualise
compilation unit options
--dast
--dir
--dair
--dlinker
--noliveness
--noelimination
--rootsection string (default root)
QCPU-CLI v0.1.0
1 Instructions
Like most assembly languages, instruction lines generally look like the following:
label: instr op, op, op ; comments col 80There are two supported types of comments, prefixed with ; and //. QCPU assembly doesn’t support multiline comments (like /* */ in C). This allows the assembler to tokenise any line out of context.
Instructions are taken from the instruction reference and operands are separated by commas:
move d1, (sp)++ ; pop from stack2 Getting started
A QCPU assembly program contains sections, which define the instructions and/or data for the program, as well as a linking order for those sections. To aid in eliminating dead code paths (for instance in a multi-target project or using a generic static library), QCPU CLI lazily evaluates the assembly program through label references.
The root section defines the start of the assembly tree. References made to other section blocks are descended from the root tree.
@section root
_: bkpt
@linkinfo(origin) root, 0$ qcpu --dlinker snippets/root.s
Linker:
@section root (2 bytes)
0x0000 0b0000000000000000 bkptQCPU 2 begins executing instructions at instruction pointer 0x0000. Therefore, the root section’s linked addresses are based from zero.
2.1 Sections
Sections can be added to the program by defining them with @section <name>.
@section root
_: bkpt
@linkinfo(origin) root, 0
@section text
_: bkpt$ qcpu --dlinker snippets/sections.s
Linker:
@section root (2 bytes)
0x0000 0b0000000000000000 bkptThis, however, did not change the output of the program. The content in the text section’s block was not referenced by the root tree. In order to do that, the root section may jump to (or otherwise reference) content in the text section.
@section root
_: jmpr .foo
@linkinfo(origin) root, 0
@section text
.foo: bkpt$ qcpu snippets/sections-1.s
snippets/sections-1.s:8:1: error: section 'text' was referenced but not linked
@section text
^~~~~~~~
snippets/sections-1.s:4:19: note: first referenced in section 'root' here
_: jmpr .foo
^~~~
snippets/sections-1.s:6:1: note: which is linked here
@linkinfo(origin) root, 0
^~~~~~~~~
snippets/sections-1.s:8:1: note: use '@linkinfo(...) text, ...' to link this sectionThe assembler complains. Not surprising, because whilst a block of the text section was referenced through the .foo label in the root section, the text section is not placed in memory. To do this, we must add a @linkinfo entry which defines our new section somewhere in memory.
By aligning the text section by 2 (the alignment of a QCPU 2 instruction), the assembler will place it behind the root section.
; ... (omitted)
@linkinfo(origin) root, 0
@linkinfo(aligned) text, 2
; ... (omitted)$ qcpu --dlinker snippets/sections-2.s
Linker:
@section root (2 bytes)
0x0000 0b0001000000001001 addi ip, .foo + 0
@section text (2 bytes)
0x0002 0b0000000000000000 bkpt
sections-2.foo -> 0x0002Tada!
2.2 Non-eliminating sections
In certain given circumstances, some blocks must always exist in memory even if they seem not referenced. For example, an interrupt handler may not be referenced by the root tree, but is executed by the CPU when an interrupt is offered.
@section root
_: bkpt
@linkinfo(origin) root, 0
@linkinfo(origin) int, 512
@section(noelimination) int
_: bkpt$ qcpu --dlinker snippets/noelimination.s
Linker:
@section root (2 bytes)
0x0000 0b0000000000000000 bkpt
@section int (2 bytes)
0x0200 0b0000000000000000 bkpt2.3 Macros
Macros are a way to reuse expressions (through @defines) and blocks of instructions and/or data (through @headers). Expressions are used within built-ins and instructions. Any operand is an expression which can be evaluated as well as passed in headers.
@define foo, 5
@define bar, foo + 3
@section root
_: lli d1, bar
bkpt
@linkinfo(origin) root, 0$ qcpu --dlinker snippets/define.s
Linker:
@section root (4 bytes)
0x0000 0b1001000001000010 lli d1, 8
0x0002 0b0000000000000000 bkptHeaders are commonly used for defining data structures, label interfaces, and inline routines.
@header Queue, len
@align 2
head: u16 0
tail: u16 0
memory: reserve u8, len
@end
@section root
_: movew d1, .queue.head
bkpt
@linkinfo(origin) root, 0
@linkinfo(aligned) data, 1
@section data
@define x, 16
.queue: Queue x
.queue2: Queue 2 * x$ qcpu --dlinker snippets/header.s
Linker:
@section root (4 bytes)
0x0000 0b1110000010000010 movew d1, (.queue.head + 0)(zr)
0x0002 0b0000000000000000 bkpt
@section data (56 bytes)
0x0004 0x0 (0)
0x0006 0x0 (0)
0x0008 * 16 bytes
0x0018 0x0 (0)
0x001A 0x0 (0)
0x001C * 32 bytes
header.queue.tail -> 0x0006
header.queue -> 0x0004
header.queue.memory -> 0x0008
header.queue2.tail -> 0x001A
header.queue2.memory -> 0x001C
header.queue2.head -> 0x0018
header.queue.head -> 0x0004
header.queue2 -> 0x00183 Built-ins
Builtins are provided by the assembler and are prefixed with @.
The following documentation is in alphabetical order, but it makes more sense to start to understand @section and @linkinfo before anything else, as those builtins provide the foundation for the assembly language. However, the ‘Getting started’ section already laid those out for you, so you’re in luck. The underlying examples are incomplete, and the full examples can be found in as snippets/builtin-*.s.
3.1 @align
@align <alignment>Emits some amount of padding bytes to conform to the set alignment during linkage. <alignment> must be positive, non-zero, and a power of two.
@section example
_: u8 0xEA
@align 2
_: u16 0xDEAD$ qcpu --dlinker --noelimination snippets/builtin-align.s
... (omitted)
@section example (5 bytes)
0x0003 * 1 byte
0x0004 0xEA (234)
0x0005 * 1 byte
0x0006 0xDEAD (57005)3.2 @barrier
@barrier
@barrier(noelimination)Splits the current section into another block, to mark non-continuous execution flow, which allows dead-code elimination to further optimise the output. Alias of @section <same section name>.
The noelimination option ensures the block is not optimised away during semantic analysis.
@section a
.foo: bkpt ; referenced
@section b
.bar: bkpt ; not referenced$ qcpu --dlinker snippets/builtin-barrier.s
... (omitted)
@section example (2 bytes)
0x0002 0b0000000000000000 bkpt
builtin-barrier.foo -> 0x00023.3 @define
@define <name>, <expression>
@define(expose) <name>, <expression>Defines <name> as a symbol to reuse <expression>.
The expose option makes this macro public for @import.
@define(expose) foo, 0xBEEF
@section root
_: lui d1, foo'u$ qcpu --dlinker snippets/builtin-define.s
Linker:
@section root (4 bytes)
0x0000 0b1001110111110010 lui d1, 48640
... (omitted)3.4 @else
@elseElse prong of an @if statement.
3.5 @end
@endClosing bracket for a @header, @region, and @if.
3.6 @err
@err <message>, [formatting expressions ...]Generates an assemble error when evaluated by the semantic analyser.
This may be used in conjunction with @if for assemble-time validation, or to signal deprecation errors when an old block or @header is referenced.
@header Queue, len
@align 2
@if len < 1
@err "Queue len may not be zero or negative; found %d", len
@end
head: u16 0
tail: u16 0
memory: reserve u8, len
@end
@section root
_: Queue 0$ qcpu snippets/builtin-err.s
snippets/builtin-err.s:6:7: error: Queue len may not be zero or negative; found 0
@err "Queue len may not be zero or negative; found %d", len
^~~~
snippets/builtin-err.s:5:9: note: conditionally evaluated from here
@if len < 1
^
snippets/builtin-err.s:16:19: note: Queue unrolled from here
_: Queue 0
^~~~~The following formatting arguments are available:
%d- discrete integer%b- binary%h- hexadecimal%s- string%a- any, internal representation of argument
3.7 @header
@header <name>, [parameters ...]
@header(expose) <name>, [parameters ...]Defines <name> as a symbol to reuse the block of instructions and/or data, including labels when the header is unrolled with a namespace label.
The expose option makes this macro public for @import.
@header mutex_ulock, addr
xch zr, addr
@end
@section root
_: mutex_ulock a1$ qcpu --dlinker snippets/builtin-header.s
Linker:
@section root (4 bytes)
0x0000 0b1100100000100000 xch zr, 0(a1)
... (omitted)3.8 @if
@if <expression>Conditionally evaluates the body if <expression> evaluates non-zero.
@if feature_enabled
addi d1, 1
@end$ qcpu --dlinker snippets/builtin-if.s
Linker:
@section root (4 bytes)
0x0000 0b0000100000001010 addi d1, 1
... (omitted)3.9 @import
@import <name>, <file path>
@import(expose) <name>, <file path>Imports the public symbols of another file into a namespace.
The expose option makes this imported namespace public for @import.
@import namespace, "builtin-define.s"
@section root
_: u16 namespace.foo$ qcpu --dlinker snippets/builtin-import.s
Linker:
@section root (6 bytes)
0x0000 0xBEEF (48879)
... (omitted)3.10 @linkinfo
@linkinfo(physically) <address>
@linkinfo(origin) <name>, <address>
@linkinfo(origin, expose) <name>, <address>
@linkinfo(aligned) <name>, <alignment>
@linkinfo(aligned, expose) <name>, <alignment>@linkinfo executes a linker command. The order of commands matter, as the addresses of the section layout must be ascending, and may not overlap. Linker commands may only be defined in a single file. The following linker commands are available:
physically- sets the physical address origin for Verilog and Virtualiser outputsorigin- places the section at an exact addressaligned- places the section at the next available alignment
The expose option makes the section label (created as <name>) public for @import.
@linkinfo(origin) root, 0
@linkinfo(aligned) text, 256
@linkinfo(aligned) data, 256$ qcpu --dlinker snippets/builtin-linkinfo.s
Linker:
@section root (2 bytes)
0x0000 0b0000000000000000 bkpt
@section text (0 bytes)
@section data (0 bytes)3.11 @region
@region <length>Ensures a region is a set size. Emits padding bytes if the opaque space is less than the set size, and provides an error if the opaque space exceeds the set size.
@section root
@region 256
_: u16 0xDEAD
u16 0xBEEF
@end$ qcpu --dlinker snippets/builtin-region.s
Linker:
@section root (256 bytes)
0x0000 0xDEAD (57005)
0x0002 0xBEEF (48879)
0x0004 * 252 bytes3.12 @section
@section <name>
@section(noelimination) <name>Adds a section to the program. The body of the section is until the next @barrier definition, another @section definition, or the end of file. <name> is not required to be unique.
The noelimination option ensures the block is not optimised away during semantic analysis.
4 Expression evaluation
The assembly language defines various types of expressions:
- Scalar - constant integers (e.g.
42,5 + 3) - Address - a label reference with an optional offset and mask (e.g.
.foo,.foo + 1,.foo'l) - Instruction - an instruction argument (e.g.
move,Queue), as instructions and headers may be passed as operands - Register - a register (e.g.
d1,a1,ip) - Index - an AR-offset address (e.g.
.foo(a1),16(fp),(a1)++) - Flag - a CPU condition code (e.g.
cfor carry,nzfor not zero) - Size - a data type (e.g.
u8,u16) - String - a string argument (e.g.
"Hello world!")
The following unary, post-unary, and binary operations are available:
- Negation -
!0xEA - Inversion -
-5 - Mask -
.foo'u(upper byte),.foo'l(lower byte) - Predecrement -
--(a1) - Postincrement -
(a1)++ - Addition -
5 + 3,1 + .foo - Subtraction -
5 - 3,.foo - 1 - Multiplication -
5 * 3 - Bitwise OR -
5 | 3 - Bitwise AND -
5 & 3 - Logical left shift -
5 << 3 - Logical right shift -
5 >> 3 - Less than -
5 < 3 - Greater than -
5 > 3 - Equals -
5 = 3
5 Liveness
…