我在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