feat: show assembling result

This commit is contained in:
root 2022-08-16 00:20:59 +02:00
parent 61dec70bcb
commit 553a46eb48
1 changed files with 118 additions and 39 deletions

157
main.html
View File

@ -211,21 +211,21 @@
<span class="tui-fieldset-text right" style="margin-bottom: -5px;" id="syntax">&nbsp;Syntax:Intel&nbsp;</span> <span class="tui-fieldset-text right" style="margin-bottom: -5px;" id="syntax">&nbsp;Syntax:Intel&nbsp;</span>
<textarea id="editor" class="tui-textarea full-width" style="overflow: scroll;height:100%;width:100%"></textarea></div> <textarea id="editor" class="tui-textarea full-width" style="overflow: scroll;height:100%;width:100%"></textarea></div>
<div class="tui-window tui-no-shadow" style="height:20%;width:780px"> <div class="tui-window tui-no-shadow" style="height:20%;width:1000px">
<fieldset class="tui-fieldset" style="height:100%"> <fieldset class="tui-fieldset" style="height:100%">
<legend class="center">Results</legend> <legend class="center">Results</legend>
<div id="result" class="" style="overflow: scroll;height:100%;width:100%"></div> <div id="result" class="" style="overflow: scroll;height:100%;width:100%"></div>
</fieldset> </fieldset>
</div> </div>
<div class="tui-window tui-no-shadow" style="height:20%;width:49%"> <div class="tui-window tui-no-shadow" style="height:20%;width:520px">
<fieldset class="tui-fieldset" style="height:100%"> <fieldset class="tui-fieldset" style="height:100%">
<legend class="center">Memory zones</legend> <legend class="center">Memory zones</legend>
<div id="result2" class="" style="overflow: scroll;height:100%;width:100%"></div> <div id="result2" class="" style="overflow: scroll;height:100%;width:100%"></div>
</fieldset> </fieldset>
</div> </div>
<div class="tui-window full-width tui-no-shadow" style="height:10%"> <div class="tui-window full-width tui-no-shadow" style="height:14%">
<fieldset class="tui-fieldset" style="height:100%"> <fieldset class="tui-fieldset" style="height:100%">
<legend class="center">Status</legend> <legend class="center">Status</legend>
<div id="status" class="full-width" style="overflow: scroll;height:100%"></div> <div id="status" class="full-width" style="overflow: scroll;height:100%"></div>
@ -294,7 +294,7 @@ function toResult(atype)
} }
function newfile(){ function newfile(){
$("#editor").val(".org 0000000\n.code 16\n.title vector\ndw 0xFFFF,0xFFFF,0xFFFF\n.org 100000\n.title prog\n.code 32\nmov ax,1200\nmov esi,0x00000000\nmov edi,0x00000000\n.org 1000\nmov ecx,0x0000FFFF\nrep movsb\nhlt\n.org 100\n\.title 64bits\n.code 64\nmov rax,01"); $("#editor").val(".org 0000000\n.code 16\n.title vector\ndw 0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x1233,0x4874,0x7847,0x1887,0x1887,0x1887\n.org 100000\n.title prog\n.code 32\nmov ax,1200\nmov esi,0x00000000\nmov edi,0x00000000\n.org 1000\nmov ecx,0x0000FFFF\nrep movsb\nhlt\n.org 1FFFFFFFF\n\.title 64bits\n.code 64\nmov rax,01");
$("#result").html(""); $("#result").html("");
setFilename("new.asm"); setFilename("new.asm");
} }
@ -356,27 +356,64 @@ function realopen(file)
} }
}; };
function hexQword(value) {
value = value < 0 ? (value + 0x10000000000000000) : (value);
var str = '0000000000000000' + value.toString(16).toUpperCase();
return str.slice(-16);
};
function hexDword(value) {
value = value < 0 ? (value + 0x100000000) : (value);
var str = '00000000' + value.toString(16).toUpperCase();
return str.slice(-8);
};
function hexWord(value) {
value = value < 0 ? (value + 0x10000) : (value);
var str = '0000' + value.toString(16).toUpperCase();
return str.slice(-4);
};
function hexByte(value) { function hexByte(value) {
value = value < 0 ? (value + 0x100) : (value); value = value < 0 ? (value + 0x100) : (value);
var str = '00' + value.toString(16).toUpperCase(); var str = '00' + value.toString(16).toUpperCase();
return str.slice(-2); return str.slice(-2);
}; };
function Render(result,type) { function dumpall(zones)
var mcLen = result.length; {
var output = '<table class=""><tr><td>'; if (type=="binary")
var output = '<table class="">';
else
var output = '';
for (const zone of zones)
output+=dump(zone);
if (type=="binary")
output += '</table>';
showresult(output);
}
function dump(zone) {
var mcLen = zone.asm.length;
if (type=="binary")
var output = '<tr>';
else
var output = '';
switch (type) { switch (type) {
case 'binary': case 'binary':
var address=zone.offset;
for (var offset = 0; offset < mcLen; offset += 16) { for (var offset = 0; offset < mcLen; offset += 16) {
output += "<td>"+toHex(address,zone.code)+"</td><td>&nbsp;|&nbsp;</td><td>";
var ascii=''; var ascii='';
for (var i = offset; i < offset + 16 && i < mcLen; i++) { for (var i = offset; i < offset + 16 && i < mcLen; i++) {
if (result[i]>31) if (zone.asm[i]>31)
ascii += String.fromCharCode(result[i]); ascii += String.fromCharCode(zone.asm[i]);
else else
ascii += '.'; ascii += '.';
output += hexByte(result[i]) + " "; output += hexByte(zone.asm[i]) + " ";
} }
output += "<td>&nbsp;|&nbsp;</td><td>"+ascii+"</td></tr><td>"; output += "</td><td>&nbsp;|&nbsp;</td><td>"+ascii+"</td></tr>";
address+=16;
} }
break; break;
case 'array': case 'array':
@ -384,7 +421,7 @@ function Render(result,type) {
for (var offset = 0; offset < mcLen; offset += 8) { for (var offset = 0; offset < mcLen; offset += 8) {
output += " "; output += " ";
for (var i = offset; i < offset + 8 && i < mcLen; i++) { for (var i = offset; i < offset + 8 && i < mcLen; i++) {
output += "0x" + hexByte(result[i]) + ", "; output += "0x" + hexByte(zone.asm[i]) + ", ";
} }
output = output.substring(0, output.length - 1); output = output.substring(0, output.length - 1);
output += "\n"; output += "\n";
@ -397,7 +434,7 @@ function Render(result,type) {
for (var offset = 0; offset < mcLen; offset += 16) { for (var offset = 0; offset < mcLen; offset += 16) {
output += " \""; output += " \"";
for (var i = offset; i < offset + 16 && i < mcLen; i++) { for (var i = offset; i < offset + 16 && i < mcLen; i++) {
output += "\\x" + hexByte(result[i]); output += "\\x" + hexByte(zone.asm[i]);
} }
output += "\"\n"; output += "\"\n";
} }
@ -408,12 +445,24 @@ function Render(result,type) {
return output; return output;
} }
function showzones(zones)
{
table='<table class="tui-table striped-purple"><thead><tr><th>Id</th><th>Title</th><th>Offset</th><th>Size</th><th>Type</th><th>Compiled</th></tr></thead>';
for (const zone of zones)
table+="<tr><td align='right'>"+zone.id+"</td><td align='left'>"+zone.title+"</td><td align='right'>"+zone.realoffset+"</td><td align='right'>"+zone.asm.length+"</td><td align='right'>"+zone.code+"</td><td align='right'>"+(zone.asm.length>0)+"</td></tr>";
table+="</table>"
$("#result2").html(table);
}
function make() function make()
{ {
var assembly = $("#editor").val(); var assembly = $("#editor").val();
id=0; var id=0;
org=0; var org=0;
type=32; var type=32;
var zones=[];
var status=true;
showresult('');
while (assembly.length!=0) while (assembly.length!=0)
{ {
pos=assembly.search(/\.org [0-9A-F]+/); pos=assembly.search(/\.org [0-9A-F]+/);
@ -451,32 +500,62 @@ function make()
pos=src.search(/\.code [0-9]+/); pos=src.search(/\.code [0-9]+/);
if (pos>-1) if (pos>-1)
{ {
type=src.substring(pos,src.length); code=src.substring(pos,src.length);
pos=type.search(/\n/); pos=code.search(/\n/);
type=parseInt(type.substring(6,pos),10); code=parseInt(code.substring(6,pos),10);
}
if (code!=16 && code!=32 && code!=64)
code=32;
zone={"id":id,"offset":offset,"src":src,"size":0,"title":title,"code":code,"asm":[]};
if (assemble(zone)==true)
{
zones.push(zone);
dumpall(zones);
showzones(zones);
} }
if (type!=16 && type!=32 && type!=64)
type=32;
if (assemble(id,title,src,type,offset)==0)
asm=true;
else else
asm=false; {
zones.push({"id":id,"offset":offset,"src":src,"size":0,"title":title,"type":type,"asm":asm}); zones.push(zone);
showzones(zones);
return;
}
id++; id++;
} }
showstatus("Project is built");
} }
function assemble(id, title, src, type, offset) function showstatus(text)
{ {
status=$("#status").html(); $("#status").html($("#status").html()+text+"<br>");
allstatus=status+"Assembling zone "+id+":"+title+"@"+offset+" ("+type+"bits) => "; $("#status").scrollTop($("#status")[0].scrollHeight);
$("#status").html(allstatus); }
if (type==16)
function showresult(text)
{
$("#result").html(text);
}
function toHex(hexa,code)
{
if (code==16)
result=hexWord(hexa);
else if (code==32)
result=hexDword(hexa);
else
result=hexQword(hexa);
return result;
}
function assemble(zone)
{
if (zone.code==16)
realtype=ks.MODE_16; realtype=ks.MODE_16;
else if (type==32) else if (zone.code==32)
realtype=ks.MODE_32; realtype=ks.MODE_32;
else else
realtype=ks.MODE_64; realtype=ks.MODE_64;
zone.realoffset=toHex(zone.offset,zone.code);
allstatus="Assembling zone "+zone.id+":"+zone.title+"@"+zone.realoffset+" ("+zone.code+"bits) => ";
a = new ks.Keystone(ks.ARCH_X86, realtype); a = new ks.Keystone(ks.ARCH_X86, realtype);
if (syntax=="att") if (syntax=="att")
a.option(ks.OPT_SYNTAX, ks.OPT_SYNTAX_ATT); a.option(ks.OPT_SYNTAX, ks.OPT_SYNTAX_ATT);
@ -484,19 +563,19 @@ function assemble(id, title, src, type, offset)
a.option(ks.OPT_SYNTAX, ks.OPT_SYNTAX_NASM); a.option(ks.OPT_SYNTAX, ks.OPT_SYNTAX_NASM);
try try
{ {
result = a.asm(src, offset); zone.asm = a.asm(zone.src, zone.offset);
$("#result").html(Render(result,type)); allstatus+="DONE";
$("#status").html($("#status").html()+"DONE<br>"); showstatus(allstatus);
$("#status").scrollTop($("#status")[0].scrollHeight);
a.close(); a.close();
return 0 return true;
} }
catch(err) catch(err)
{ {
$("#status").html($("#status").html()+"ERROR: "+err+"<br>"); zone.asm = [];
$("#status").scrollTop($("#status")[0].scrollHeight); allstatus+="ERROR: "+err;
showstatus(allstatus);
a.close(); a.close();
return 1 return false;
} }
} }
</script> </script>