Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Created April 3, 2024 06:38
Show Gist options
  • Save donnaken15/aafe838dfef1b66a333cd36f4b7baf9b to your computer and use it in GitHub Desktop.
Save donnaken15/aafe838dfef1b66a333cd36f4b7baf9b to your computer and use it in GitHub Desktop.
FASM: further optimized prime number counter using array that expands with each prime number found, to not waste extra division ops (i.e. don't % 9, 15, 21, 25, 27, etc)
format PE64 console 3.1
entry @f
include 'win64a.inc'
MAX_ITERATIONS = 100000000
; 1kb EXE 1337
section '' import code data \
readable writeable executable
library msvcrt,'MSVCRT.DLL'
import msvcrt,\
printf,'printf'
align 10h
@@:
xor r13 , r13
inc r13
mov r14 , r13
add r14b, 2
@:
xor r12 , r12
@@:
xor rdx , rdx
mov rax , r14
mov r15 , [p + r12 * 8]
div r15
test rdx , rdx
jz @f
inc r12
cmp r12 , r13
jb @b
invoke printf, fmt, r14
mov [p + r13 * 8], r14
inc r13
@@:
add r14 , 2
cmp r14 , -3
jb @
ret
fmt db '%u',13,0
align 10h
p dq 3 ; array, start with 1 item
rq MAX_ITERATIONS
; linux / WSL
format ELF64 executable 3
MAX_ITERATIONS = 100000000
segment executable writeable readable
_start:
xor rcx , rcx
inc rcx
mov rbp , 3
@:
xor rbx , rbx
@@:
xor rdx , rdx
mov rax , rbp
mov rdi , [p + rbx * 8]
div rdi
test rdx , rdx
jz @f
inc rbx
cmp rbx , rcx
jb @b
mov rsi , rbp
mov [p + rcx * 8], rbp
inc rcx
@@:
add rbp , 2
cmp rbp , -3
jb @
ret
align 10h
p dq 3
rq MAX_ITERATIONS
@donnaken15
Copy link
Author

using gdb on linux version to see registers and control pausing and running of the program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment