pasm
Version:
Piston X86-64 Assembler
187 lines (183 loc) • 6.82 kB
JavaScript
// Generated by CoffeeScript 1.3.3
(function() {
$(function() {
var browserHeight, compileCode, editor, highlightCodes, myError, showCodeDetails, showData,
_this = this;
$('.byte').live('click', function(e) {
var c, classes, line, _i, _len, _results;
classes = $(this).attr('class').split(' ');
_results = [];
for (_i = 0, _len = classes.length; _i < _len; _i++) {
c = classes[_i];
if (/line/g.test(c)) {
line = parseInt(c.replace('line-', ''));
editor.setLineClass(window.hlLine, "");
window.hlLine = editor.setLineClass(line, "activeline");
_results.push(highlightCodes(line));
} else {
_results.push(void 0);
}
}
return _results;
});
highlightCodes = function(line) {
$('.byte').removeClass('highlight-byte');
$(".line-" + line).addClass('highlight-byte');
return showCodeDetails(line);
};
showData = function(code, name) {
var o, ret, _i, _ref;
if ((code[name] != null) && code[name].length > 0) {
$("." + name).show();
ret = "<tr class=\"inner-header\"><td colspan=\"" + code[name].length + "\"> </td></tr><tr>";
for (o = _i = 0, _ref = code[name].length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; o = 0 <= _ref ? ++_i : --_i) {
ret += "<td>" + code[name][o] + "</td>";
}
ret += "</tr>";
return $("." + name + "-table").html(ret);
}
};
showCodeDetails = function(line) {
var code, i, m, modrm, myPref, o, opcode, p, pref, r, ret, rex, sib, _i, _j, _k, _l, _len, _len1, _len2, _m, _n, _o, _p, _ref, _ref1, _ref2, _ref3;
rex = ['r', 'w', 'x', 'b'];
code = window.code.lines[line];
for (_i = 0, _len = rex.length; _i < _len; _i++) {
r = rex[_i];
$(".rex-" + r).html(0);
$('.details-table').hide();
}
if (code != null) {
if ((code.prefix != null) && code.prefix.length > 0) {
pref = code.prefix.match(/.{1,2}/g);
myPref = "";
for (_j = 0, _len1 = pref.length; _j < _len1; _j++) {
p = pref[_j];
if (p[0] !== '4') {
myPref += p;
}
}
code.prefix = myPref;
showData(code, 'prefix');
}
if ((code.rex != null) && code.rex.length > 0) {
$(".prefix-rex").show();
_ref = code.rex;
for (_k = 0, _len2 = _ref.length; _k < _len2; _k++) {
r = _ref[_k];
$(".rex-" + r).html(1);
}
}
if ((code.code != null) && code.code.length > 0) {
$(".opcode").show();
opcode = "<tr class=\"inner-header\"><td colspan=\"" + code.code.length + "\"> </td></tr><tr>";
for (o = _l = 0, _ref1 = code.code.length - 1; 0 <= _ref1 ? _l <= _ref1 : _l >= _ref1; o = 0 <= _ref1 ? ++_l : --_l) {
opcode += "<td>" + code.code[o] + "</td>";
}
opcode += "</tr>";
$(".opcode-table").html(opcode);
}
if ((code.modrm != null) && code.modrm.length > 0) {
$(".modrm").show();
modrm = parseInt('0x' + code.modrm).toString(2);
for (i = _m = _ref2 = modrm.length; _ref2 <= 7 ? _m <= 7 : _m >= 7; i = _ref2 <= 7 ? ++_m : --_m) {
modrm = "0" + modrm;
}
ret = '';
for (m = _n = 0; _n <= 7; m = ++_n) {
ret += "<td>" + modrm[m] + "</td>";
}
$(".modrm-tr").html(ret);
}
if ((code.sib != null) && code.sib.length > 0) {
$(".sib").show();
sib = parseInt('0x' + code.sib).toString(2);
for (i = _o = _ref3 = sib.length; _ref3 <= 7 ? _o <= 7 : _o >= 7; i = _ref3 <= 7 ? ++_o : --_o) {
sib = "0" + sib;
}
ret = '';
for (m = _p = 0; _p <= 7; m = ++_p) {
ret += "<td>" + sib[m] + "</td>";
}
$(".sib-tr").html(ret);
}
showData(code, 'imm');
return showData(code, 'disp');
}
};
editor = CodeMirror.fromTextArea(document.getElementById('asm'), {
theme: 'monokai',
lineNumbers: true,
gutter: true,
onCursorActivity: function() {
var line;
line = editor.getCursor().line;
editor.setLineClass(window.hlLine, "");
window.hlLine = editor.setLineClass(line, "activeline");
return highlightCodes(line);
},
onChange: function() {
return compileCode(editor.getValue());
}
});
window.hlLine = editor.setLineClass(1, "activeline");
myError = function(err, line) {
var myErrStr;
if (line != null) {
if (typeof line !== "number") {
line = line.line;
}
myErrStr = "Line " + (line + 1) + ": " + err;
} else {
myErrStr = err;
}
window.error = true;
if (myErrStr.length > 500) {
return $('#error').html(myErrStr.substring(0, 500) + ' ...');
} else {
return $('#error').html(myErrStr);
}
};
window.Opcode.error = myError;
pasm.parseError = myError;
compileCode = function(value) {
var key, res, s, show, str, val, _i, _ref, _ref1;
window.error = false;
try {
res = pasm.parse(value);
if ((res != null) && !window.error) {
show = '';
window.code = res;
_ref = res.lines;
for (key in _ref) {
val = _ref[key];
val.final = val.final.toUpperCase();
str = val.final.match(/.{1,2}/g);
if (str.length > 1) {
for (s = _i = 0, _ref1 = str.length - 2; 0 <= _ref1 ? _i <= _ref1 : _i >= _ref1; s = 0 <= _ref1 ? ++_i : --_i) {
show += "<div class=\"byte line-" + key + "\">" + str[s] + " </div>";
}
show += "<div class=\"byte line-" + key + "\">" + str[s] + "</div><span> </span>";
} else {
show += "<div class=\"byte line-" + key + "\">" + str + "</div><span> </span>";
}
}
$('#bytecode').html(show);
return $('#error').html("");
}
} catch (err) {
}
};
compileCode(editor.getValue());
highlightCodes(1);
browserHeight = $(window).height();
editor.getWrapperElement().style.height = (browserHeight - 230) + 'px';
$('.codemirror-scroll').css('height', (browserHeight - 230) + 'px');
editor.refresh();
return window.onresize = function() {
browserHeight = $(window).height();
editor.getWrapperElement().style.height = (browserHeight - 230) + 'px';
$('.codemirror-scroll').css('height', (browserHeight - 230) + 'px');
return editor.refresh();
};
});
}).call(this);