Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Last active March 7, 2023 19:09
Show Gist options
  • Save tallpeak/471bb8cb1a9d428a440d75d01edc61f0 to your computer and use it in GitHub Desktop.
Save tallpeak/471bb8cb1a9d428a440d75d01edc61f0 to your computer and use it in GitHub Desktop.
After slight help from chatGPT, it took me some time to get this working, and using VirtualAlloc rather than a static buffer
format PE64 CONSOLE
entry main
include 'C:\util\fasmw17330\INCLUDE\WIN64a.INC'
include 'C:\util\fasmw17330\INCLUDE\API\KERNEL32.INC'
;section '.data' data readable writeable
; primes dq 2
;sieve db 1000000 dup 1
define sieve r13
; sieve_end equ $
sieve_len equ 10000 ; $-sieve
section '.text' code readable executable
; global main
; extern printf
main:
; set up stack frame
push rbp
mov rbp, rsp
mov r8,sieve_len+64
xor rax,rax
invoke VirtualAlloc,0,r8,MEM_COMMIT+MEM_RESERVE,PAGE_READWRITE
test rax,rax
jnz alloc_good
lea rcx,[error_alloc]
call [printf]
jmp end_program
alloc_good:
mov r13,rax
; initialize the array
mov rdi,r13
mov rax, 0101010101010101h ; 1=prime
mov rcx,sieve_len / 8
cld
rep stosq ; mark everything prime
; sieve algorithm
mov rcx, 3 ; start at 3
sieve_loop:
cmp rcx, sieve_len
jge print_primes
cmp byte[sieve + rcx],0
je sieve_skip ; already cleared (not prime)
mov rbx, rcx
add rbx, rcx
sieve_mark:
cmp rbx, sieve_len
jge sieve_skip
mov byte [sieve + rbx], 0 ; not prime
add rbx, rcx
jmp sieve_mark
sieve_skip:
add rcx, 2
jmp sieve_loop
; print out primes
print_primes:
lea rcx,[primes_msg]
call [printf]
mov r12, 1
print_loop:
cmp byte [sieve + r12], 0
je print_skip
push rax ; align stack before call
lea rcx, [prime_format]
mov rdx, r12
;mov rdi, qword [primes + rcx*8 - 8]
xor rax, rax
call [printf]
add rsp, 8 ; restore stack pointer after call
print_skip:
add r12,2
cmp r12, sieve_len
jl print_loop
invoke VirtualFree,r13,0,MEM_RELEASE
; clean up stack frame
end_program:
mov rsp, rbp
pop rbp
invoke ExitProcess,0
section '.rdata' data readable
prime_format db "%ld ", 0
error_alloc db 'error in virtual alloc',0
primes_msg db 'primes:',0
section '.idata' import data readable
library msvcrt, 'msvcrt.dll',\
kernel32, 'kernel32.dll'
import msvcrt, \
printf, 'printf',\
getchar, 'getchar',\
putchar, 'putchar',\
setmode, '_setmode'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment