汇编语言错误

时间:2012-03-27 23:32:23

标签: assembly

我一直试图调试我的程序,但是当我调用我的函数时似乎总是被抓住了。我收到错误说

  

“windows32.exe中0x0018fed8处的未处理异常:0xC0000005:   访问冲突。“

我尝试过研究,我收集的是它是一个堆栈错误。还有其他可能吗?

    .586
.MODEL FLAT
INCLUDE io.h
.STACK 4096     

.DATA           ; reserve storage for data
number1         WORD        ?
anArray         WORD        100 DUP (?)
count           WORD        ?
search          WORD        ?
prompt1         BYTE        "Enter a number or -1 to quit.", 0
prompt2         BYTE        "Enter a number to search for", 0
prompt3         BYTE        "Search for another number Y/N",0
inString        BYTE        40 DUP (?)
searchString    BYTE        16 DUP (?)
outMsgLabel     BYTE        "Search Result", 0
frontOut1       BYTE        6 DUP (?)
outMsg1         BYTE        " is element"
rearOut1        BYTE        6 DUP (?),0
frontOut2       BYTE        6 DUP (?)
outMsg2         BYTE        " is not in array",0

EXTERN          function1:PROC

.CODE           ; start of main program code
_MainProc       PROC
                lea         ebx, anArray
                mov         cx, count
moveThrough:    input       prompt1, inString, 40   ; read ASCII characters
                atow        inString                ; convert to integer
                cmp         ax,0                    ; check for -1
                jl          next
                mov         [ebx], eax              ; store in memory
                add         ebx,2                   ; move to next location in array
                inc         ecx
                cmp         ecx,50                  ; check to make sure array isn't over 50
                je          next
                jmp         moveThrough             ; jump to add more numbers


next:           lea         ebx, anArray            ; get address of array
                mov         count, cx
                input       prompt2, inString, 40   ; prompt for number to search for
                atow        inString
                mov         dx,ax
                mov         cx,count                ; prep cx to be the counter
                lea         eax, anArray
                push        eax
                push        edx
                push        ecx
                call        function1
                add         esp,6
                cmp         eax,0
                je          notThere
                jmp         equalTo

notThere:       wtoa        frontOut2, search
                output      outMsgLabel,frontOut2   ; output message
                jmp         searchAgain

equalTo:        wtoa        frontOut1, search
                wtoa        rearOut1, ax
                output      outMsgLabel,frontOut1   ; output message
                mov         cx,count
                jmp         searchAgain



searchAgain:    input       prompt3, searchString, 16 ; prompt for search again input
                cmp         searchString,"n"          ; check for n
                je          end1
                cmp         searchString,"N"          ; check forN
                je          end1
                jmp         next

end1:


                mov         eax, 0                  ; exit with return code 0
                ret
_MainProc       ENDP
                END                                 ; end of source code

.586
.MODEL FLAT
.CODE

;void function1(int count, int search, int array[])
;outputs whether the search is in the array
function1       PROC
                push        ebp         
                mov         ebp,esp
                push        ebx
                push        ecx
                push        edx
                push        esi
                mov         ecx,[ebp]
                mov         edx,[ebp+2]
                mov         esi,[ebp+4]

arraySearch:    mov         ax, dx                      
                cmp         [esi],ax                ; check if number is in array
                je          equalTo                 
                add         ebx,2                   ; move to next number in array
                loop        arraySearch             ; loop back to top

notThere:       pop         esi
                pop         edx
                pop         ecx
                pop         ebx
                mov         ax,0
                ret

equalTo:        pop         esi
                pop         edx
                pop         ecx
                pop         ebx
                inc         cx
                mov         ax,cx
                ret

                ret
function1       ENDP

END

2 个答案:

答案 0 :(得分:0)

atow是否期望任何机会出现以NULL结尾的字符串? 0xC0000005是内存错误,也可能是由写入不属于您的内存,将多字节值写入未对齐位置(例如,将32位值移动到奇数内存地址)等引起的。它不仅限于堆栈相关的错误。

<强>更新 function中的以下行应该调整esi而不是ebx吗?

            add         ebx,2                   ; move to next number in array

我已经很长时间没有读过X86了,所以我对寄存器交互的记忆有点模糊。

更新2 EXTERN关键字是否告诉汇编程序查看其他库? EXTERN通常意味着“在其他地方寻找符号”。老实说,我可以说我从未使用过装配以外的任何单元。这些天我大多看到拆解代码。如果它希望分支到非本地的某个地方,该程序可能会跳到太空。你在使用什么汇编程序?

答案 1 :(得分:0)

  1. 为什么在调用function1后向esp添加6?这是32位汇编,堆栈是DWORD对齐的,你推了3个32位寄存器,所以你应该在调用function1后添加12到esp。

  2. 在function1中,您设置了一个堆栈框架:

    推送ebp

    mov ebp,esp

  3. 你在哪里弹出ebp并恢复堆栈指针?这两个错误会让你的程序搞得很糟糕!

    哦,就是说,函数的参数从[ebp + 8]开始,每个参数为该偏移量加4。 第一个参数= [ebp + 8] 第2名= [ebp + 12] 3 = [ebp + 16] 等...