It's like ASM, but it's JavaScript underneath.
I wanted to learn about how a CPU works. I figured the best way to do that was to ignore all existing literature and just try to emulate one, based on the tiny amount of assembly knowledge I have. This is the result.
Usage:
$ jsm-assemble [source.jsm] [program.bin]
Description:
Assembles a .jsm source file into a binary program image, ready for execution.
Usage:
$ jsm-execute [program.bin]
Description:
Executes a program image and dumps out the state of the CPU at the end.
The CPU is a really simple thing. It has a configurable number of registers (by default 8,) one region of memory, an instruction pointer, a "running" flag, and a counter of the number of ticks it has processed.
I stole inspiration from a bunch of difference places to put it together - most of the terminology is gleaned from Wikipedia and early Intel specifications.
These will probably change a lot as I mess with things. Deal with it.
Long name:
No operation
Arguments:
none
Effects:
none
Long name:
Set register value
Arguments:
- register (number)
- value (number)
Effects:
Sets register register to value value.
Long name:
Copy register value
Arguments:
- from (number)
- to (number)
Effects:
Sets register to to the value of register from.
Long name:
Dynamic read byte
Arguments:
- target (number)
- reference (number)
Effects:
Sets register target to the value of the byte-sized region of memory pointed to by register reference.
Long name:
Dynamic write byte
Arguments:
- source (number)
- reference (number)
Effects:
Sets the byte-sized region of memory pointed to by register reference to the value of register source.
Long name:
Static read byte
Arguments:
- target (number)
- address (number)
Effects:
Sets register target to the value of the byte-sized region of memory at address.
Long name:
Static write byte
Arguments:
- source (number)
- address (number)
Effects:
Sets the byte-sized region of memory address to the value of register source.
Long name:
Add registers
Arguments:
- target (number)
- register a (number)
- register b (number)
Effects:
Sets register target to the sum of registers register a and register b.
Long name:
Multiply registers
Arguments:
- target (number)
- register a (number)
- register b (number)
Effects:
Sets register target to the product of registers register a and register b.
Long name:
AND registers
Arguments:
- target (number)
- register a (number)
- register b (number)
Effects:
Sets register target to the AND of registers register a and register b.
Long name:
OR registers
Arguments:
- target (number)
- register a (number)
- register b (number)
Effects:
Sets register target to the OR of registers register a and register b.
Long name:
XOR registers
Arguments:
- target (number)
- register a (number)
- register b (number)
Effects:
Sets register target to the XOR of registers register a and register b.
Long name:
Jump
Arguments:
- location (number)
Effects:
Sets the instruction pointer to location.
Long name:
Jump if not equal
Arguments:
- register a (number)
- register b (number)
- location (number)
Effects:
Sets the instruction pointer to location if register register a is not equal to register register b.
Long name:
End execution
Arguments:
none
Effects:
Tells the CPU to stop executing by setting the running flag to false.
The language I've cooked up is basically just the result of me stealing little bits of syntax from Wikipedia and whatever popped up when I searched for "assembly language" on the Google. It'll likely be in a quickly-changing state until I'm happy with it.
The basic concept is that you have your operation on the left, and arguments following it, separated by commas. Arguments can be either literal numbers (in decimal or hex) or expressions, where expressions are composed of very basic arithmetic. Inside expressions, labels will be expanded to refer to their position in the program's image. Labels are specified by prefixing their name with a colon. Comments are designated by a semicolon, and will eat up anything right to the end of the line.
If you've read any assembly before, it should be familiar enough. This is how it looks (you can find this code in examples/loop.jsm):
; this is the entry point of our program. everything is just executed from start
; to finish. this means that if you go past the end of your code you start
; executing data. lol.
;
; this program is basically:
;
; for (i=0;i<10;++i) {
; // do nothing
; }
;
:start
SET 0, 0x00 ; initial value
SET 1, 0x01 ; increment amount
SET 2, 0x0a ; target value (10)
:before_loop ; do {
ADD 0, 0, 1 ; r0 = r0 + r1;
JNE 0, 2, [:before_loop] ; } while (r0 != r2);
:after_loop
; at this point, registers 0 and 2 should be equal.
; this is the end of our code! when the CPU gets here, it'll stop. hopefully.
END
3-clause BSD. A copy is included with the source.
- GitHub (deoxxa)
- Twitter (@deoxxa)
- Email (deoxxa@fknsrs.biz)