A P-code definition

Jon Loeliger <loeliger@bach.convex.com>
Wed, 16 Oct 91 22:53:24 CDT

          From comp.compilers

Related articles
Pcode definition wanted mcj@zycor.lgc.com (1991-10-15)
A P-code definition loeliger@bach.convex.com (Jon Loeliger) (1991-10-16)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Jon Loeliger <loeliger@bach.convex.com>
Keywords: pascal, question
Organization: Compilers Central
References: 91-10-053
Date: Wed, 16 Oct 91 22:53:24 CDT

Ok sports fans! Michael C. Jones (mjc@zycor.lgc.com) asks about P-code.
We reel and spin, do the twilight-zone theme and comply...


I dug deep into the bowels of my closet and found the oft desired P-Code
definition! With it, I betray my background; a dedicated Z-80 hacker, I
confess.


So here it is, the P-Code as read from page 7 of The People's Pascal
System Documentation, as published in a TRS-80 Computing rag of 1979.
(Say that with dignity! :-)


All typos are mine, naturally. Let's start with section 5, then 4.




5. The P-codes:
    P-codes are the machine language of the imaginary P-machine.
    P-codes occupy four bytes each. The first byte is the operation
code (op-code). There are nine 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.


5.1 P-code details:
p-code hex description
op-code
-----------------------------------
LIT 0,N 00 load literal value onto stack
OPR 0,N 01 arithmetic or logical operation on top of stack
LOD L,N 02 load value of variable at level offset L, base
offset N in stack onto to of stack
LODX L,N 12 load indexed (array) variables as above
STO L,N 03 store value on top of stack into variable location
at level offset L, base offset N in stack
STOX L,N 13 store indexed variable as above
CRL L,N 04 call PROC or FUNC at P-code location N declared
at level offset L
INT 0,N 05 increment stackpointer (T) by N (may be negative)
JMP 0,N 06 jump to P-code location N
JPC C,N 07 jump if C=value on top of stack to P-code location
N (C can = 0 or 1)
CSP 0,N 08 call standard procedure number N




4. P-code instructions:
    (Note: POP X menads 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: 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: POP A, POP B, PUSH (B<=A)
OPR 0,14 OR: POP A, POP B, PUSH (B or A)
OPR 0,15 AND: POP A, POP B, PUSH (B and A)
OPR 0,16 NOT: POP A, PUSH (not A)
OPR 0,17 SHIFT LEFT: POP A, POP B, PUSH (B left shifted A bits)
OPR 0,18 SHIFT RIGHT: POP A, POP B, PUSH (B right shifted A bits)
OPR 0,19 INCREMENT: POP A, PUSH A+1
OPR 0,20 DECREMENT: POP A, PUSH A-1
OPR 0,21 COPY: POP A, PUSH A, PUSH A
LOD L,D LOAD: 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 INDEXED LOAD: POP index, load A from (base of level
offset)+D+index, PUSH A
STO L,D STORE: POP A, store A at (base of level offset L)+D
STO 255,0 STORE IN MEMORY: POP A, POP address, store low byte
at address
STOX L,D INDEXED STORE: 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
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,8 OUTPUT A CHARACTER STRING: POP A, FOR I := 1 to A DO BEGIN
POP B, OUTPUT B, END


NOTE: The result of a logical operation such as (A=B) is defined as 1 if
the condition was met and 0 otherwise.








Ok, now. I can, like, add the Z-80 translations of these p-codes
if someone *really* wants them. And, in the "I am sicker than you"
category... I am willing to provide (on an as-is basis) a Z-80
interpreter I wrote in C that handles almost all Z-80 instructions.




jdl
--
Jon Loeliger | loeliger@convex.com
Convex Computer Corporation
3000 Waterview, Richardson TX 75080
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.