Rules
General
All SAP programs fit in 16 addresses (0 to 15) because the program counter (
PC) is 4-bit.Initial values are
{PC: 0, Register A: 0, Register B: 0, FlagC: 0, FlagZ: 0, Executing: 1}.AandBregisters are unsigned and 8-bit by default. Number of bits is configurable.Instructions and data are all bytes. [1]
An instruction is a Mnemonic representing an Opcode (4-bit) and an Arg (4-bit).
For an instruction, the Arg can be represented in base-10 or base-16.
For example,
JC 15could also be written asJC F.
All data must fit in a byte. Specifically, the Mnemonic is a hexit, and the Arg is a hexit.
For example, 254 =
0xFEis MnemonicF, ArgE.10 =
0x0Ais Mnemonic0, ArgA. You may not omit the leading 0.
Programs run until they
HLTor until an Exception is raised. Infinite loops are possible, of course.
Note
These are the same rules a SAP computer implemented by hardware has to follow.
“This is a feature, not a bug”
Allowed syntax
In the Mnemonic column, these are allowed
two or three letter Mnemonic (for an instruction)
single-digit hexit
0toF(for data)
In the Arg column, these are allowed
single-digit hexit
0toF(for instruction or data)double-digit base-10 integer
10to15representing a hexite.g.
JC 15andJC Fare both legal and represent the same instruction
Technically, the Mnemonic column also accepts a hex or decimal number representing an opcode. SAPsim can’t distinguish between instructions and data. [1]
Disallowed syntax
If you follow the templates and example programs, you won’t run into any problems.
But if something goes wrong, an exception will occur, and there’ll be a descriptive error message.
The full list of Exceptions is in exceptions.py.
Footnotes