user670595
user670595

Reputation:

Unhandled exception at 0x00000005 in : 0xC0000005: Access violation reading location 0x00000005. when making a ret call

My program is supposed to read an integer n from the user and then calculate all the divisors and if they are prime or not. I am using the Irvine 32 library. Now this is the weird part, when I enter in an even number my program executes as it is supposed to. When I enter in and odd number my program gets the error which is the title of this post.

My main Proc:

main PROC
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This block displays greeting and newline;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov edx, OFFSET greeting  
call Writestring
call Crlf   

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This block gets the integer from the user;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call GetInt 
call Crlf

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This block gets calculates the divsiors and prime divisors.;
; It then puts them in to an array to get ready to display.  ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call CalcDivisors

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This block displays the results to the screen.             ;
; in an n-1 by 3 table.                                      ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call Display_Results

exit
main ENDP

Now the Proc that has produces the error:

CalcDivisors PROC uses eax ebx ecx edx esi edi
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Function calculates divisors then pushes them on to an array;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


mov eax,0
    mov ecx,0
    mov ebx,0
    mov edx,0
    mov esi,0
    mov edi,0

    mov ecx,n
    sub ecx,1
    mov eax,n
    mov ebx,divisors
    mov esi,OFFSET prime_arr
    mov edi,OFFSET div_arr
    push eax    

    Calc_Div:
             call dumpregs
             div ebx
             call dumpregs
             cmp edx,0
             jz Calc_Prime_Div
             inc ebx
             mov edx,0
             mov eax,n
             loop Calc_Div

    Calc_Prime_Div:
                    cmp ebx,2
                    jz Push_2_array


                    push ebx
                    push ecx

                    mov eax,0
                    mov eax,ebx
                    mov ecx,ebx
                    mov divisor_counter,ebx
                    sub ecx,2
                    mov ebx,0
                    mov ebx,prime_divisors


                    P1:
                       call dumpregs
                       div ebx
                       call dumpregs
                       cmp edx,0
                       jz Push_div_array
                       inc ebx
                       mov eax,divisor_counter
                       mov edx,0
                       loop P1

     jmp Push_prime_array

     Jump_above:
                call dumpregs
                loop Calc_div
                call dumpregs
                jmp foo

     Push_prime_array:
                      pop ecx
                      pop ebx
                      mov [esi],ebx
                      mov eax,[esi]
                      call writedec
                      add esi,4
                      mov eax,0
                      mov eax,n
                      call dumpregs
                      inc ebx
                      call dumpregs
                      jmp jump_above
                      call dumpregs
     Push_div_array:
                      pop ecx
                      pop ebx
                      mov [edi],ebx
                      mov eax,[edi]
                      call writedec
                      add edi,4
                      mov eax,0
                      mov eax,n
                      call dumpregs
                      inc ebx
                      jmp Jump_above
     Push_2_array:
                  mov [esi],ebx
                  add esi,4
                  inc ebx
                  pop eax
                  jmp Jump_above
      foo:
          ret

CalcDivisors ENDP

Now the line that is giving me the exact error is the following:

foo:
    ret

It is boggling my mind as to why it is crashing when I enter in an odd number for n and not crashing when n is even. Any suggestions?

Upvotes: 1

Views: 1922

Answers (1)

Sergey Podobry
Sergey Podobry

Reputation: 7189

It looks like you forget to pop some values from the stack. Check the number of push and pop instructions executed.

Upvotes: 1

Related Questions