我遇到了一個非常特殊的問題。 對於VM我需要從該指令的功能拷貝代碼到一個UBYTE數組,然後執行該陣列(該TECHNIC類同於GCC內嵌宏VM),基本上它的工作原理是這樣的:D內聯彙編程序:函數調用出錯
__gshared void * sp = null, sb = null; //stack pointer and stack base
__gshared void add() //the function is just there to access the instruction code
{
asm{db "INSTRUCTIONCODESTART";} //this is a key to know where the instruction code starts
//instruction code here (sample instruction add, pops 2 values from the stack and pushes its result)
sp += 4;
*cast(uint*)sp += *cast(uint*)(sp - 4);
asm{db "INSTRUCTIONCODEEND";} //this is a key to know where instruction code ends
}
在Init方法,每個指令代碼都有自己的緩衝區,緩衝區中的每個字節都是INSTRUCTIONCODESTART和INSTRUCTIONCODEEND鍵之間的字節。我通過windows VirtualProtect調用使該數組可執行。到目前爲止,一切都按預期工作,但是當我試圖做一個函數調用作爲指令時,我會得到一個錯誤。
__gshared void testcall(){}
__gshared void call()
{
asm{db "INSTRUCTIONCODESTART";} //this is a key to know where the instruction code starts
//instruction code here (just calls a D function)
testcall(); //this somehow throws an error
asm{db "INSTRUCTIONCODEEND";} //this is a key to know where instruction code ends
}
順便說一句我用下面的代碼測試說明
void instructiontest()
{
uint dummy;
ubyte[] buf = getFunctionCode(&add) ~ 0xC3; //gets code of instruction, appends 0xC3 at it ("ret" instruction, for test purposes only to see if it returns to the D code without errors)
VirtualProtect(cast(void*)buf, buf.length, PAGE_EXECUTE_READWRITE, &dummy); //makes it executeable
dummy = cast(uint)&buf[0];
asm
{
call dummy[EBP];
}
print("instruction worked without errors!");
}
到目前爲止,每一個簡單的指令(添加,MUL子,push0,push1,...)的作品,但如果我嘗試通過函數調用來獲取指令的代碼,它會拋出一個錯誤
我會很高興,非常感謝任何幫助。 (順便說一句,我需要在指令函數調用爲了讓腳本語言與溝通d)
僅供參考:'__gshared'對功能,只有變量沒有影響。 – 2012-07-15 12:14:12