p-code.mdwn (1516B)
1 A p-code machine is a [[virtual machine]] designed to execute the [[assembly language]] of a hypothetical CPU. 2 3 [[Niklaus Wirth]] specified a simple p-code machine in the 1976 book Algorithms + Data Structures = Programs. The machine had 3 registers - a program counter p, a base register b, and a top-of-stack register t. There were 8 instructions: 4 5 lit 0, a : load constant a 6 opr 0, a : execute operation a (13 operations: RETURN, 5 math functions, and 7 comparison functions) 7 lod l, a : load variable l,a 8 sto l, a : store variable l,a 9 cal l, a : call procedure a at level l 10 int 0, a : increment t-register by a 11 jmp 0, a : jump to a 12 jpc 0, a : jump conditional to a[6] 13 14 ## Operations 15 16 0: begin {return} 17 t := b - 1; p := s[t + 3]; b := s[t + 2]; 18 end; 19 1: s[t] := -s[t]; 20 2: begin t := t - 1; s[t] := s[t] + s[t + 1] end; 21 3: begin t := t - 1; s[t] := s[t] - s[t + 1] end; 22 4: begin t := t - 1; s[t] := s[t] * s[t + 1] end; 23 5: begin t := t - 1; s[t] := s[t] div s[t + 1] end; 24 6: s[t] := ord(odd(s[t])); 25 8: begin t := t - 1; s[t] := ord(s[t] = s[t + 1]) end; 26 9: begin t := t - 1; s[t] := ord(s[t] <> s[t + 1]) end; 27 10: begin t := t - 1; s[t] := ord(s[t] < s[t + 1]) end; 28 11: begin t := t - 1; s[t] := ord(s[t] >= s[t + 1]) end; 29 12: begin t := t - 1; s[t] := ord(s[t] > s[t + 1]) end; 30 13: begin t := t - 1; s[t] := ord(s[t] <= s[t + 1]) end; 31 32 33 See more: 34 35 * [On Wikipedia](https://en.wikipedia.org/wiki/P-code_machine)