Example Machine
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, with one (opr) having multiple forms.
This is the code for the machine, written in Pascal:
const levmax=3; amax=2047; type fct=(lit,opr,lod,sto,cal,int,jmp,jpc); instruction=packed record f:fct; l:0..levmax; a:0..amax; end; procedure interpret; const stacksize = 500; var p, b, t: integer; {program-, base-, topstack-registers} i: instruction; {instruction register} s: array 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; l := l - 1 end; base := b1 end {base}; begin writeln(' start pl/0'); t := 0; b := 1; p := 0; s := 0; s := 0; s := 0; repeat i := code; p := p + 1; with i do case f of lit: begin t := t + 1; s := a end; opr: case a of {operator} 0: begin {return} t := b - 1; p := s; b := s; end; 1: s := -s; 2: begin t := t - 1; s := s + s end; 3: begin t := t - 1; s := s - s end; 4: begin t := t - 1; s := s * s end; 5: begin t := t - 1; s := s div s end; 6: s := ord(odd(s)); 8: begin t := t - 1; s := ord(s = s) end; 9: begin t := t - 1; s := ord(s <> s) end; 10: begin t := t - 1; s := ord(s < s) end; 11: begin t := t - 1; s := ord(s >= s) end; 12: begin t := t - 1; s := ord(s > s) end; 13: begin t := t - 1; s := ord(s <= s) end; end; lod: begin t := t + 1; s := s end; sto: begin s := s; writeln(s); t := t - 1 end; cal: begin {generate new block mark} s := base(l); s := b; s := p; b := t + 1; p := a end; int: t := t + a; jmp: p := a; jpc: begin if s = 0 then p := a; t := t - 1 end end {with, case} until p = 0; writeln(' end pl/0'); end {interpret};This machine was used to run Wirth's PL/0, which was a Pascal subset compiler used to teach compiler development.
Read more about this topic: P-code Machine
Famous quotes containing the word machine:
“The momentary charge at Balaklava, in obedience to a blundering command, proving what a perfect machine the soldier is, has, properly enough, been celebrated by a poet laureate; but the steady, and for the most part successful, charge of this man, for some years, against the legions of Slavery, in obedience to an infinitely higher command, is as much more memorable than that as an intelligent and conscientious man is superior to a machine. Do you think that that will go unsung?”
—Henry David Thoreau (18171862)
“But a man must keep an eye on his servants, if he would not have them rule him. Man is a shrewd inventor, and is ever taking the hint of a new machine from his own structure, adapting some secret of his own anatomy in iron, wood, and leather, to some required function in the work of the world. But it is found that the machine unmans the user. What he gains in making cloth, he loses in general power.”
—Ralph Waldo Emerson (18031882)