/x64js

Primary LanguageJavaScript

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 and dd pseudo-instructions (from nasm)
  • Implement section .text with global 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

Other cool things

Random tips

  • In [rax*2], the square brackets act just like the C * dereference operator. In this case, if rax held a pointer (let's say 4), the interpreter would calculate 4*2 and then dereference whatever was in memory address 8.

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.