I'm attempting to write a program that will find the nth prime - in this case the 10001st prime.
Currently, the program detects every number as a prime number, so my result ends up being 10002.
When stepping through the program in GDB, the values being retrieved from the array of primes to divide are not what is expected - i.e. after determining that 3 is prime, the next iteration for 4, the program retrieves 770 from the array.
The code is:
%include '../resources.asm'
SECTION .data
SECTION .text
global main
extern malloc, free, calloc
main:
; Allocate space for primes
mov rsi, 8
mov rdi, 10001
call calloc
; Store address in r10
mov r10, rax
; First prime is 2
mov qword [r10], 2
; r11 is current number of primes found
mov r11, 1
; r12 is current number being checked
mov r12, 2
.outer:
; Move to next number
inc r12
; Reset array index
mov rcx, 0
.inner:
; Get number and divisor in rax and rbx respectively
mov rax, r12
mov qword rbx, [r10 + rcx] ;;;; Issue here, rbx is 770?
; Increment array index
inc rcx
; Get modulus
call umod
; If modulus is 0, number is not prime, move to next number
cmp rax, 0
jz .outer
; See if we've hit the end of current list of primes
cmp rcx, r11
jnz .inner
; If we are at the end of the list of primes, move the current number into the array
mov qword [r10 + rcx], r12
inc r11
; Stop if we've got the 10001st prime
cmp r11, 10001
jl .outer
And the umod snippet from resources
;
; int udiv(int rax, int rbx) -> rax, rbx
; Unsigned division
udiv:
push rdx
xor rdx, rdx
div rbx
mov rbx, rdx
pop rdx
ret
;
; int umod(int rax, int rbx) -> rax
; Unsigned modulus operation
umod:
call udiv
mov rax, rbx
ret
Note, I realize I may be not following the correct conventions for parameter passing and such. Feel free to make notes if you think it would be helpful.