//汇编转 aardio 函数 import win.ui; import process import fsys; /*DSG{{*/ var winform = win.form(text="汇编转 aardio 函数";right=563;bottom=492) winform.add( btnDown={cls="button";text="下载头文件";left=390;top=465;right=476;bottom=490;db=1;dr=1;z=8}; btnEdit={cls="button";text="转换到编辑器";left=404;top=4;right=552;bottom=29;dr=1;dt=1;z=4}; btnGen={cls="button";text="生成文件";left=480;top=465;right=552;bottom=490;db=1;dr=1;z=7}; btnInstruction={cls="button";text="x86 指令集";left=192;top=465;right=270;bottom=490;db=1;dr=1;z=10}; btnPdf={cls="button";text="FASM 用户手册";left=274;top=465;right=386;bottom=490;db=1;dr=1;z=9}; button={cls="button";text="转换";left=337;top=4;right=394;bottom=29;dr=1;dt=1;z=1}; chkAtt={cls="checkbox";text="AT&&T注释";left=18;top=469;right=102;bottom=489;db=1;dr=1;z=11}; chkHex={cls="checkbox";text="HEX 格式";left=106;top=469;right=183;bottom=489;db=1;dr=1;z=13}; editAsm={cls="edit";left=15;top=33;right=557;bottom=233;ah=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=2}; editByteCode={cls="edit";left=15;top=239;right=554;bottom=462;db=1;dl=1;dr=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=3}; editParams={cls="edit";text="void( INT id, struct & cpuid )";left=77;top=5;right=334;bottom=28;align="right";dl=1;dr=1;dt=1;edge=1;multiline=1;z=6}; splitter={cls="splitter";left=15;top=232;right=462;bottom=237;dl=1;dr=1;horz=1;z=5}; static={cls="static";text="函数原型:";left=18;top=9;right=75;bottom=25;dl=1;dt=1;transparent=1;z=12} ) /*}}*/ winform.splitter.split( winform.editAsm,winform.editByteCode ) winform.editAsm.text = /* push ebp mov ebp,esp sub esp,0C0h push ebx push ecx push edx push esi mov eax,[ebp+8]; mov esi,[ebp+12]; cpuid mov [esi],eax mov [esi+4],ebx mov [esi+8],ecx mov [esi+12],edx pop esi pop edx pop ecx pop ebx mov esp, ebp pop ebp ret */ import string.template; var templateCode = string.template(); templateCode.template = /*** import raw.asm.cdecl; //写入机器码,并返回函数对象 var func = raw.asm.cdecl("${PARAMS}", ${ASM_CODE} ) ***/ var hexTemplateCode = string.template(); hexTemplateCode.template = /*** import raw.asm.cdecl; //写入机器码,并返回函数对象 var func = raw.asm.cdecl("${PARAMS}"," ${ASM_CODE} ") ***/ import udis86; import raw.fasm; import raw.asm.opCode; winform.button.oncommand = function(id,event){ winform.button.text = "正在编译" winform.button.disabled = true; winform.editByteCode.text = "请稍候......" var binCode,err,line = raw.fasm.assemble32( winform.editAsm.text ) if(!binCode){ winform.editByteCode.text = string.concat("错误: ",err," 错误行: #",tostring(line) ); winform.button.text = "转换" winform.button.disabled = false; return; } var tAsmCode = {}; var ud = udis86(); if( winform.chkAtt.checked ){ ud.translateAtt(); //使用ATT汇编语法 } ud.setInputBuffer(binCode); //输入机器码 if(winform.chkHex.checked){ while( ud.disassemble() ){ var byteCode = string.replace( ud.byteCode() ,".", function(x){ return string.format("%02X ",x[1] ); } ); table.push(tAsmCode,string.format(" %s %s",byteCode, ud.asm) ); } asmCode = string.join(tAsmCode,'\r\n'); winform.editByteCode.text = hexTemplateCode.format( ASM_CODE = asmCode; PARAMS = winform.editParams.text ) } else{ while( ud.disassemble() ){ var byteCode = string.replace( ud.byteCode() ,".", function(x){ return string.format("\x%02X",x[1] ); } ); table.push(tAsmCode,string.format(" '%s', // %s",byteCode, ud.asm) ); } asmCode = string.join(tAsmCode,'\r\n'); winform.editByteCode.text = templateCode.format( ASM_CODE = string.trim( asmCode ); PARAMS = winform.editParams.text ) } if( winform.editParams.text = "void( INT id, struct & cpuid )" ){ winform.editByteCode.text = winform.editByteCode.text + ('\r\n\r\n import console;\r\n var cpuid = func( 0,{ INT eax;BYTE ebx[4];BYTE ecx[4];BYTE edx[4] });\r\n console.log( cpuid.ebx ++ cpuid.edx ++ cpuid.ecx )\r\n console.pause()' ) } elseif( string.match(winform.editParams.text,"\(\s*\)") ){ winform.editByteCode.text = winform.editByteCode.text + ('\r\n\r\n import console;\r\n console.log( func() ); console.pause();\r\n ' ) } winform.button.text = "转换" winform.button.disabled = false; } winform.btnEdit.oncommand = function(id,event){ var code = string.trim(winform.editByteCode.text) winform.button.oncommand(); var tempPath = io.tmpname() + ".aardio" string.save(tempPath, winform.editByteCode.text ) process.execute(tempPath); } import fsys.dlg; winform.btnGen.oncommand = function(id,event){ var path = fsys.dlg.open("DLL文件|*.dll|EXE文件|*.exe||","请选择生成文件路径",,winform.hwnd) if(!path)return; winform.btnGen.disabled = true; var binCode,err,line = raw.fasm.assemble32( winform.editAsm.text ) if(!binCode){ winform.editByteCode.text = string.concat("错误:",err," 错误行:",tostring(line) ); winform.btnGen.disabled = false; return; } string.save(path,binCode) winform.btnGen.disabled = false; } import zlib.httpFile; winform.btnDown.oncommand = function(id,event){ if( !..io.exist("/include" ) ){ if(! zlib.httpFile.download("http://download.aardio.com/v10.files/demo/fasm/include.zip","aardio- 下载 FASM 头文件","/include") ){ return; } fsys.attrib("/include",/*移除*/,0x2/*_FILE_ATTRIBUTE_HIDDEN*/) } else { winform.msgbox(" FASM 头文件已下载!") } } import process; winform.btnPdf.oncommand = function(id,event){ process.execute("http://download.aardio.com/v10.files/demo/fasm/FASM.pdf") } winform.btnInstruction.oncommand = function(id,event){ process.execute("http://en.wikipedia.org/wiki/X86_instruction_listings") } import fsys.config; config = ..fsys.config("/config/"); winform.bindConfig( config.winform,{ edit = "text"; checkbox = "checked"; } ); winform.show() win.loopMessage();