素数中的逻辑错误

Sea*_*ock 1 linux x86 assembly

我是汇编语言的新手.我编写了一个程序,用于获取输入,然后显示该数字是否为素数.

这是我的源代码.

.intel_syntax noprefix

.include "console.i"


.data

        Num:    .long 0

.text

        ask:    .asciz "Enter a +ve number : "
        ansp:   .asciz " is prime."
        ans:    .asciz " is not prime."

_entry:

        Prompt ask
        GetInt Num

        mov eax,Num # store Number in eax
        #mov ecx,0   # Reset ecx to 0
        mov ecx,0    # Reset ecx t0 2 for dividing.
        cdq

1:      inc ecx       # increment ecx
        mov ebx,eax   #backup eax
        Div ecx       #Divide eax by ecx

        cmp edx,0     #if remainder is zero num is not prime
        je 2f
        mov edx,0     #reset edx to 0
        mov eax,ebx   #reset eax to Num

        cmp eax,ecx   if ecx is less than number.
        jl 1b


        #Prime
        PutInt Num
        Prompt ansp
        jmp 3f

2:      #Not Prime
        PutInt Num
        Prompt ans

3:      PutEol
        ret

.global _entry

.end
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,它总是显示它不是素数.

例如,如果我输入7,则显示7不是素数.

我正在使用英特尔x86架构并在Ubuntu上开发它.

编辑1:根据Darron的说法,我将ecx寄存器初始化为1,然后将ecx递增到1,以便从2开始循环.

但问题是,当我输入9时,它显示我9是素数.我不知道我的逻辑有什么问题.

编辑2:我将我的号码存储在eax中,然后我将其除以ecx,然后最后检查edx寄存器中的reaminder是否为零.

谢谢.

Dar*_*ron 8

首先除以1.即使素数也可以被1整除.