57 lines
1.3 KiB
NASM
57 lines
1.3 KiB
NASM
; This programs checks if a number n>1 is prime
|
|
; it checks for divisors from 2 to n-1, but does not terminate if n=1
|
|
section .data
|
|
is_prime db "prime"
|
|
is_len equ $ - is_prime
|
|
not_prime db "not prime"
|
|
not_len equ $ - not_prime
|
|
|
|
|
|
section .text
|
|
global _start
|
|
_start:
|
|
mov ebx, 37 ; hardcoded number to check
|
|
call check_prime
|
|
|
|
; n is loaded from ebx
|
|
check_prime:
|
|
mov ecx, 2 ; number to start at for primality check
|
|
loop:
|
|
cmp ecx, ebx
|
|
je end_is_prime ; if at the end of the loop, call end_is_prime
|
|
|
|
; operate division (bdx/ecx) <=> (num/current)
|
|
xor edx, edx ; will store remainder
|
|
mov eax, ebx
|
|
div ecx
|
|
|
|
cmp edx, 0 ; check if ecx divides ebx
|
|
je end_is_not_prime
|
|
inc ecx ; continue loop
|
|
jmp loop
|
|
|
|
|
|
; success
|
|
end_is_prime:
|
|
mov edx, is_len
|
|
mov ecx, is_prime
|
|
mov ebx, 1 ; stdout
|
|
mov eax, 4 ; sys_write
|
|
int 0x80
|
|
|
|
mov eax, 1 ; sys_exit
|
|
mov ebx, 0 ; exit code 0
|
|
int 0x80 ; syscall
|
|
|
|
; failure
|
|
end_is_not_prime:
|
|
mov edx, not_len
|
|
mov ecx, not_prime
|
|
mov ebx, 1 ; stdout
|
|
mov eax, 4 ; sys_write
|
|
int 0x80 ; syscall
|
|
|
|
mov ebx, 1 ; exit code 1
|
|
mov eax, 1 ; sys_exit
|
|
int 0x80 ; syscall
|