在 Project.exe 中的 0x00406A09 抛出异常:0xC0000005:访问冲突执行位置 0x00406A09
Exception thrown at 0x00406A09 in Project.exe: 0xC0000005: Access violation executing location 0x00406A09
这可能是什么原因造成的?我是汇编 (asm) 编程的新手,我对我的代码中发生的事情感到有点沮丧,因为我已经花了几个小时试图弄清楚它。
.data
stringInput BYTE 21 dup (0)
wrongInput BYTE "That is incorrect", 0
correctInput BYTE "That is correct you win", 0
inputSize = 20
.code
push EDX
mov EDX, OFFSET stringInput
mov ECX, inputSize
call readString
loopWord:
mov AL, [ESI]
mov BL, [EDX]
cmp AL, 0
jne loopWord2
cmp BL, 0
jne loopWord2
jmp loopWord4
loopWord2:
inc ESI ;point to the next
inc EDX ;point to next element
cmp AL, BL ;is the letter equals?
je loopWord ;IF EQUAL loop again
jne loopWord3 ;not equal go out
pop EDX
loopWord3:
mov EDX, OFFSET wrongInput
jmp WordFinish
loopWord4:
mov EDX, OFFSET correctInput
jmp WordFinish
WordFinish:
call WriteString
RET ;the exception is thrown here
WordMatching ENDP
我很确定代码可以正常工作,它可以正常运行,直到 return 部分。
PS: 我还有除此之外的代码,其中将调用 wordMatching PROC。
在代码的开头放置一个断点(在执行 push EDX
之前),记下堆栈地址 esp
加上堆栈中的值(return 调用者的地址)。
然后在ret
下断点。 运行 代码。检查 esp
.
(你永远不会执行 pop EDX
,你在代码中有它,但它在对 je
+ jne
后面,所以实际上无法访问)。
关于compare的逻辑,可以简化很多:
.code
push EDX
mov EDX, OFFSET stringInput
mov ECX, inputSize
call readString
mov EBX, OFFSET wrongInput
loopWord:
mov AL, [ESI]
cmp AL, [EDX]
jne loopWord_wrongInput ; first difference -> wrongInput
inc ESI
inc EDX
test AL,AL
jnz loopWord ; until 0 is found in both strings
; here is the branch for correct word
mov EBX, OFFSET correctInput ; no difference -> switch result string
loopWord_wrongInput:
; display result string
mov EDX,EBX
call WriteString
pop EDX ; your missing "pop EDX" fixed here
ret
编辑:我忘记在第一个版本中增加 esi/edx,现在修复了。
这可能是什么原因造成的?我是汇编 (asm) 编程的新手,我对我的代码中发生的事情感到有点沮丧,因为我已经花了几个小时试图弄清楚它。
.data
stringInput BYTE 21 dup (0)
wrongInput BYTE "That is incorrect", 0
correctInput BYTE "That is correct you win", 0
inputSize = 20
.code
push EDX
mov EDX, OFFSET stringInput
mov ECX, inputSize
call readString
loopWord:
mov AL, [ESI]
mov BL, [EDX]
cmp AL, 0
jne loopWord2
cmp BL, 0
jne loopWord2
jmp loopWord4
loopWord2:
inc ESI ;point to the next
inc EDX ;point to next element
cmp AL, BL ;is the letter equals?
je loopWord ;IF EQUAL loop again
jne loopWord3 ;not equal go out
pop EDX
loopWord3:
mov EDX, OFFSET wrongInput
jmp WordFinish
loopWord4:
mov EDX, OFFSET correctInput
jmp WordFinish
WordFinish:
call WriteString
RET ;the exception is thrown here
WordMatching ENDP
我很确定代码可以正常工作,它可以正常运行,直到 return 部分。 PS: 我还有除此之外的代码,其中将调用 wordMatching PROC。
在代码的开头放置一个断点(在执行 push EDX
之前),记下堆栈地址 esp
加上堆栈中的值(return 调用者的地址)。
然后在ret
下断点。 运行 代码。检查 esp
.
(你永远不会执行 pop EDX
,你在代码中有它,但它在对 je
+ jne
后面,所以实际上无法访问)。
关于compare的逻辑,可以简化很多:
.code
push EDX
mov EDX, OFFSET stringInput
mov ECX, inputSize
call readString
mov EBX, OFFSET wrongInput
loopWord:
mov AL, [ESI]
cmp AL, [EDX]
jne loopWord_wrongInput ; first difference -> wrongInput
inc ESI
inc EDX
test AL,AL
jnz loopWord ; until 0 is found in both strings
; here is the branch for correct word
mov EBX, OFFSET correctInput ; no difference -> switch result string
loopWord_wrongInput:
; display result string
mov EDX,EBX
call WriteString
pop EDX ; your missing "pop EDX" fixed here
ret
编辑:我忘记在第一个版本中增加 esi/edx,现在修复了。