[英]: 0xC0000005: Access violation writing location 0x00000000 C++ with inline assembly
I've been writing this code to implement the C "strcmp()" function using C/C++ with embedded assembly code like this我一直在编写这段代码来实现 C "strcmp()" function 使用 C/C++ 和嵌入式汇编代码这样
bool myStrCmp(char* mystr1, char* mystr2) {
if (myStrLen(mystr1) != myStrLen(mystr1)) return false;
char s1[100], s2[100];
strcpy_s(s1, mystr1);
strcpy_s(s2, mystr2);
int i = 0;
int flag = 1;
_asm mov ecx, flag;
_asm
{
push esi
mov esi,i
startCmp:
mov al,s1[esi]
mov dl,s2[esi]
cmp al,NULL
je endCmp
cmp al,dl
jne zeroFlag
inc [esi]
jmp startCmp
zeroFlag:
mov ecx,0
endCmp:
pop esi
}
_asm mov flag, ecx
return flag == 1;
}
However, there is an exception at the exact line of jne zeroFlag
saying : 0xC0000005: Access violation writing location 0x00000000
但是,在
jne zeroFlag
的确切行有一个异常说: 0xC0000005: Access violation writing location 0x00000000
this exception happens whenever I enter a similar charecters in the first and second string generally每当我在第一个和第二个字符串中输入类似的字符时,就会发生此异常
I have no idea why does this happen我不知道为什么会这样
It seems your debugger stops at the last instruction before the one where an exception occurred.似乎您的调试器在发生异常的指令之前的最后一条指令处停止。 The error is actually at the next line:
错误实际上在下一行:
inc [esi]
That tries to increment a value stored at address esi
.这试图增加存储在地址
esi
的值。 Since esi
is 0, incrementing a value at address 0 results in an access violation.由于
esi
为 0,因此增加地址 0 处的值会导致访问冲突。
To increment esi
itself, simply write:要增加
esi
本身,只需编写:
inc esi
With that said, there's no need to copy C-strings into temporary arrays, you can compare them in-place (and you can optimize the process by comparing in dwords, falling back to a byte compare in the last chunk).话虽如此,没有必要将 C 字符串复制到临时 arrays 中,您可以就地比较它们(并且您可以通过在 dwords 中进行比较来优化过程,回退到最后一个块中的字节比较)。
cmp al,dl
jne zeroFlag
So, you fault on the jne instruction with address NULL.因此,您在地址为 NULL 的 jne 指令上出错了。 This is literally impossible.
这实际上是不可能的。 Remembering how the processor works, it actually faulted on the previous instruction and IP points to the next one;
记住处理器是如何工作的,它实际上在上一条指令上出错,IP 指向下一条; if the debugger doesn't adjust for this it faulted on the cmp instruction, which is equally impossible.
如果调试器没有对此进行调整,它会在 cmp 指令上出错,这同样是不可能的。
Only one possibility.只有一种可能。 The code your running is not the code you see in the debugger.
您运行的代码不是您在调试器中看到的代码。 Rebuild all, and fix your compilation errors, and try again.
全部重建,修复编译错误,然后重试。
You should just replace this with您应该将其替换为
bool myStrCmp(char* mystr1, char* mystr2) {
return 0 == strcmp(mystr1, mystr2);
}
You can't beat the builtin.你不能打败内置的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.