Instruction set¶
SAPHO is an accumulator machine: almost every instruction combines the
accumulator a with one operand x and leaves the result in a. The
operand is a data word named in the instruction (ADD k), or for the
S_/SF_ forms the top of the data stack, which the instruction pops.
The order matters for the operations that are not commutative: LES k
gives k < a, F_SU2 k gives k - a, DIV k divides k by a.
The names follow one convention: F marks a float operation; a P_/PF_
prefix pushes the accumulator first; an S_/SF_ prefix takes the operand
off the stack; an _M suffix applies a unary operation to the data word
instead of the accumulator; a _V suffix adds a constant offset to the
address (an array element at a known index). Several mnemonics share an
opcode and differ only in how the assembler reads the operand.
A processor contains only the hardware of the instructions its program
uses. An integer division takes five words, DIV k; NOP; NOP; NOP; QUO
(also MOD/REM), a float division four, F_DIV k; NOP; NOP; F_QUO:
the compilers write it that way, because the dividers take five and four
cycles. A JIZ, LDI or ILI right after an operation whose result goes through the
float normaliser (F_ADD, F_SU1, F_SU2, F_MLT, F_DIV/F_QUO,
F_ROT, I2F, I2F_M, F_INN and their stack, P_ and _V forms) gets
a NOP before it: the jump and the read index take the arithmetic unit
before the normaliser, and asmcomp inserts the NOP by itself, in a
hand-written .asm too. The compilers never write that pattern: they
test a float condition after F2I, and an index is an integer.
Columns: operand, what the operand field holds (data a data address,
code an instruction address, in/out a port, link a link number,
offset an address plus a constant, lea an address taken as a constant);
a, what the instruction does to the accumulator (r reads, w writes);
stack, push or pop of one data-stack word; x, what it does to the
data word named (r, w, base_r/base_w an unknown word of the array
named, addr its address only).
Memory and stack¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
0 |
none |
- |
- |
- |
nothing |
|
1 |
lea |
w |
- |
addr |
a = the address of x (a constant) |
|
1 |
data |
w |
- |
r |
a = x |
|
1 |
offset |
w |
- |
r |
as LOD, x at a constant offset from the name |
|
2 |
data |
rw |
push |
r |
push a; a = x |
|
2 |
offset |
rw |
push |
r |
as P_LOD, x at a constant offset from the name |
|
3 |
data |
rw |
- |
base_r |
a = mem[x + a] (x a base: an array, or a number) |
|
3 |
offset |
rw |
- |
base_r |
as LDI, x at a constant offset from the name |
|
4 |
data |
rw |
- |
base_r |
a = mem[x + bit-reversed a] (FFT addressing) |
|
5 |
data |
r |
- |
w |
x = a |
|
5 |
offset |
r |
- |
w |
as SET, x at a constant offset from the name |
|
6 |
data |
rw |
pop |
w |
x = a; a = pop |
|
7 |
data |
r |
pop |
base_w |
mem[x + pop] = a (the index is popped) |
|
7 |
offset |
r |
pop |
base_w |
as STI, x at a constant offset from the name |
|
8 |
data |
r |
pop |
base_w |
mem[x + bit-reversed pop] = a (FFT addressing) |
|
9 |
none |
r |
push |
- |
push a |
|
10 |
none |
w |
pop |
- |
a = pop |
Input and output¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
11 |
in |
w |
- |
- |
a = input port x (int) |
|
12 |
in |
w |
- |
- |
a = input port x (float) |
|
13 |
in |
rw |
push |
- |
push a; a = input port x |
|
14 |
in |
rw |
push |
- |
push a; a = input port x (float) |
|
15 |
out |
r |
- |
- |
output port x = a |
Flow control and links¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
16 |
code |
- |
- |
- |
go to x |
|
17 |
code |
r |
- |
- |
go to x when a is 0 |
|
18 |
code |
- |
- |
- |
call x (the return address is pushed on the subroutine stack) |
|
19 |
none |
- |
- |
- |
return to the address the last CAL pushed |
|
20 |
link |
r |
- |
- |
offer a on link x; wait here until the partner takes it |
|
21 |
link |
w |
- |
- |
wait here until link x offers a word; a = that word |
Integer arithmetic¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
22 |
data |
rw |
- |
r |
a = a + x |
|
22 |
offset |
rw |
- |
r |
as ADD, x at a constant offset from the name |
|
23 |
none |
rw |
pop |
- |
a = a + x |
|
24 |
data |
rw |
- |
r |
a = x * a |
|
24 |
offset |
rw |
- |
r |
as MLT, x at a constant offset from the name |
|
25 |
none |
rw |
pop |
- |
a = x * a |
|
26 |
data |
rw |
- |
r |
starts x / a (integer quotient, toward zero); read it with QUO four words later |
|
26 |
none |
w |
- |
- |
a = the quotient of the DIV four words before |
|
27 |
none |
rw |
pop |
- |
starts x / a (integer quotient, toward zero); read it with QUO four words later |
|
28 |
data |
rw |
- |
r |
starts x % a (remainder, the sign of x); read it with REM four words later |
|
28 |
none |
w |
- |
- |
a = the remainder of the MOD four words before |
|
29 |
none |
rw |
pop |
- |
starts x % a (remainder, the sign of x); read it with REM four words later |
|
30 |
data |
rw |
- |
r |
a = a with the sign of x |
|
31 |
none |
rw |
pop |
- |
a = a with the sign of x |
Float arithmetic¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
32 |
data |
rw |
- |
r |
a = x + a |
|
32 |
offset |
rw |
- |
r |
as F_ADD, x at a constant offset from the name |
|
33 |
none |
rw |
pop |
- |
a = x + a |
|
34 |
data |
rw |
- |
r |
a = a - x |
|
35 |
data |
rw |
- |
r |
a = x - a |
|
36 |
none |
rw |
pop |
- |
a = a - x |
|
37 |
none |
rw |
pop |
- |
a = x - a |
|
38 |
data |
rw |
- |
r |
a = x * a |
|
38 |
offset |
rw |
- |
r |
as F_MLT, x at a constant offset from the name |
|
39 |
none |
rw |
pop |
- |
a = x * a |
|
40 |
data |
rw |
- |
r |
starts x / a; read it with F_QUO three words later |
|
40 |
none |
w |
- |
- |
a = the quotient of the F_DIV three words before |
|
41 |
none |
rw |
pop |
- |
starts x / a; read it with F_QUO three words later |
|
42 |
data |
rw |
- |
r |
a = a with the sign of x |
|
43 |
none |
rw |
pop |
- |
a = a with the sign of x |
|
44 |
data |
rw |
- |
r |
a = a * 2^x (x an int, a a float) |
|
45 |
none |
rw |
pop |
- |
a = a * 2^x (x an int, a a float) |
Integer unary¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
46 |
none |
rw |
- |
- |
a = -a |
|
47 |
data |
w |
- |
r |
a = -x |
|
48 |
data |
rw |
push |
r |
push a; a = -x |
|
49 |
none |
rw |
- |
- |
a = |a| |
|
50 |
data |
w |
- |
r |
a = |x| |
|
51 |
data |
rw |
push |
r |
push a; a = |x| |
|
52 |
none |
rw |
- |
- |
a = a, or 0 when a is negative |
|
53 |
data |
w |
- |
r |
a = x, or 0 when x is negative |
|
54 |
data |
rw |
push |
r |
push a; a = x, or 0 when x is negative |
|
55 |
none |
rw |
- |
- |
a = a / NUGAIN (the #NUGAIN directive, a power of two) |
|
56 |
data |
w |
- |
r |
a = x / NUGAIN (the #NUGAIN directive, a power of two) |
|
57 |
data |
rw |
push |
r |
push a; a = x / NUGAIN (the #NUGAIN directive, a power of two) |
|
58 |
none |
rw |
- |
- |
a = ~a (bitwise not) |
|
59 |
data |
w |
- |
r |
a = ~x (bitwise not) |
|
60 |
data |
rw |
push |
r |
push a; a = ~x (bitwise not) |
|
61 |
none |
rw |
- |
- |
a = !a (1 or 0) |
|
62 |
data |
w |
- |
r |
a = !x (1 or 0) |
|
63 |
data |
rw |
push |
r |
push a; a = !x (1 or 0) |
Float unary¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
64 |
none |
rw |
- |
- |
a = -a, float |
|
65 |
data |
w |
- |
r |
a = -x, float |
|
66 |
data |
rw |
push |
r |
push a; a = -x, float |
|
67 |
none |
rw |
- |
- |
a = |a|, float |
|
68 |
data |
w |
- |
r |
a = |x|, float |
|
69 |
data |
rw |
push |
r |
push a; a = |x|, float |
|
70 |
none |
rw |
- |
- |
a = a, or 0 when a is negative, float |
|
71 |
data |
w |
- |
r |
a = x, or 0 when x is negative, float |
|
72 |
data |
rw |
push |
r |
push a; a = x, or 0 when x is negative, float |
|
73 |
none |
rw |
- |
- |
the power of two nearest to the square root of the float a |
|
74 |
none |
rw |
- |
- |
a = the base-2 exponent of the float a, as an int |
|
75 |
data |
w |
- |
r |
a = the base-2 exponent of the float x, as an int |
Conversions¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
76 |
none |
rw |
- |
- |
a = a converted from int to float |
|
77 |
data |
w |
- |
r |
a = x converted from int to float |
|
78 |
data |
rw |
push |
r |
push a; a = x converted from int to float |
|
79 |
none |
rw |
- |
- |
a = a converted from float to int |
|
80 |
data |
w |
- |
r |
a = x converted from float to int |
|
81 |
data |
rw |
push |
r |
push a; a = x converted from float to int |
Logic¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
82 |
data |
rw |
- |
r |
a = x & a (bitwise) |
|
83 |
none |
rw |
pop |
- |
a = x & a (bitwise) |
|
84 |
data |
rw |
- |
r |
a = x | a (bitwise) |
|
85 |
none |
rw |
pop |
- |
a = x | a (bitwise) |
|
86 |
data |
rw |
- |
r |
a = x ^ a (bitwise) |
|
87 |
none |
rw |
pop |
- |
a = x ^ a (bitwise) |
|
88 |
data |
rw |
- |
r |
a = x && a (1 or 0) |
|
89 |
none |
rw |
pop |
- |
a = x && a (1 or 0) |
|
90 |
data |
rw |
- |
r |
a = x || a (1 or 0) |
|
91 |
none |
rw |
pop |
- |
a = x || a (1 or 0) |
Comparisons¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
92 |
data |
rw |
- |
r |
a = x < a (1 or 0) |
|
93 |
none |
rw |
pop |
- |
a = x < a (1 or 0) |
|
94 |
data |
rw |
- |
r |
a = x > a (1 or 0) |
|
95 |
none |
rw |
pop |
- |
a = x > a (1 or 0) |
|
96 |
data |
rw |
- |
r |
a = x == a (1 or 0) |
|
97 |
none |
rw |
pop |
- |
a = x == a (1 or 0) |
|
98 |
data |
rw |
- |
r |
a = x < a, floats (1 or 0) |
|
99 |
none |
rw |
pop |
- |
a = x < a, floats (1 or 0) |
|
100 |
data |
rw |
- |
r |
a = x > a, floats (1 or 0) |
|
101 |
none |
rw |
pop |
- |
a = x > a, floats (1 or 0) |
Shifts¶
mnemonic |
opcode |
operand |
a |
stack |
x |
computes |
|---|---|---|---|---|---|---|
|
102 |
data |
rw |
- |
r |
a = x << a |
|
103 |
none |
rw |
pop |
- |
a = x << a |
|
104 |
data |
rw |
- |
r |
a = x >> a (logical: zeros enter) |
|
105 |
none |
rw |
pop |
- |
a = x >> a (logical: zeros enter) |
|
106 |
data |
rw |
- |
r |
a = x >> a (arithmetic: the sign enters) |
|
107 |
none |
rw |
pop |
- |
a = x >> a (arithmetic: the sign enters) |