【发布时间】:2019-05-07 18:35:31
【问题描述】:
我编写了一个函数来确定一个值是素数还是非素数。但是当我从函数返回时,它会出现错误。
错误信息是
Project.exe 中 0x00000001 处的未处理异常:0xC0000005:访问冲突执行位置 0x00000001。
这个函数应该返回eax。
push ebp
mov ebp, esp
mov eax, [ebp+8] ; store input value
mov ecx, [ebp+8] ; store input value in counter
sub esp, 4 ; local variable
sub ecx, 1 ; avoid compare with itself
cmp eax, 3 ; compare with 1, 2, 3
jbe Prime
L1:
cmp ecx, 3 ; when count=3 to stop
je NotP
mov edx, 0 ; clear edx to save remainder
mov [esp-4], eax ; save input value
div ecx ; divide number
cmp edx, 0 ; check remainder
je NotP ; if remainder=0 then not prime
jmp Prime
loop L1
NotP:
mov eax, 0
push eax ; if delete this ilne still come up error
pop ebp
ret
Prime:
mov eax, 1
push eax ; if delete this ilne still come up error
pop ebp
ret
isPrime endp
【问题讨论】:
-
什么错误?将其包含在您的帖子中。编辑:函数 Epilog 之前的
push eax指令是可疑的。如果省略那行会发生什么? -
导致异常的参数有哪些?
-
我省略了那行仍然会出现同样的错误。
-
您没有正确清理堆栈(即您的 Epilog 代码错误)。除了去掉push eax,还需要restore esp,否则就是“返回”到本地变量中的值了。
标签: function assembly x86 primes masm