【问题标题】:Exception thrown at 0x00406A09 in Project.exe: 0xC0000005: Access violation executing location 0x00406A09在 Project.exe 中的 0x00406A09 处引发异常:0xC0000005:访问冲突执行位置 0x00406A09
【发布时间】:2016-11-04 15:10:52
【问题描述】:

这可能是什么原因?我是汇编(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

我很确定代码可以正常运行,直到返回部分。 PS:除此之外我还有其他代码,其中会调用 wordMatching PROC。

【问题讨论】:

  • 如果您已到达 either 字符串 ([esi] == 0 || [edx] == 0) 的末尾,您应该退出循环。目前,如果[esi] == 0 && [edx] == 0,您将退出。
  • 我认为 jmp loopWord4 等于退出循环。 loopWord4 只是打印一个字符串,然后将其返回给调用语句
  • 您到达jmp loopWord4 的唯一时间是如果both [esi] == 0[edx] == 0 在同一迭代中,即[esi] == 0 && [edx] == 0。那不是你想要的。您想在到达 either 字符串的末尾时立即退出循环。

标签: assembly masm masm32 irvine32


【解决方案1】:

在代码的开头放置一个断点(在执行push EDX之前),记下堆栈地址esp加上堆栈中的值(返回地址给调用者)。

然后在ret 处设置断点。运行代码。检查esp

(你永远不会执行pop EDX,你有它在代码中,但它在je + jne之后,所以实际上无法访问)。

关于比较的逻辑,可以简化很多:

.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,现在已修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 2021-03-06
    • 1970-01-01
    • 2016-07-14
    • 2021-07-03
    • 2020-08-09
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多