This is an attempt to create a toy CPU interpreting a subset of the x86_64 instruction set in JavaScript.
Check out a (sort of working) demo in React here: http://jeffcarp.github.io/x64js/.
TODO
- Add public API usage in README (
aBlankCpu()
,stepProgramOnce()
) - Implement the 13 instructions stubbed in
test/test.js
- Implement labels (could be improved)
- Implement
db
anddd
pseudo-instructions (from nasm) - Implement
section .text
withglobal
keyword - Change cpu.instructionPointer to
rip
- containing "the address of the next instruction to be executed if no branching is done" - Implement comments
Would be really cool to have: a suite of integration tests that took NASM files, actually compiled them using nasm
, and actually compared the output against that of x64js.
Usage
var x64 = require('x64js');
var cpu = x64.aBlankCpu();
cpu = x64.loadProgramFromFile(cpu, './hello-world.asm');
cpu = x64.stepProgramOnce(cpu);
Goals
- To be able to feed this module a reasonably simple NASM file and have it produce the expected output.
- To design this system in functional style, leaving management of state up to you.
Reference
- Intel 80x86 Assembly Language OpCodes (mathemainzel.info)
- x86 Assembly Guide (cs.virginia.edu)
- Say hello to x64 Assembly (0xax.blogspot.com)
- NASM Syntax (github.com/yasm/yasm)
Other cool things
Random tips
- In
[rax*2]
, the square brackets act just like the C*
dereference operator. In this case, ifrax
held a pointer (let's say4
), the interpreter would calculate4*2
and then dereference whatever was in memory address8
.
Assumptions and Limitations
- This cpu can only hold one program in memory at a time.
- There is no operating system.
- Since the program you're executing never has to interact with a dynamic linker, the
global
keyword currently noops.