Question

I am implementing in nasm program that can detect prime number that is provided.

    xor rax,rax;
    mov eax, 6 ;
    mov ecx, 1      ; 
loop:
    cmp     ecx, 6      
    jbe     end_loop    ;  ecx >= bufor

    xor edx ,edx ; 
    mov eax, 6 
    div ecx         ;  div dla edi

    cmp edx, 0
    je not_prime

    inc     ecx             ; i++
    jmp     loop_for

This is simple algorithm that checks weather 6 is prime number but it fails. It check div reminder in edx register. It always exit on end_loop(but it should end in not_prime) directive so program said that 6 is a prime number but it isn't. Can someone advice me how I can fix it? I have no clue.

Was it helpful?

Solution

This code is incorrect:

  mov ecx, 1      ; 
loop:
  cmp     ecx, 6      
  jbe     end_loop    ;  ecx >= bufor

jbe means jump if below or equal. So what you're doing here is essentially:

ecx = 1;
do {
    if (ecx <= 6) goto end_loop;

Obviously 1 is <= 6, so it will always jump to end_loop on the first iteration of the loop.
That jump should probably be a jae instead.


Another problem is that you start with ecx = 1, since ecx appears to be your divisor. The first number you should try to divide by if you're checking for primality is 2, not 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top