Hey guys,
I wrote a code to search for primenumbers in a given range.
My programm shall take an argument, which will be the last number checked.
To accelerate the code i wanna check divisibility through 2,3,5,7 before i increase the divisor.
If the divisor reaches the current number x/2 you can be sure that it is prime.
I am sorry bothering here for help, but i don't understand whats wrong with my code, because it gives my an errormessage while linking.
Also do i want to find large primenumbers > 1000000 etc....
Therefore i am not sure if i declared the size of "output" correctly.
; Compiled with
; nasm -f elf64 prime.asm && gcc -o prime prime.o
global _start
extern atoi
extern printf
extern puts
section .text
main:
cmp rdi, 2 ;check if it has one argument
jne error1
xor rdi, rdi
mov rdi, rsi; argument
call atoi wrt ..plt
mov r8, rdi
inc r8
xor rdi, rdi
mov r9, 2 ;number to begin
mov r11, 11
checkPrime2: ; check if divisible by 2
xor rdx, rdx
cmp r8, r9
je done
mov rax, r9
mov rcx, 2
div rcx
mov r10, rax
cmp rdx, 0
je notPrime
checkPrime3: ; check if divisible by 3
xor rdx, rdx
mov rax, r9
mov rcx, 3
div rcx
cmp rdx, 0
je notPrime
checkPrime5: ; check if divisibly by 5
xor rdx, rdx
mov rax, r9
mov rcx, 5
div rcx
cmp rdx, 0
je notPrime
checkPrime7: ; check if is divisible by 7
xor rdx, rdx
mov rax, r9
mov rcx, 7
div rcx
cmp rdx, 0
je notPrime
; increase divisor until it reaches half of the given number.
; r11 is the divisor which will be incremented
; in r10 is the actual number divided by two
divisorIncrease:
xor rcx, rcx
mov rax, r9
mov rcx, r11
div rcx
cmp rdx, 0
je notPrime
cmp r11, r10
je printPrime
inc r11
jmp divisorIncrease
printPrime:
mov rdi, output
mov rsi, r9
call printf wrt ..plt
inc r9
cmp r8, r9 ; r8 contains the argument (highest number) +1
je done
jmp checkPrime2
notPrime:
inc r9
cmp r8, r9
je done
jmp checkPrime2
error1:
mov rdi, noArgument
call puts wrt ..plt
done:
ret
section .data
output:
dq "%d", 10, 0
noArgument:
db "Requires exactly one argument (last number to check)", 10, 0
I try to compile and link this code to make an executable, but everytime i get this error:
nasm -f elf64 prime.asm && gcc -static -m64 prime.o -o prime
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/crt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Best Regards and very gracefully,
Renegade