SYS_GETRANDOM equ 318 SYS_TIME equ 201 segment .data LCG_a dq 1103515245 LCG_m dq 2147483648 LCG_c dq 12345 LCG_SEED dq 0 segment .bss RANDOM_SEED_TIME_TMP resq 2 RANDOM_SEED_RESULT_SIZE equ 8 RANDOM_SEED_RESULT resb RANDOM_SEED_RESULT_SIZE segment .text global GET_RANDOM global SEED_RANDOM global RANDOM_SEED ; rdi holds the seed SEED_RANDOM: mov [LCG_SEED], rdi ret ; rdi will hold the method ; 0 - rdtsc ; 1 - SYS_TIME ; 2 - SYS_GETRANDOM ; ; I need to use rax, rdi, rsi, rdx for system calls ; all are scratch so I am fine. ; I will use r8 for if statemetns, so I am fine there ; rdi holds the seed method RANDOM_SEED: mov r8, rdi test r8,r8 jnz RANDOM_SEED_STEP_2 ; r8 was 0 so user rdtsc rdtsc mov [LCG_SEED], rax jmp RANDOM_SEED_DONE RANDOM_SEED_STEP_2: dec r8 test r8,r8 jnz RANDOM_SEED_STEP_3 ; r8 was a 1 so use SYS_TIME mov rax, SYS_TIME lea rdi, RANDOM_SEED_TIME_TMP syscall mov rax, [RANDOM_SEED_TIME_TMP] mov [LCG_SEED], rax jmp RANDOM_SEED_DONE RANDOM_SEED_STEP_3: ; use GET_RANDOM mov rax, SYS_GETRANDOM mov rdi, RANDOM_SEED_RESULT mov rsi, RANDOM_SEED_RESULT_SIZE mov rdx, 0 syscall mov rax, [RANDOM_SEED_RESULT] mov [LCG_SEED], rax RANDOM_SEED_DONE: ; note RAX will hold the seed ret ; the result will be returned in rax GET_RANDOM: mov rax, [LCG_SEED] mov rdx, 0 mov rcx, [LCG_a] mul rcx ; rdx:rax = eax * rcx add rax, [LCG_c] mov rcx, [LCG_m] div rcx ; rdx will now hold the remainder/answer mov rax, rdx mov [LCG_SEED], rax ret