MASM上的访问冲突(Project.exe中0x00000002处的未处理异常:0xC0000005:访问冲突执行位置0x00000002。)

时间:2017-06-01 04:54:37

标签: assembly masm x86-16

所以,我正在访问某个错误的地方,但我无法弄明白。我知道我的代码非常糟糕,所以请随意修复或指出错误。但是,我真正想知道的是如何修复此(标题)特定错误。 我只能理解MASM。 谢谢。

以下是我在MASM中的代码。

 INCLUDE Irvine32.inc

 .data
      v1 dword 2
      v2 dword 2
      s1 byte "Please enter a number:",0
      s3 byte "|       The value will be recognized as non signed      |",0Ah, 0
      s4 byte "|By definition of Prime Number, number can't be negative|",0Ah, 0

 .code

 main proc
      mov edx, offset s4
      call writestring
      mov edx, offset s3
      call writestring
      mov edx, offset s1
      call writestring
      call readdec
      mov ecx, eax
      dec ecx
      dec ecx
      L1:
           push ecx
           push v2
           mov eax, v1
           push eax
           xor edx, edx
           mov ebx, 2
           div ebx
           mov ecx, eax
           call isPrime
           pop v2
           pop ecx
           inc v1
           cmp edx, 0
           JE L4
           LOOP L1

      L4:
           call writedec
           dec ecx
           JMP L1

      exit
 main endp

 isPrime proc
      L2:
           pop eax
           push eax
           xor edx, edx
           div v2
           cmp edx, 0
           JE L3
           inc v2
           LOOP L2

           pop eax
           mov edx, 0
           ret

      L3:
           pop eax
           mov edx, 1
           ret  <----------- ERROR OCCURS HERE.

 isPrime endp

 end main

好的,我解决了问题,现在它正在运行 代码在下面

 INCLUDE Irvine32.inc

 .data
      v1 dword 2
      v2 dword 2
      s1 byte "Please enter a number:",0
      s3 byte "|       The value will be recognized as non signed      |",0Ah, 0
      s4 byte "|By definition of Prime Number, number can't be negative|",0Ah, 0
      s5 byte "No Prime found.", 0Ah, 0
      s6 byte "Primes found until the given number:", 0Ah, 0

 .code

 main proc

           mov edx, offset s4
           call writestring
           mov edx, offset s3
           call writestring
           mov edx, offset s1
           call writestring
           call readdec
           ;if(eax < 2)
           cmp eax, 2
           JB L6
           mov ecx, eax
           dec ecx
           dec ecx
           ;if(ecx < 1)
           cmp ecx, 1
           JB L7
      L1:
           push ecx
           mov eax, v1
           push eax
           xor edx, edx
           div v2
           mov ecx, eax
           pop eax
           mov ebx, v2
           call isPrime
           pop ecx
           inc v1
           ;if (edx == 0)
           cmp edx, 0
           JE L4
           ;if (eax == 2)
           cmp eax, 2
           JE L5
           LOOP L1

           exit

      L4:
           call writedec
           mov al, ' '
           call writechar
           dec ecx
           ;if(ecx > 0)
           cmp ecx, 0
           JA L1
           exit
      L5:
           mov edx, offset s6
           call writestring
           call writedec
           mov al, ' '
           call writechar
           dec ecx
           ;if(ecx > 0)
           cmp ecx, 0
           JA L1
           exit

      L6:
           mov edx, offset s5
           call writestring
           exit
      L7:
           mov edx, offset s6
           call writestring
           call writedec
           mov al, ' '
           call writechar
           exit

 main endp

 COMMENT!
 isPrime recieves eax and ebx. And, it returns edx as boolean, eax as number.
 !
 isPrime proc
      L2:
           push eax
           xor edx, edx
           div ebx
           ;if(edx == 0)
           cmp edx, 0
           JE L3
           inc ebx
           pop eax
           LOOP L2

           xor edx, edx
           ret

      L3:
           pop eax
           mov edx, 1
           ret 

 isPrime endp

 end main

0 个答案:

没有答案