Web 51 P-Code Interpreter

P-Code, interpreter  software.html  P-Code, network

Part 2 - P-Instructions for arithmetics and program branches

Connection of P-Code Interpreter with individual P-Instructions can be demonstrated e.g. on the math library. Skeleton of a P-Instruction can be shown on the following example:
; *** PCMPBI *** compare STRING (cpu IDATA mem) with BYTE constant ***
; pcmpbi buffer_addr, byteconst
;       R7,  R5
pcmpbi: SETB zflag
        MOV  A,R7
        MOV  R0,A
        CLR  C
        MOV  A,@R0
        SUBB A,R5
        MOV  cflag,C
        JZ   pcmpbi0
        CLR  zflag

Input parameters are passed in R7 (parameter 1) and R5 (parameter 2) as detailed in the previous part. This procedure compares two numbers, one specified indirectly through R7, the other directly in R5. Comparison results are stored into global bit variables zflag and cflag. In the I51 mnemonic, the procedure does: zflag = (@R7 == R5), cflag = (@R7 < R5). Program can use these bit variables (defined in the main P-Code Interpreter module - Pcode.asm) to branch. Procedure is terminated with RET instruction.

Contents of LIBW - Library of math functions

instruction parameters description operation flags
pcmpbi (B)a, (B)imm Compare Byte to Immediate (BYTE)@a - (BYTE)imm zflag, cflag
pmovbi (B)a, (B)imm Mov Immediate to Byte (BYTE)@a = (BYTE)imm  
pmovb (B)a, (B)b Mov Byte to Byte (BYTE)@a = (BYTE)@b  
pcmpwi (B)a, (W)imm Compare Word to Immediate (WORD)@a - (WORD)imm zflag, cflag
pbitwi (B)a, (W)imm Test bit Immediate Word (WORD)@a AND (WORD)imm zflag
pmovwi (B)a, (W)imm Mov Immediate to Word (WORD)@a = (WORD)imm  
paddwi (B)a, (W)imm Add Immediate to Word (WORD)@a += (WORD)imm  
psubwi (B)a, (W)imm Subbtract Immediate Word (WORD)@a -= (WORD)imm  
pxorwi (B)a, (W)imm XOR Immediate Word (WORD)@a = (WORD)@a XOR (WORD)imm  
pandwi (B)a, (W)imm AND Immediate Word (WORD)@a = (WORD)@a AND (WORD)imm  
pshiftw (B)a, (B)n Shift Word n-bit Left/Right if(n>0){(WORD)@a << n}
else{(WORD)@a >>(-n)}
pcmpn (B)a, (B)b, (B)lng Compare two string for(i=0;i<lng;i++)(B)@a[i]-(B)@b[i] zflag, cflag
paddn (B)a, (B)b, (B)lng Add two string for(i=0;i<lng;i++)(B)@a[i]+=(B)@b[i]  

The table of basic arithmetic operations unveils limited addressing options of individual operands - mostly immediate values are used. That is not entirely true; remember the INDirect flags of P-Code itself. For example, command
   DPBB_ pmovwi, IND2, data_addr, wordtemp, blank
   performs (WORD)@a = (WORD)@b, that is, assignment (WORD)data_addr = (WORD)wordtemp.
When using INDirect flags on BYTE commands, remember that INDirect expands as WORD and a BYTE command uses LSB of this expansion. So, it is necessary to remember that the operand is actually taken from @(operand+1), or to use INDirect parameters with SWAP flag set. Considering the power of INDirect flag, a closer look at the instruction set reveals that for example MOVB instruction is unnecessary.

Contents of LIBW, module PCODE

instruction parameters description operation flags
pcall (W)addr Call to P-Code subroutine push current P-Code PC to stack, jump to addr
pret Return from P-Code subroutine pop P-Code PC from stack
pjump (W)addr Jump to P-Code addr jmp addr
pjumpeq (W)addr Jump if zflag to P-Code addr if(zflag) jmp addr
pjumpne (W)addr Jump if not zflag to P-Code addr if(zflag == 0) jmp addr
pjumpCarry (W)addr Jump if cflag to P-Code addr if(cflag) jmp addr
pjumpnCarry (W)addr Jump if not cflag to P-Code addr if(cflag == 0) jmp addr

The address specified as a parameter of a P-Code instruction must also point into a P-Code section. It is not possible to jump to a machine code instruction directly. Instead, P-Code section needs to be terminated with DB 0, and then an appropriate machine instruction such as jmp, jb, jnb, etc. is used. However, it is possible to call a machine code procedure from a P-Code section in a way similar to calling a P-Procedure - for example, with the macro DP machine_code, parameter1, parameter2, parameter3.

Part 2P-Code Interpreter, part 3

Sponzored by LPhard Ltd. Graphics by GIMP Created by EasyPad

(c)Copyright 2000 - 2002, HW server & Radek Benedikt
Web51@HW.cz, Web51.HW.cz
P-Code, interpreter  Obsah  P-Code, network