Last active
December 21, 2015 00:18
-
-
Save awreece/6218762 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
registers = ["eax", "ebx", "ecx", "edx", "esi", "edi", "esp", "ebp"]; | |
function hex(v) { | |
var parts = new Array(8); | |
for (var i = 0; i < 8; i++) { | |
parts[8 - i] = (v & 0xf).toString(16); | |
v >>= 4 | |
} | |
return "0x" + parts.join(""); | |
} | |
var MAX_STACK = 10; | |
function State (prevState) { | |
this.prevState = prevState; | |
self = this; | |
if (prevState) { | |
this.stack = prevState.stack.slice(0) | |
registers.forEach(function (reg) { | |
self[reg] = prevState[reg]; | |
}); | |
} else { | |
this.stack = new Array(MAX_STACK); | |
for (var i = 0; i < MAX_STACK; i++) { | |
this.stack[i] = 0; | |
} | |
this.eax = 0; this.ebx = 0; this.ecx = 0; this.esi = 0; this.edi = 0; | |
this.esp = this.stackBase; this.ebp = this.stackBase; | |
} | |
} | |
State.prototype.stackBase = 0xfffffe00 | |
State.prototype.toString = function () { | |
var self = this; | |
var ret = ""; | |
for (var stackIndex = 0; stackIndex < this.stack.length; stackIndex++) { | |
ret += hex(self.stackBase - (stackIndex << 2)); | |
ret += ": " + hex(self.stack[stackIndex]) + "\n" | |
} | |
registers.slice(0,4).forEach(function (v) { | |
ret += v + ": " + hex(self[v]) + " "; | |
}); | |
ret += "\n"; | |
registers.slice(4).forEach(function (v) { | |
ret += v + ": " + hex(self[v]) + " "; | |
}); | |
return ret; | |
} | |
function Command(regex, callback) { | |
regex = regex.replace("REG", "%(" + registers.join("|") + ")"); | |
regex = regex.replace("IMM", "\\$(0x\\d+|\\d+)"); | |
regex = "^" + regex + "$"; | |
this.regex = new RegExp(regex, "i"); | |
this.callback = callback; | |
} | |
var commands = [ | |
new Command("pushl? REG", function (state, reg) { | |
stackIndex = (state.stackBase - state.esp) >> 2; | |
state.stack[stackIndex] = state[reg] | |
state.esp -= 4; | |
}), | |
new Command("pushl? IMM", function (state, imm) { | |
stackIndex = (state.stackBase - state.esp) >> 2; | |
state.stack[stackIndex] = parseInt(imm); | |
state.esp -= 4; | |
}), | |
new Command("popl? REG", function (state, reg) { | |
stackIndex = (state.stackBase - state.esp) >> 2; | |
state[reg] = state.stack[stackIndex] | |
state.esp += 4; | |
}), | |
new Command("movl? IMM, ?REG", function(state, imm, reg) { | |
state[reg] = parseInt(imm); | |
}), | |
new Command("movl? REG, ?REG", function(state, src, dst) { | |
state[dst] = parseInt(src); | |
}), | |
] | |
State.prototype.eval = function (string) { | |
var newState = new State(this); | |
var matched = commands.some(function (command) { | |
var found = string.match(command.regex); | |
if (found) { | |
var args = [newState]; | |
args.push.apply(args, found.slice(1)); | |
command.callback.apply(this, args); | |
return true; | |
} | |
return false; | |
}); | |
if (matched) { | |
return newState; | |
} else { | |
throw "No matching command for " + string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment