Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
P-code machine
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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}}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
P-code machine
(section)
Add topic