我已经将一个程序从1增加到检查100以下的每个数字,如果是素数,这个数字将被存储到一个数组中。然而,我遇到的问题是,没有任何东西被存储到数组中,并且寄存器没有按预期的方式使用。在它自己的情况下,'_isPrime‘过程完全按照预期工作,但是当我将它与代码放在一起时,然后调用它,这似乎是错误的来源。我的循环主要实现正确吗?在手术过程中还有什么我应该做的吗?
.DATA
arr DWORD 27 DUP (?)
.CODE
_MainProc PROC
mov eax, 1 ; number in eax will be prime checked / edx will be 1 if true
J1:
mov edx, 0 ; clear edx
inc eax ; increment eax to begin prime checking
cmp eax, 100 ; if eax reaches 100 loop ends
je J2
push eax
push edx
call _isPrime ; prime check on eax
cmp edx, 0 ; if not prime begin loop again
je J1
mov [arr], eax ; if prime eax is stored into array
jmp J1 ; loop begins again
J2:
mov eax, 0
ret
_MainProc ENDP
_isPrime PROC
push ebp
mov ebp, esp
push ecx
push ebx
cmp eax,1
jle L2
mov ecx,eax
shr ecx,1
mov ebx,2
mov edx,1
L1:
cmp ebx,ecx
jg L2
push eax
mov edx,0
div ebx
cmp edx,0
je L2
inc ebx
jmp L1
mov edx,1
L2:
pop ebx
pop ecx
pop ebp
ret
_isPrime ENDP
END 发布于 2020-11-21 21:36:55
push eax push edx call \_isPrime ; prime check on eax
您根本不需要提供这个EDX参数。_isPrime过程不使用它作为参数。
接下来,您需要平衡堆栈。您可以选择使用ret 4从proc返回,以弹出惟一的参数EAX,至少该proc使用类似于mov eax, [ebp + 8]的指令来获取它.
mov [arr], eax ; if prime eax is stored into array jmp J1 ; loop begins again
你要把每一个质数都储存在另一个上面!
mov edi, offset arr
...
mov [edi], eax
add edi, 4
jmp J1这个能行的。
push eax mov edx,0 div ebx cmp edx,0 je L2
在_isPrime中,您无法保留EAX中的值,因为缺少pop eax!
push eax ; (1)
xor edx, edx
div ebx
pop eax ; (1)
test edx, edx
jz L2为了进行正确的操作,mov edx, 1指令需要在循环中执行,而不是在循环之前执行:
L1: mov edx,1
cmp ebx,ecx
jg L2https://stackoverflow.com/questions/64948156
复制相似问题