我在C++中有inline asm問題。我試圖實現快速strlen,但它不工作 - 當我使用__declspec(naked)
關鍵字調試器顯示輸入地址爲0x000000,當我不使用該關鍵字時,eax指向一些垃圾,函數返回各種值。C++將參數傳遞給內聯彙編程序函數
這裏是代碼:
int fastStrlen(char *input) // I know that function does not calculate strlen
{ // properly, but I just want to know why it crashes
_asm // access violation when I try to write to variable x
{
mov ecx, dword ptr input
xor eax, eax
start:
mov bx, [ecx]
cmp bl, '\0'
je Sxend
inc eax
cmp bh, '\0'
je Sxend
inc eax
add ecx, 2
jmp start
Sxend:
ret
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char* test = "test";
int x = fastStrlen(test);
cout << x;
return 0;
}
任何人可以點我什麼我做錯了?
你想'mov'不是'lea'。此外,你沒有返回長度,你正在返回一個地址。將16位寄存器與'\ 0''比較是不會做你想要的。順便說一句,這不會非常快,而且你不需要真正做到這一點。 – Jester 2014-09-01 17:48:34
@Jester mov既不工作也不錯,我知道我現在正在返回錯誤的val,但我改變了這個,因爲我認爲這些錯誤是因爲使用ESI寄存器; _; – encoree1337 2014-09-01 17:50:08
它是ABI的具體(實際上取決於處理器,操作系統和編譯器)。 – 2014-09-01 17:52:12