diff --git a/main.html b/main.html index 52390ad..8a10c29 100644 --- a/main.html +++ b/main.html @@ -211,21 +211,21 @@  Syntax:Intel  -
+
Results
-
+
Memory zones
-
+
Status
@@ -294,7 +294,7 @@ function toResult(atype) } 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(""); 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) { value = value < 0 ? (value + 0x100) : (value); var str = '00' + value.toString(16).toUpperCase(); return str.slice(-2); }; -function Render(result,type) { - var mcLen = result.length; - var output = ''; + else + var output = ''; switch (type) { case 'binary': + var address=zone.offset; for (var offset = 0; offset < mcLen; offset += 16) { + output += ""; + address+=16; } break; case 'array': @@ -384,7 +421,7 @@ function Render(result,type) { for (var offset = 0; offset < mcLen; offset += 8) { output += " "; 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 += "\n"; @@ -397,7 +434,7 @@ function Render(result,type) { for (var offset = 0; offset < mcLen; offset += 16) { output += " \""; for (var i = offset; i < offset + 16 && i < mcLen; i++) { - output += "\\x" + hexByte(result[i]); + output += "\\x" + hexByte(zone.asm[i]); } output += "\"\n"; } @@ -408,12 +445,24 @@ function Render(result,type) { return output; } +function showzones(zones) +{ + table='
'; +function dumpall(zones) +{ + if (type=="binary") + var output = ''; + else + var output = ''; + for (const zone of zones) + output+=dump(zone); + if (type=="binary") + output += '
'; + showresult(output); +} + +function dump(zone) { + var mcLen = zone.asm.length; + if (type=="binary") + var output = '
"+toHex(address,zone.code)+" | "; var ascii=''; for (var i = offset; i < offset + 16 && i < mcLen; i++) { - if (result[i]>31) - ascii += String.fromCharCode(result[i]); + if (zone.asm[i]>31) + ascii += String.fromCharCode(zone.asm[i]); else ascii += '.'; - output += hexByte(result[i]) + " "; + output += hexByte(zone.asm[i]) + " "; } - output += " | "+ascii+"
"; + output += " | "+ascii+"
'; + for (const zone of zones) + table+=""; + table+="
IdTitleOffsetSizeTypeCompiled
"+zone.id+""+zone.title+""+zone.realoffset+""+zone.asm.length+""+zone.code+""+(zone.asm.length>0)+"
" + $("#result2").html(table); +} + function make() { var assembly = $("#editor").val(); - id=0; - org=0; - type=32; + var id=0; + var org=0; + var type=32; + var zones=[]; + var status=true; + showresult(''); while (assembly.length!=0) { pos=assembly.search(/\.org [0-9A-F]+/); @@ -451,32 +500,62 @@ function make() pos=src.search(/\.code [0-9]+/); if (pos>-1) { - type=src.substring(pos,src.length); - pos=type.search(/\n/); - type=parseInt(type.substring(6,pos),10); + code=src.substring(pos,src.length); + pos=code.search(/\n/); + 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 - 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++; } + showstatus("Project is built"); } -function assemble(id, title, src, type, offset) +function showstatus(text) { - status=$("#status").html(); - allstatus=status+"Assembling zone "+id+":"+title+"@"+offset+" ("+type+"bits) => "; - $("#status").html(allstatus); - if (type==16) + $("#status").html($("#status").html()+text+"
"); + $("#status").scrollTop($("#status")[0].scrollHeight); +} + +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; - else if (type==32) + else if (zone.code==32) realtype=ks.MODE_32; else 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); if (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); try { - result = a.asm(src, offset); - $("#result").html(Render(result,type)); - $("#status").html($("#status").html()+"DONE
"); - $("#status").scrollTop($("#status")[0].scrollHeight); + zone.asm = a.asm(zone.src, zone.offset); + allstatus+="DONE"; + showstatus(allstatus); a.close(); - return 0 + return true; } catch(err) { - $("#status").html($("#status").html()+"ERROR: "+err+"
"); - $("#status").scrollTop($("#status")[0].scrollHeight); + zone.asm = []; + allstatus+="ERROR: "+err; + showstatus(allstatus); a.close(); - return 1 + return false; } }