Instruction reference

This document is a detailed reference manual for the instruction set of the Bedrock computer system.

If this document doesn’t answer your question, the definitive guide is the Architecture specification.

Summary

Oper. Name Description Stack signature
0x00 HLT Halt the processor. ( -- )
0x01 PSH Move a value between stacks. ( -- x ) ( x -- )
0x02 POP Remove a value from a stack. ( x -- )
0x03 CPY Copy a value between stacks. ( -- x ) ( x -- x )
0x04 DUP Duplicate a value. ( x -- x x )
0x05 OVR Duplicate the next value down. ( x y -- x y x )
0x06 SWP Swap the top two values. ( x y -- y x )
0x07 ROT Rotate the top three values. ( x y z -- y z x )
0x08 JMP Jump to an address. ( a* -- )
0x09 JMS Jump to an address, keeping the current address. ( a* -- ) ( -- b* )
0x0A JCN Conditionally jump. ( t a* -- )
0x0B JCS Conditionally jump, keeping the current address. ( t a* -- ) ( -- [b*] )
0x0C LDA Load a value from a memory address. ( a* -- v )
0x0D STA Store a value to a memory address. ( v a* -- )
0x0E LDD Load a value from a device port. ( p. -- v )
0x0F STD Store a value to a device port. ( v p. -- )
0x10 ADD Add two values. ( x y -- r )
0x11 SUB Subtract one value from another. ( x y -- r )
0x12 INC Increment a value. ( x -- r )
0x13 DEC Decrement a value. ( x -- r )
0x14 LTH Test if a value is less than another. ( x y -- t. )
0x15 GTH Test if a value is greater than another. ( x y -- t. )
0x16 EQU Test if two values are equal. ( x y -- t. )
0x17 NQK Test if two values are inequal, keeping both. ( x y -- x y t. )
0x18 SHL Shift the bits of a value to the left. ( x y. -- r )
0x19 SHR Shift the bits of a value to the right. ( x y. -- r )
0x1A ROL Rotate the bits of a value to the left. ( x y. -- r )
0x1B ROR Rotate the bits of a value to the right. ( x y. -- r )
0x1C IOR Find the bits that are set in either value. ( x y -- r )
0x1D XOR Find the bits that are different in both values. ( x y -- r )
0x1E AND Find the bits that are set in both values. ( x y -- r )
0x1F NOT Invert the bits in a value. ( x -- r )

How instructions work

An operation and three mode flags.

Eight variants of each operation.

Operation groups

There are 32 operations, three modes. 8 variants of each operation, 256 possible instructions.

Instructions are grouped into four categories

Stack HLT PSH POP CPY DUP OVR SWP ROT
Control JMP JMS JCN JCS LDA STA LDD STD
Numeric ADD SUB INC DEC LTH GTH EQU NQK
Binary SHL SHR ROL ROR IOR XOR AND NOT

Stack operations

HLT

The HLT operation halts the Bedrock processor, terminating the current program.

Variants: NOP, and DB1-6

If any of the mode flags are set, this instruction will do nothing.

All bytes of program memory are zero by default, and so a program will halt if it jumps

Stops the program.

All uninitialised memory is zero, HTL instructions.

Also NOP, for doing nothing.

Also DB[1-6], for debug stuff, implementation defined. Look at the manual for your implementation.

PSH

The PSH operation pushes a value onto a stack, or moves a value between stacks.

Pushing a value

When using PSH in immediate mode, the next value in the program will be pushed to a stack. This can be used to push a byte or a double. The value will normally be pushed to the working stack, but can be pushed to the return stack with return mode.

This is such a common operation that there are aliases for each of the four immediate-mode PSH instructions: PSH:, PSH*:, PSHr:, and PSHr*: can be shortened to :, *:, r:, and r*:.

PSH:01 PSHr:02  ( 01    | 02    )
:03    r:04     ( 01 03 | 02 04 )

Moving between stacks

When using PSH without immediate mode, a value will be moved from one stack to the other. The value will normally move from the return stack to the working stack, but can be moved the other way with return mode.

:01 :02  ( 01 02 |    )
PSHr     ( 01    | 02 )
PSH      ( 01 02 |    )

POP

The POP operation removes a value from the top of a stack.

Remove a value from a stack.

Cleaning up before returning from a function.

CPY

The CPY operation copies a value from one stack to another.

Duplicate a value across stacks

For keeping a copy around.

Good for loop counters.

DUP

The DUP operation duplicates the value at the top of a stack.

duplicate to keep a copy around

good for before a function that doesn’t preserve its arguments.

OVR

The OVR operation copies the value second-from-top of a stack to the top.

SWP

The SWP operation exchanges the positions of the top two values on a stack.

insert a value below the current one

turn a byte into a double.

ROT

The ROT operation rotates the positions of the top three values on a stack.

extract values

good for byte below double

Control operations

Decisions in the program

Interacting with the world

JMP

The JMP operation jumps to an address in the program.

Return from a function

JMS

The JMS operation jumps to an address, storing the original address.

Call a function

JCN

The JCN operation jumps to an address if a condition is true.

Decisions, conditional logic

JCS

The JCS operation jumps if a condition is true, storing the original address.

Conditional call, good for no-argument functions.

LDA

The LDA operation loads a value from a memory address onto a stack.

Access an array

Load variables

STA

The STA operation stores a value from a stack to a memory address.

Store variables

Edit code

LDD

The LDD operation loads a value from a device port onto a stack.

Read from device

STD

The STD operation stores a value from a stack to a device port.

Write to device

Numeric operations

ADD

The ADD operation adds two values together.

Adding offsets to addresses, for indexing into an array.

SUB

The SUB operation subtracts one value from another.

INC

The INC operation increments a value by one.

Loops

DEC

The DEC operation decrements a value by one.

Loops

LTH

The LTH operation compares two values, returning true if the first is less than the second.

GTH

The GTH operation compares two values, returning true if the first is greater than the second.

Test if something isn’t zero, since there isn’t a dedicated NEQ instruction

EQU

The EQU operation compares two values, returning true if they are equal.

NQK

The NQK operation compares two values, returning true if they are not equal. The values are also both kept on the stack.

Used for loops

Binary operations

Binary operations treat values as sequences of bits that can be turned on, turned off, and moved left and right.

First half is shifting. Second half is logic.

SHL

SHR

ROL

ROR

IOR

The IOR operation merges two values, keeping the bits that are set in either value.

Merges two values together.

Also logic

Setting bits

XOR

The XOR operation merges two values, keeping the bits that are different in both values.

Finds the bits that are different between two values.

Can be used to invert specific bits in a value. Given a mask.

AND

The AND operation merges two values, keeping the bits that are set in both values.

The AND operation i

pops two values and pushes a value that Keeps the bits that are set in both values.

Can be used to turn off specific bits in a value. Given a mask,

order isn’t important

:55  (  input value - bits are 0101_0101 )
:0F  (   mask value - bits are 0000_1111 )
AND  ( result value - bits are 0000_0101 )

Can also be used to check two boolean values, returning true if both values are true. The values must be either ff or 00.

:00 :00 AND ( returns 00 )
:ff :00 AND ( returns 00 )
:ff :ff AND ( returns ff )

NOT

The NOT operation inverts every bit in a value.

Turn ff into 00

This reference is intended to be used by people working with Bedrock programs. Every instruction is expanded to show the effect of each mode flag combination.

For people implementing a Bedrock system, the exact semantics of each instruction can be found in the architecture specification.

In the instruction table provided for each operation, the * column contains a character when the wide-mode flag is set, the : column contains a character when the immediate-mode flag is set, and the r column contains a character when the return-mode flag is set.

Appendix A: Stack reference

Omits return-mode instructions for brevity. Swap the working stack and return stack columns, and add 0x80 to the instruction value.

i is an immediate value.

Instr. Mnm. * : Working stack Return stack
0x00 HLT ( -- ) ( -- )
0x20 NOP * ( -- ) ( -- )
0x40 DB1 : ( -- ) ( -- )
0x60 DB2 * : ( -- ) ( -- )
0x01 PSH ( -- x ) ( x -- )
0x21 PSH* * ( -- x* ) ( x* -- )
0x41 PSH: : ( -- i ) ( -- )
0x61 PSH*: * : ( -- i* ) ( -- )
0x02 POP ( x -- ) ( -- )
0x22 POP* * ( x* -- ) ( -- )
0x42 POP: : ( -- ) ( -- )
0x62 POP*: * : ( -- ) ( -- )
0x03 CPY ( -- x ) ( x -- x )
0x23 CPY* * ( -- x* ) ( x* -- x* )
0x43 CPY: : ( -- i ) ( -- i )
0x63 CPY*: * : ( -- i* ) ( -- i* )
0x04 DUP ( x -- x x ) ( -- )
0x24 DUP* * ( x* -- x* x* ) ( -- )
0x44 DUP: : ( -- i i ) ( -- )
0x64 DUP*: * : ( -- i* i* ) ( -- )
0x05 OVR ( x y -- x y x ) ( -- )
0x25 OVR* * ( x* y* -- x* y* x* ) ( -- )
0x45 OVR: : ( x -- x i x ) ( -- )
0x65 OVR*: * : ( x* -- x* i* x* ) ( -- )
0x06 SWP ( x y -- y x ) ( -- )
0x26 SWP* * ( x* y* -- y* x* ) ( -- )
0x46 SWP: : ( x -- i x ) ( -- )
0x66 SWP*: * : ( x* -- i* x* ) ( -- )
0x07 ROT ( x y z -- y z x ) ( -- )
0x27 ROT* * ( x* y* z* -- y* z* x* ) ( -- )
0x47 ROT: : ( x y -- y i x ) ( -- )
0x67 ROT*: * : ( x* y* -- y* i* x* ) ( -- )
0x08 JMP ( a* -- ) ( -- )
0x28 JMP* * ( a* -- ) ( -- )
0x48 JMP: : ( -- ) ( -- )
0x68 JMP*: * : ( -- ) ( -- )
0x09 JMS ( a* -- ) ( -- b* )
0x29 JMS* * ( a* -- ) ( -- b* )
0x49 JMS: : ( -- ) ( -- b* )
0x69 JMS*: * : ( -- ) ( -- b* )
0x0A JCN ( t a* -- ) ( -- )
0x2A JCN* * ( t* a* -- ) ( -- )
0x4A JCN: : ( t -- ) ( -- )
0x6A JCN*: * : ( t* -- ) ( -- )
0x0B JCS ( t a* -- ) ( -- [b*] )
0x2B JCS* * ( t* a* -- ) ( -- [b*] )
0x4B JCS: : ( t -- ) ( -- [b*] )
0x6B JCS*: * : ( t* -- ) ( -- [b*] )
0x0C LDA ( a* -- v ) ( -- )
0x2C LDA* * ( a* -- v* ) ( -- )
0x4C LDA: : ( -- v ) ( -- )
0x6C LDA*: * : ( -- v* ) ( -- )
0x0D STA ( v a* -- ) ( -- )
0x2D STA* * ( v* a* -- ) ( -- )
0x4D STA: : ( v -- ) ( -- )
0x6D STA*: * : ( v* -- ) ( -- )
0x0E LDD ( p -- v ) ( -- )
0x2E LDD* * ( p -- v* ) ( -- )
0x4E LDD: : ( -- v ) ( -- )
0x6E LDD*: * : ( -- v* ) ( -- )
0x0F STD ( v p -- ) ( -- )
0x2F STD* * ( v* p -- ) ( -- )
0x4F STD: : ( v -- ) ( -- )
0x6F STD*: * : ( v* -- ) ( -- )
0x10 ADD ( x y -- r ) ( -- )
0x30 ADD* * ( x* y* -- r* ) ( -- )
0x50 ADD: : ( x -- r ) ( -- )
0x70 ADD*: * : ( x* -- r* ) ( -- )
0x11 SUB ( x y -- r ) ( -- )
0x31 SUB* * ( x* y* -- r* ) ( -- )
0x51 SUB: : ( x -- r ) ( -- )
0x71 SUB*: * : ( x* -- r* ) ( -- )
0x12 INC ( x -- r ) ( -- )
0x32 INC* * ( x* -- r* ) ( -- )
0x52 INC: : ( -- r ) ( -- )
0x72 INC*: * : ( -- r* ) ( -- )
0x13 DEC ( x -- r ) ( -- )
0x33 DEC* * ( x* -- r* ) ( -- )
0x53 DEC: : ( -- r ) ( -- )
0x73 DEC*: * : ( -- r* ) ( -- )
0x14 LTH ( x y -- t ) ( -- )
0x34 LTH* * ( x* y* -- t ) ( -- )
0x54 LTH: : ( x -- t ) ( -- )
0x74 LTH*: * : ( x* -- t ) ( -- )
0x15 GTH ( x y -- t ) ( -- )
0x35 GTH* * ( x* y* -- t ) ( -- )
0x55 GTH: : ( x -- t ) ( -- )
0x75 GTH*: * : ( x* -- t ) ( -- )
0x16 EQU ( x y -- t ) ( -- )
0x36 EQU* * ( x* y* -- t ) ( -- )
0x56 EQU: : ( x -- t ) ( -- )
0x76 EQU*: * : ( x* -- t ) ( -- )
0x17 NQK ( x y -- x y t ) ( -- )
0x37 NQK* * ( x* y* -- x* y* t ) ( -- )
0x57 NQK: : ( x -- x i t ) ( -- )
0x77 NQK*: * : ( x* -- x* i* t ) ( -- )
0x18 SHL ( x y -- r ) ( -- )
0x38 SHL* * ( x* y -- r ) ( -- )
0x58 SHL: : ( x -- r ) ( -- )
0x78 SHL*: * : ( x* -- r ) ( -- )
0x19 SHR ( x y -- r ) ( -- )
0x39 SHR* * ( x* y -- r ) ( -- )
0x59 SHR: : ( x -- r ) ( -- )
0x79 SHR*: * : ( x* -- r ) ( -- )
0x1A ROL ( x y -- r ) ( -- )
0x3A ROL* * ( x* y -- r ) ( -- )
0x5A ROL: : ( x -- r ) ( -- )
0x7A ROL*: * : ( x* -- r ) ( -- )
0x1B ROR ( x y -- r ) ( -- )
0x3B ROR* * ( x* y -- r ) ( -- )
0x5B ROR: : ( x -- r ) ( -- )
0x7B ROR*: * : ( x* -- r ) ( -- )
0x1C IOR ( x y -- r ) ( -- )
0x3C IOR* * ( x* y* -- r* ) ( -- )
0x5C IOR: : ( x -- r ) ( -- )
0x7C IOR*: * : ( x* -- r* ) ( -- )
0x1D XOR ( x y -- r ) ( -- )
0x3D XOR* * ( x* y* -- r* ) ( -- )
0x5D XOR: : ( x -- r ) ( -- )
0x7D XOR*: * : ( x* -- r* ) ( -- )
0x1E AND ( x y -- r ) ( -- )
0x3E AND* * ( x* y* -- r* ) ( -- )
0x5E AND: : ( x -- r ) ( -- )
0x7E AND*: * : ( x* -- r* ) ( -- )
0x1F NOT ( x -- r ) ( -- )
0x3F NOT* * ( x* -- r* ) ( -- )
0x5F NOT: : ( -- r ) ( -- )
0x7F NOT*: * : ( -- r* ) ( -- )