Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
P-code machine
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example machine== {{Textbook|section|date=January 2024}} [[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 [[stack frame|base register]] ''b'' and a [[stack (data structure)|top-of-stack register]] ''t''. There were 8 instructions: # <code>'''lit''' 0, ''a''</code> : load constant {{mono|{{var|a}}}} # <code>'''opr''' 0, ''a''</code> : execute operation {{mono|{{var|a}}}} (13 operations: RETURN, 5 mathematical functions, and 7 comparison functions) # <code>'''lod''' ''l'', ''a''</code> : load variable {{mono|{{var|l}}}}, {{mono|{{var|a}}}} # <code>'''sto''' ''l'', ''a''</code> : store variable {{mono|{{var|l}}}}, {{mono|{{var|a}}}} # <code>'''cal''' ''l'', ''a''</code> : call procedure {{mono|{{var|a}}}} at level {{mono|{{var|l}}}} # <code>'''int''' 0, ''a''</code> : increment t-register by {{mono|{{var|a}}}} # <code>'''jmp''' 0, ''a''</code> : jump to {{mono|{{var|a}}}} # <code>'''jpc''' 0, ''a''</code> : jump conditional to {{mono|{{var|a}}}}<ref name="Hansotten"/> This is the code for the machine, written in Pascal: <syntaxhighlight lang="pascal"> const amax=2047; {maximum address} levmax=3; {maximum depth of block nesting} cxmax=200; {size of code array} type fct=(lit,opr,lod,sto,cal,int,jmp,jpc); instruction=packed record f:fct; l:0..levmax; a:0..amax; end; var code: array [0..cxmax] of instruction; procedure interpret; const stacksize = 500; var p, b, t: integer; {program-, base-, topstack-registers} i: instruction; {instruction register} s: array [1..stacksize] of integer; {datastore} function base(l: integer): integer; var b1: integer; begin b1 := b; {find base l levels down} while l > 0 do begin b1 := s[b1]; l := l - 1 end; base := b1 end {base}; begin writeln(' start pl/0'); t := 0; b := 1; p := 0; s[1] := 0; s[2] := 0; s[3] := 0; repeat i := code[p]; p := p + 1; with i do case f of lit: begin t := t + 1; s[t] := a end; opr: case a of {operator} 0: begin {return} t := b - 1; p := s[t + 3]; b := s[t + 2]; end; 1: s[t] := -s[t]; 2: begin t := t - 1; s[t] := s[t] + s[t + 1] end; 3: begin t := t - 1; s[t] := s[t] - s[t + 1] end; 4: begin t := t - 1; s[t] := s[t] * s[t + 1] end; 5: begin t := t - 1; s[t] := s[t] div s[t + 1] end; 6: s[t] := ord(odd(s[t])); 8: begin t := t - 1; s[t] := ord(s[t] = s[t + 1]) end; 9: begin t := t - 1; s[t] := ord(s[t] <> s[t + 1]) end; 10: begin t := t - 1; s[t] := ord(s[t] < s[t + 1]) end; 11: begin t := t - 1; s[t] := ord(s[t] >= s[t + 1]) end; 12: begin t := t - 1; s[t] := ord(s[t] > s[t + 1]) end; 13: begin t := t - 1; s[t] := ord(s[t] <= s[t + 1]) end; end; lod: begin t := t + 1; s[t] := s[base(l) + a] end; sto: begin s[base(l)+a] := s[t]; writeln(s[t]); t := t - 1 end; cal: begin {generate new block mark} s[t + 1] := base(l); s[t + 2] := b; s[t + 3] := p; b := t + 1; p := a end; int: t := t + a; jmp: p := a; jpc: begin if s[t] = 0 then p := a; t := t - 1 end end {with, case} until p = 0; writeln(' end pl/0'); end {interpret}; </syntaxhighlight> This machine was used to run Wirth's [[PL/0]], a Pascal subset compiler used to teach compiler development.<ref>{{Cite report|last=Alpert|first=Donald|date=September 1979|title=A Pascal P-Code Interpreter for the Stanford Emmy|url=http://www.bitsavers.org/pdf/stanford/sel_techReports/TN164_A_Pascal_P-Code_Interpreter_for_the_Stanford_Emmy_Sep79.pdf|publisher=Computer Systems Laboratory, Departments of Eleotrioal Engineering and Computer Scienoes, Stanford University|id=Technioal Note No. 164}}</ref>{{Failed verification|date=April 2020}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)