f a 0 Report post Posted May 23, 2006 (edited) ..ni sambungan dari Visual Studio.net thread. aku buat thread baru sebab rase macam dah melencong abis dah kot thread tu kiter bleh bina assembly ngan native code, i mean create methods ngan native asm, bukan il asm. only visual c++ (yg aku aware laa) yang bleh lakukan ni.. agaknya ni laa .net compiler paling power ye tak.. actually Mono bleh support native unmanaged method kalau dia nak.. cuma belum lagi laa kot.. aku dah buat brape experiment ngan fasm tapi nampak gayanya kalau nak support sintaks cam kat dalam spec (ilasm) takleh laa kot.. aku cuma nak memudahkan creations of metadata tables using syntax aku sendiri... so bleh laaa aku experiment banyak2 ngan .net nih.. hehehe..ari tu aku guna ilasm's ".emitbyte" directive untuk generate native code, e. g public static void F() native unmanaged { .emitbyte 0xC3 } camtu laa konon2nya.. but since benda ni ader "header" kat awal, bila aku call dari managed method dapatlaa exception sebab dia execute "header" tu dulu.. guna pointer manipulation (e.g ldftn, add) pun tak berjaya gak.. susah tul takde debugger nih.. aku test inline assembly Managed C++ pakai simple useless kod ni: #using <mscorlib.dll> #pragma optimize( "asp", on ) #pragma unmanaged int testing(int a, int b) { int rtn = 0; __asm { mov eax, [a] add eax, [b] mov [rtn], eax } return rtn; } #pragma managed int main() { int a = 5; int b = 10; System::Console::WriteLine("a + b = {0}" , testing(a,b).ToString()); } and mase aku call direcly gune mono 1.1.7 (for windows) , aku dapat result macam nie:so, aku try compile gune --aot dapat plak macam ni:sebab takde 'as' (assembler kot) dalam windowz aku. so aku test kat suse aku plak, boleh compile tapi tak leh run.aku rase yang Managed C++ ni memang 'terpakse' support inline assembly untuk backward compatible (berdasarkan error dari mono tu sendiri). ye la. nak ildasm pon tak leh kat function tu (tak support)..aku tak test lagi kat dotgnu. lambat gile nak download gune dailup nihkalo ko nak call ilasm code dari fasm, takde ke dalam mscoree? aku tengok export function dalam mscoree tu memang takde il, tapi aku rase boleh selit2 kot kat mane2 function. nanti aku try test tengok. tapi takde la sampai tahap code injection. hehe.. tapi dalam export function mscoree.dll tu ade banyak yang aku rase macam boleh gune kot.maybe link ni berguna untuk ko kot: http://blogs.msdn.com/netcfteam/archive/20.../24/442612.aspx Edited June 4, 2006 by F A Quote Share this post Link to post Share on other sites
Ancient One 3 Report post Posted May 30, 2006 FA : .net, Mono Dan Portable.net., Test area.hehe.. tambah la sekali sscli kat title.Managed C++ tu bukan terpaksa support inline assembly.. dia memang dah support inline assembly. yang terpaksa kat sini adalah nak tambah /clr switch tu so existing C++ code yg selama ni dikompil ke native machine instructions bleh dikompil kepada il bytecode. team C++ tuh tak buat dari scratch, compare ngan team C# yang bleh start dari 0. thats why diaorang panggil embedded unmanaged native code dalam managed .net program tu IJW, stands for It Just Works! cam magic laa...actually aper yg aku perlukan simple jer, sebijik "hybrid" assembler yang bleh compile native x86 assembly dan jugak il assembly. sort of gabungan fasm ngan ilasm laa.. ilasm takleh compile native unmanaged method kecuali p/invoke(call ke external dlls) sebab dia kena support round-tripping (dengan ildasm). Tak semer native unmanaged code bleh di"disassemble" dan di"assemble" semula tanpa sebarang perubahan. fasm plak dah tentulah takleh compile il assembly.. hehe.. Visual C++ bleh, cuma dia C++ language. Setakat ni aku tak jumpa lagi assembler camni.setau aku dari unmanaged application (non .net app) kita bleh aper call managed method. ader beberapa cara tapi aku tak brape nak amik tau. satu jer aku sure, kita bleh bina normal dll yg mana kita expose managed method sebagai unmanaged export (cam dalam dll tradisional laa cuma bezanya dll ni kena ader .net runtime untuk digunakan). prosedurnya takde laa kompleks sangat, ngan gabungan beberapa directives cam .vtfixup, .vtentry, .data ngan .export. nanti aku kasi contoh. kalau nak buat unmanaged app jadik host runtime aku tak reti plak..Since aku takleh nak build sscli so rancangan aku untuk modify sscli ilasm source ikut kemahuan aku terbantut buat masa sekarang. Instead aku telah buat satu benda bodo tapi menarik, iaitu use sscli ilasm nyer source sebagai guide untuk aku buat patch "kotor" untuk .net fx nyer ilasm, dan hasilnya modified ilasm ni bleh generate methoddef metadata yang betul untuk pinvoke (ijw) native unmanaged. So, kod kat bawah .assembly extern mscorlib {} .assembly Test {} .method public static pinvokeimpl(stdcall) int32 Add(int32 x, int32 y) native unmanaged preservesig { //mov eax, [esp+4] .emitbyte 0x8b .emitbyte 0x44 .emitbyte 0x24 .emitbyte 0x4 //mov ecx, [esp+8] .emitbyte 0x8b .emitbyte 0x4c .emitbyte 0x24 .emitbyte 0x8 //add eax, ecx .emitbyte 0x3 .emitbyte 0xc1 //ret 8 .emitbyte 0xC2 .emitbyte 0x8 .emitbyte 0 } .method public static void entry() cil managed { .entrypoint ldc.i4.1 ldc.i4.2 call int32 Add(int32, int32) call void [mscorlib]System.Console::WriteLine(int32) ret } akan hasilkan error berikut jika dikompil dengan .net fx ilasm yang asalC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>ilasm test.ilMicrosoft ® .NET Framework IL Assembler. Version 2.0.50727.42Copyright © Microsoft Corporation. All rights reserved.Assembling 'test.il' to EXE --> 'test.exe'Source file is ANSItest.il(4) : error -- Local (embedded native) PInvoke method, the resulting PE file is unusabletest.il(4) : error -- Cannot compile native/unmanaged methodtest.il(24) : error -- Method cannot have body if it is pinvoke non-ILAssembled global method AddAssembled global method entry***** FAILURE *****C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>tapi modified .net fx ilasm (aku panggil sebagai filasm, DOWNLOAD and extract ke folder yg sama ngan .net fx ilasm) plak takde masalah nak handle kod kat atas :C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>filasm test.ilMicrosoft ® .NET Framework IL Assembler. Version 2.0.50727.42Copyright © Microsoft Corporation. All rights reserved.Assembling 'test.il' to EXE --> 'test.exe'Source file is ANSIAssembled global method AddAssembled global method entryCreating PE fileEmitting classes:Emitting fields and methods:Global Methods: 2;Resolving local member refs: 1 -> 1 defs, 0 refs, 0 unresolvedEmitting events and properties:GlobalResolving local member refs: 0 -> 0 defs, 0 refs, 0 unresolvedWriting PE fileOperation completed successfullyC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>Walaupun kita leh guna switch "/error" dalam ilasm untuk paksa dia create output, but methoddef untuk Add still tak betul since RVA column point kepada method header which should not exist in ijw method. filasm generata semernya ngan betul. tapi body still kena create sendiri laa guna .emitbyte sebab ilasm mmg takkan assemble native assembly code sampai mati.Nasib aku baik sebab terdapat persamaan di antara sscli ngan .net nyer ilasm, tu pasal aku mampu buat patch tuh. Aku tak ingat brape banyak perubahan aku buat (sikit jer kot), kalau kat sscli ilasm (\clr\src\ilasm), sepatutnya modify Assembler::SetPinvoke ngan Assembler::AddMethod.Lepas ni rasanya aku akan seriously cuba modify fasm kod untuk buat hibrid assembler nih. Tapi mungkin amik masa laa kot sebab aku tak penah study kod fasm in details.. aku ingat nak kekalkan kod fasm sedia ada dan support "format cli" untuk activate ilasm nyer sintaks... remove preprocessor module dan ganti ngan parser baru. Banyak benda lagi nak buat.. cuma semuanya masih2 berlegar dalam kepala otak jer.. hehe..kena cari jalan paling mudah dengan kod sedia ada. ader assembler camni baru cool . Quote Share this post Link to post Share on other sites