0

No matter what I enter, if I get a prime number or a composite number, it's always not prime number. I've checked the code many times. Anyone know maybe what's going on?I am a beginner in assembler


; Sprawdzanie czy liczba jest pierwsza

section .text
global _start

_start:

    ; Wyświetlenie pytania w terminalu
    mov rax, 4
    mov rbx, 1
    mov rcx, hello
    mov rdx, helloLength
    int 80h

    ; Wczytywanie danych z klawiatury do zmiennej number
    mov rax, 3
    mov rbx, 0
    mov rcx, number
    mov rdx, numberLength
    int 80h

    mov rcx, 2
    mov rax, number
    mov rsi, number

    loop:
        xor rdx, rdx
        mov rax, rsi
        cmp rcx, rsi
        jae prime

        div rcx

        or rdx, rdx
        jz notPrime

        inc rcx

        jmp loop

    notPrime:
        mov rax, 1
        mov rbx, 1
        mov rsi, no
        mov rdx, noLength
        syscall

        mov rax, 60
        syscall

    prime:
        mov rax, 1
        mov rbx, 1
        mov rsi, yes
        mov rdx, yesLength
        syscall

        mov rax, 60
        syscall

section .data

    hello db "Check number: "
    helloLength equ $ - hello

    no db "It's not prime number"
    noLength equ $ - no

    yes db "It's prime number"
    yesLength equ $ - yes

    number db 1
    numberLength equ $ - number
fuz
  • 88,405
  • 25
  • 200
  • 352
lu7x00
  • 1
  • 1
    `mov rsi, number` <-- What you'll have in `rsi` is the address of `number`. Comparing that address against 2,3,4... doesn't seem useful. As a side note, mixing `int 80h` and `syscall` just looks strange. Pick one or the other (preferably the other since this is 64-bit code). – Michael Mar 14 '20 at 15:12
  • related: [Checking if a number is prime in NASM Win64 Assembly](https://codereview.stackexchange.com/a/204965) has a working trial-division prime checking loop. But you also have a bug reading input: you only handle a single digit input, and you're checking the address for being prime. Use `movzx esi, byte [number]` to load the single digit, and see [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) – Peter Cordes Mar 14 '20 at 18:48

0 Answers0