gpt4 book ai didi

C 内联程序集生成未处理的异常

转载 作者:太空宇宙 更新时间:2023-11-04 05:51:25 24 4
gpt4 key购买 nike

所以我用C写了这个函数

int transform ( char * p )
{
if(*p!='-'){
return 0;
}

p++;
if(*p == 'a')
{
return 1;
}
else if(*p == 'b')
{
return 2;
}
else
{
return 0;
}
}

我试着像这样将它翻译成内联汇编 ia32

    int trasform ( char * p )
{
int result;
_asm
{
mov eax, p
mov ebx, 0
mov bl, [eax]
cmp bl, '-' ;
jne invalid
mov bl, [4*eax]
cmp bl, 'a'
jne isB
mov result, 1
mov eax, result
jmp out
isB:
cmp bl, 'b'
jne invalid
mov result, 2
mov eax, result
jmp out
invalid:
mov result, 0
mov eax, result
out: ; end

}
return result;
}

当我用 C 编写函数时,它在 Visual Studio 中完美运行,但是当我将其更改为内联汇编并执行代码时,我收到一条错误消息

Unhandled exception at 0x774e15ee in proyect.exe : 0xC0000005: Access violation reading location 0x01745388.

这个问题是代码的问题还是visual studio的问题?

我调试了我的代码,发现错误就在这一行

mov bl, [4*eax] 

最佳答案

mov bl, [4*eax]
cmp bl, 'a'

思路是在EAX中推进指针。不需要乘以它!

inc eax              ;ptr++
mov bl, [eax]
cmp bl, 'a'

关于C 内联程序集生成未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com