a simple register machine, written in ruby (code is not optimized or even clean ;), but it works)
Instruction Description CLOAD a Load value a to register 1 LOAD r Load value from register r to register 1 STORE r Save value from register 1 to register r CADD a Add value a to value in register 1 ADD r Add value in register r to value in register 1 CSUB a Substract value a from value in register 1 SUB r Substract value in register r from value in register 1 CMULT a multiply value in register 1 with a MULT r multiply value in register 1 with value in register r CDIV a divide value in register 1 by a DIV r divide value in register 1 by value in register r GOTO a jump to line a IF r1=0 GOTO a jump to line a, if value in register 1 is equal to 0 END exit the machine
the registers can only store natural integers and 0 (CSUB & SUB will at least result in 0, CDIV & DIV will result in integers, means 5/3=1)
unknown instructions will raise Exceptions
lines beginning with # are comments
the interpreter will always quit (on success) with a line similar to the following
[0, 1] in line 5 after 20 steps
this shows the register ([0, 1]), the last executed line, and the total number of steps
regmachine.rb [-t] [-c COUNT] [-a LIST] <file> -t, --trace trace commands -c, --count COUNT register count -a, --assignment ASSIGNMENT register assignment, format is: 1,2,3
For example:
regmachine -c 2 -a 5,1 samples/test.mch
this will start the interpreter with 2 registers, the two are filled with the values 5 and 1 (the sample will then count down to 0)
Creative Commons by-nc-sa 3.0
- creativecommons.org/licenses/by-nc-sa/3.0/
-
You are free:
-
to Share — to copy, distribute and transmit the work
-
to Remix — to adapt the work
Under the following conditions:
-
Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
-
Noncommercial. You may not use this work for commercial purposes.
-
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.
-