/ Resources / References / P-Code / P-Codes Defined

RCOS.java Home Page
P-codes occupy four bytes each. The first byte is the operation code (op-code). There are thirteen basic P-code instructions, each with a different op-code.

The second byte of the P-code instruction contains either 0 or a lexical level offset, or a condition code for the conditional jump instruction.

The last two bytes taken as a 16-bit integer form an operand which is a literal value, or a variable offset from a base in the stack, or a P-code instruction location, or an operation number, or a special routine number, depending on the op-code.

Basic P-Codes

P-Code Hex OP-Code Description
LIT 0,N 0x00 load literal value onto stack by incrementing stack pointer, writing N to data stack.
OPR 0,N  0x01  arithmetic or logical operation on top of stack 
LOD L,N  0x02  load value of variable at level offset L, base offset N in stack onto top of stack 
STO L,N  0x03  store value on top of stack into variable location at level offset L, base offset N in stack 
CRL L,N  0x04  call PROC or FUNC at P-code location N declared at level offset L 
INT 0,N  0x05  increment stackpointer (T) by N (may be negative) 
JMP 0,N  0x06  jump to P-code location N 
JPC C,N  0x07  jump if C=value on top of stack to P-code location N (C can = 0 or 1) 
CSP 0,N  0x08  call standard procedure number N 
LODX L,N  0x12  load indexed (array) variables as above 
STOX L,N  0x13  store indexed variable as above 

P-Machine Instruction Set

POP X means remove the top element of the stack and load it into X (the stack is now one smaller). PUSH X means place the value of X onto the top of the stack (the stack is now one bigger).
 
LIT 0,NN  literal: push NN 
OPR 0,0  process and function, return operation 
OPR 0,1  negate: POP A, PUSH (-A)
OPR 0,2  add: POP A, POP B, PUSH (B+A)
OPR 0,3  subtract: POP A, POP B, PUSH (B-A)
OPR 0,4  multiply: POP A, POP B, PUSH (B*A)
OPR 0,5  divide: POP A, POP B, PUSH (B/A)
OPR 0,6  low bit: POP A, PUSH (A and 1) 
OPR 0,7  mod: POP A, POP B, PUSH (B mod A) 
OPR 0,8  test equal: POP A, POP B, PUSH (B=A) 
OPR 0,9  test not equal: POP A, POP B, PUSH (B<>A) 
OPR 0,10  test less than: POP A, POP B, PUSH (B<A) 
OPR 0,11  test greater than or equal to: POP A, POP B, PUSH (B>=A) 
OPR 0,12  test greater than: POP A, POP B, PUSH (B>A) 
OPR 0,13  test less than or equal to: POP A, POP B, PUSH (B<=A) 
OPR 0,14  logical OR: POP A, POP B, PUSH (B or A) 
OPR 0,15  logical AND: POP A, POP B, PUSH (B and A) 
OPR 0,16  logical XOR: POP A, PUSH (xor A) 
OPR 0,17  logical NOT: POP A, PUSH (not A) 
OPR 0,18  shift left: POP A, POP B, PUSH (B left shifted A bits) 
OPR 0,19  shift right: POP A, POP B, PUSH (B right shifted A bits) 
OPR 0,20  increment: POP A, PUSH A+1 
OPR 0,21  decrement: POP A, PUSH A-1 
OPR 0,22  copy: POP A, PUSH A, PUSH A 
OPR 0,23  random number: PUSH generated number 
LOD L,D  load a word: LOAD A from (base of level offset L)+D, PUSH A 
LOD 255,0  load byte from memory address which is on top of stack onto top of stack: POP address, load A with byte from address, PUSH A 
LODX L,D  load a word by index: POP index, load A from (base of level offset)+D+index, PUSH A 
STO L,D  store a word: POP A, store A at (base of level offset L)+D 
**STO 255,0  store a byte: POP A, POP address, store low byte at address 
STOX L,D  store a word by index: POP index, POP A, store A at (base of level offset L)+D+index 
CAL L,A  call procedure or function at p-code location A, with base at level offset L 
**CAL 255,0  call procedure address in memory: POP address, PUSH return address, JUMP to address 
INT 0,NN  add NN to stack pointer 
JMP 0,A  jump to P-code location A 
JPC 0,A  jump if true : POP A, IF (A AND 1) = 0 then jump to location A 
**JPC 1,A jump if true : POP A, IF (A AND 1) = 1 then jump to location A?
CSP 0,0  input 1 character: INPUT A, PUSH A 
CSP 0,1  output 1 character: POP A, OUTPUT A 
CSP 0,2  input an integer: INPUT A#, PUSH A 
CSP 0,3  output an integer: POP A, OUTPUT A# 
**CSP 0,4 input a hexadecimal number
**CSP 0,5 output a hexadecimal number
CSP 0,8  output a character string: POP A, FOR I := 1 to A DO BEGIN POP B, OUTPUT B, END
*CSP 0,9 semaphore close: details to come
*CSP 0,10 semaphore create: details to come
*CSP 0,11 semaphore open: details to come
*CSP 0,12 semaphore signal: details to come
*CSP 0,13 semaphore wait: details to come
*CSP 0,14 shared memory close: details to come
*CSP 0,15 shared memory create: details to come
*CSP 0,16 shared memory open: details to come
*CSP 0,17 shared memory read: details to come
*CSP 0,18 shared memory write: details to come
*CSP 0,19 shared memory size: details to come
*CSP 0,20 file allocate: details to come
*CSP 0,21 file open: details to come
*CSP 0,22 file create: details to come
*CSP 0,23 file close: details to come
*CSP 0,24 file end of file (EOF): details to come
*CSP 0,25 file delete: details to come
*CSP 0,26 file read: details to come
*CSP 0,27 file write: details to come
* - Addition to the standard P-Code specification (as specified from Byte).
** - Not implemented in RCOS.java.

NOTE: When a logical operation is performed a TRUE result is indicated by a 1 and a 0 inidicates FALSE.