Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Write an Assembly code that checks if a number is prime or not (If yes, output: true, if…

  1. User

    Write an Assembly code that checks if a number is prime or not (If yes, output: true, if no, output: false)

  2. Assistant

    Here is an x86 assembly code using NASM syntax that checks if a given number is prime or not. The program outputs "true" if the number is prime and "false" if it is not.

    section .data
        number     dd 29           ; The number to check
        true_msg   db "true", 10   ; Message to display if the number is prime (with newline)
        true_len   equ $ - true_msg
        false_msg  db "false", 10  ; Message to display if the number is not prime (with newline)
        false_len  equ $ - false_msg
    
    section .text
        global _start
    
    _start:
        ; Load the number to check into eax
        mov eax, [number]
    
        ; Handle numbers less than 2 (not prime)
        cmp eax, 2
        jl not_prime       ; If number < 2, it's not prime
        je is_prime        ; If number == 2, it's prime
    
        ; Initialize divisor (ecx) to 2
        mov ecx, 2
    
    check_loop:
        ; Compute ecx * ecx and compare with the number
        mov ebx, ecx
        imul ebx, ebx      ; ebx = ecx * ecx
        cmp ebx, eax
        jg is_prime        ; If ecx^2 > number, number is prime
    
        ; Divide the number by the current divisor
        mov edx, 0         ; Clear edx before division
        mov ebx, ecx       ; Divisor
        div ebx            ; edx:eax / ebx; quotient in eax, remainder in edx
    
        ; Check if remainder is zero
        cmp edx, 0
        je not_prime       ; If remainder is zero, number is not prime
    
        ; Increment divisor and repeat the loop
        inc ecx
        jmp check_loop
    
    is_prime:
        ; Output "true"
        mov eax, 4         ; sys_write
        mov ebx, 1         ; File descriptor (stdout)
        mov ecx, true_msg  ; Message to write
        mov edx, true_len  ; Message length
        int 0x80           ; Make system call
        jmp program_exit
    
    not_prime:
        ; Output "false"
        mov eax, 4         ; sys_write
        mov ebx, 1         ; File descriptor (stdout)
        mov ecx, false_msg ; Message to write
        mov edx, false_len ; Message length
        int 0x80           ; Make system call
    
    program_exit:
        ; Exit the program
        mov eax, 1         ; sys_exit
        xor ebx, ebx       ; Exit status 0
        int 0x80           ; Make system call
    

    Explanation:

    • Data Section (.data):

      • number: The number to check for primality.
      • true_msg and false_msg: Messages to output.
      • true_len and false_len: Lengths of the messages.
    • Text Section (.text):

      • The program starts at the label _start.
      • It first checks if the number is less than 2. Numbers less than 2 are not prime.
      • If the number is 2, it's prime.
      • For numbers greater than 2, it uses a loop to check for factors:
        • It starts with a divisor i = 2.
        • Computes i * i and compares it with the number. If i * i exceeds the number, the number is prime.
        • Divides the number by i to check for a zero remainder.
        • If the remainder is zero, the number is not prime.
        • Increments i and repeats the loop.
      • Uses Linux system calls (int 0x80) to output messages and exit.

    How to Assemble and Run the Code:

    1. Save the code in a file, e.g., prime_checker.asm.

    2. Assemble the code using NASM:

      nasm -f elf32 prime_checker.asm -o prime_checker.o
      
    3. Link the object file to create an executable:

      ld -m elf_i386 prime_checker.o -o prime_checker
      
    4. Run the executable:

      ./prime_checker
      

    Note: This code is intended for Linux systems using NASM assembler and the ELF32 format. It uses 32-bit assembly instructions and system calls, so ensure that your system supports 32-bit execution (you may need to install 32-bit libraries or run on a 32-bit environment).

    You can change the value of the number variable in the data section to check different numbers.