6 ms·
JSVM
Javascript virtual machine is one instruction set machine to parse and execute an assembly like code.
- jared314 13y agoI love low-level learning projects like this, but I wish more of them would centralize around something like Nand2Tetris[1] to create an expanded, and interconnected, learning ecosystem. [1] http://www.nand2tetris.org/ http://www.nand2tetris.org/
- chengsun 13y agoShameless plug: a while back I wrote an assembler-and-VM for a similarly hypothetical assembly language that I was studying in class. Just for kicks, I wondered if I could fit the entire thing into 1KB of JavaScript, whilst including features as step by step execution, a "debugging GUI" (as much as you could expect from 1KB!) featuring the stack, register contents and program counter, plus the assembler on top which performs full syntax/opcode argument checking. The result (which I'm pretty pleased with) is here: http://chengsun.github.io/ocremutiny.html http://chengsun.github.io/ocremutiny.html
- angersock 13y agoIf I make make a few suggestions (mostly from a desire to see a more familiar sort of spec): > Add arithmetic shift left/right > Add logical shift left/right > Add exclusive OR (XOR) > Pick a word size so your integer and bit ops have meaning Questions: > How do I set the program counter from my assembly? > Do I have registers (seems like a stack-based VM, so no)? If not, can I have more stacks? > Can you setup a dedicated block of memory to use as a framebuffer and render it to a <canvas> tag? ~ Good work so far!
- jawb 13y ago> Add arithmetic shift left/right .. I'll add > Add logical shift left/right .. I'll add > Add exclusive OR (XOR) .. I'll add > Pick a word size so your integer and bit ops have meaning .. I think I'll just rely on js types to keep things simple, If you see any reason not to please explain further > How do I set the program counter from my assembly? The only way to set PC is through branching > Do I have registers (seems like a stack-based VM, so no)? If not, can I have more stacks? Like you said it's a stack-based VM, so no registers. Currently it's a single stack since it's enough. I never encountered a machine with multiple stacks, can you enlighten me about this ? > Can you setup a dedicated block of memory to use as a framebuffer and render it to a <canvas> tag I made this machine to be used by students to test compilers, But I think I'll add this feature in case there is any ambitious student who wants to embed a graphics library in his language :) Thanks,
- jawb 13y agoI think I understand the thing about word size, thank you I'll fix it
- tylerkahn 13y agoI had to write one of these (in C) for a class a couple years ago. https://github.com/tylerkahn/MIPS-Virtual-Machine https://github.com/tylerkahn/MIPS-Virtual-Machine
- angersock 13y agoYay MIPS! Also, let us not forget the Halt and Spontaneously Combust (HSC) instruction: ftp://reports.stanford.edu/pub/cstr/reports/csl/tr/86/289/CSL-TR-86-289.pdf .
- gcr 13y agoWhoa dude, this is awesome! I once wrote a MIPS assembler+VM in Racket. It went OK but I never used it for anything: https://github.com/gcr/mips-simulator https://github.com/gcr/mips-simulator Was a great learning experience though.
- Xelom 13y agoBetter name it VM.js !!
- gkya 13y agoI real disappointed when I discovered this was not an amateur JS vm that I could read in a weekend or two, that would be awesome to have :(
- jawb 13y agoSorry for disappointing you, the name is correct, what you're looking for is called a javascript engine like V8, SpiderMonkey ...
- jawb 13y agoI didn't want to call it VM.js since it sounds like a library, this project is a library (you can find it in js/vm.js) + UI which uses the library and define IO methods and callbacks. It's a webapp, briefly
- knome 13y agoYour VM will work much faster if you take the time to do a second pass over the generated "code" and resolve the jumps to LABELs all at once instead of linearly scanning the code for the appropriate target on each jump.
- jawb 13y agoI agree with you, I'll fix these ASAP.