MASM 打印素数

问题描述 投票:0回答:0

嗨,我的 Assembly 项目有问题。我应该按顺序打印出 x 个素数。但是我不能使用功能 USES。当我试图删除它时,出现了无限循环。有人可以帮帮我吗,谢谢

isPrime PROC USES ECX EBX
; Checks whether a number is prime or not. If given number is prime, returns
; boolean value 1. Otherwise, returns 0.
; receives: A number in EAX
; returns: boolean value(0 or 1) in EAX
; preconditions: eax is in [1..200]
; registers changed: eax, ebx, ecx, edx
;-------------------------------------------------------------------------
        mov ecx,eax                         ; copy the number p into ECX
        mov esi, 2                          ; i=2
  startLoop:
        cmp esi,ecx                         
        jge PrimeNum                        ; if i>=p, p is prime and goto PrimeNum
        mov edx,0
        mov eax,ecx
        div esi                             ; calculate p/i. EAX=p/i and EDX=p%i
        cmp edx, 0                          
        je  NotPrime                        ; if remainder = 0, p is not prime
        inc esi                             ; otherwise, continue search
        jmp startLoop
PrimeNum:   
    mov eax, TRUE                           ; if p is prime , return true
    ret
NotPrime:
    mov eax, FALSE                          ; if p is not prime, return true
    ret
isPrime ENDP

当我删除 USES 部分时

mov ecx,eax                         ; copy the number p into ECX
        mov try2, 2                         ; i=2
  startLoop:
        cmp try2,ecx                            
        jge PrimeNum                        ; if i>=p, p is prime and goto PrimeNum
        mov edx,0
        mov eax,ecx
        div try2                                ; calculate p/i. EAX=p/i and EDX=p%i
        cmp edx, 0                          
        je  NotPrime                        ; if remainder = 0, p is not prime
        inc try2                                ; otherwise, continue search
        jmp startLoop
PrimeNum:   
    mov eax, TRUE                           ; if p is prime , return true
    
    
    ret
NotPrime:
    mov eax, FALSE                          ; if p is not prime, return true
    ret
assembly masm
© www.soinside.com 2019 - 2024. All rights reserved.