Friday, September 14, 2012

Dave Computes A Very Tiny Interpreter

Several years ago I designed a simple 4-bit computer processor that my students could build from TTL chips and wire. I called the processor the CHUMP (Cheap Homebrew Understandable Minimal Processor), and I called the corresponding programming language Chumpanese (which we imagine is not constrained by 4-bit addresses).  You can read an early draft of the paper I wrote on it here.

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

Yes, my students found Chumpanese awfully confusing, too, even though they knew exactly what voltages would change in response to each instruction.

No comments:

Post a Comment