# Web 51 P-Code Interpreter   ## 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 ***
;       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
pcmpbi0:RET
```

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
• (B) or (BYTE) - 8-bit argument or operation
• (W) or (WORD) - 16-bit argument or operation
• a, b - address of data in processor memory (DATA/IDATA)
• @a - data in processor memory at address a
• @a[i] - data in processor memory at address a + i
• imm - immediate constant
• n - number of bits
• lng - string length

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 P-Code Interpreter, part 3    (c)Copyright 2000 - 2002, HW server & Radek Benedikt Web51@HW.cz, Web51.HW.cz   