Anyway, I was playing around with a well-known OISC (one instruction set computer) language tonight, in which the only instruction is:
subeq a, b, c
This instruction behaves as follows:
mem[a] = mem[a] - mem[b];
if (mem[a] == 0)
pc = mem[c];
else
pc++;
(subleq is the more popular version, which branches when mem[a]<=0.)
It occurred to me tonight that I could easily write an interpreter for this OISC in Chumpanese. I used one address for the program counter. pc would contain a, pc+1 would contain b, pc+2 would contain c, and pc+3 would be the beginning of the next instruction. Here is the very tiny OISC interpreter I wrote in Chumpanese:
fetch: read pc load it add 1 storeto b // b = mem[pc + 1]
read pc read it load it // mem[a] (a=mem[pc])
read b read it sub it // mem[a] - mem[b]
read pc read it storeto it // store in mem[a]
ifzero jump // if (mem[a] == 0)
read pc load it add 3 goto end // pc + 3 (next inst)
jump: read pc load it add 2 storeto c // c = pc + 2
read c read it load it // mem[c]
end: storeto pc goto fetch // set pc and repeat
No comments:
Post a Comment