Skip to content

Instantly share code, notes, and snippets.

@skalnik
Created February 12, 2009 15:31
Show Gist options
  • Save skalnik/62679 to your computer and use it in GitHub Desktop.
Save skalnik/62679 to your computer and use it in GitHub Desktop.
.orig x3000
and R0, R0, #0 ; Counter
ld R7, NMAX ; Negative size of array. This way it can be added to the counter to check if we're done.
lea R6, PRIMES ; Put memory location of array into R6
and R4, R5, #0 ; Always zero.
INIT_LOOP:
add R3, R0, R6 ; Location in the array
str R0, R3, #0 ; Set mem[R3] to counter
add R0, R0, #1 ; Increment counter
add R5, R0, R7 ; Subtracts MAX from counter the negative amount remaining.
brn INIT_LOOP ; Go back to INIT_LOOP
;Array is now initialized so that the value is equal to the index.
;Now PRIMES[1] needs to be set to 0, since PRIMES[0] already is 0.
and R0, R0, #0 ; Reset R0/Counter
add R0, R0, #1 ; Add 1 to R0, so that R0 = 1
add R3, R0, R6 ; Get memory address of PRIMES[1]
str R4, R3, #0 ; Set PRIMES[1] to 0
and R0, R0, #0 ; Reset counter
add R0, R0, #-1 ; Subtract 1 from counter to allow for initial case
OUTER_LOOP:
INNER_LOOP_1:
add R0, R0, #1 ; Add one to counter
add R3, R0, R6 ; R3 <- memory address of PRIMES[0]
ldr R5, R3, #0 ; R5 <- PRIMES[counter]
brz INNER_LOOP_1 ; If the value at PRIMES[counter] is zero, go back to the first inner loop
and R1, R1, #0 ; Reset R1/Inner loop counter
add R1, R1, R0 ; R1 <- R1+R0
INNER_LOOP_2:
add R1, R1, R0 ; R1 += R0
add R3, R1, R6 ; Get memory address of current position in array
str R4, R3, #0 ; Set the value in the array to 0.
add R5, R1, R7
brn INNER_LOOP_2
add R5, R0, R7
brn OUTER_LOOP
HALT
NMAX .FILL #-3001
PRIMES .BLKW 3001
.END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment