-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.asm
69 lines (52 loc) · 1.73 KB
/
random.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
section .bss
; Current random number and seed
rand: resb 8
section .text
global set_seed
global next_rand
; Generate the random number using the current Epoch time as the seed. Returns the seed used.
; This uses the current second though, not the millisecond, so it's not very good
; Prototype: long seed();
; Returns the seed used
%define CLK_M_R 4
set_seed:
push rbp
mov rbp, rsp
sub rsp, 32
mov [rbp - 8], rbx
mov rax, 228 ; System call clock_gettime
mov rdi, CLK_M_R
lea rsi, [rbp - 32]
syscall
mov rax, [rbp - 24] ; Get the nanoseconds
xor rdx, rdx ; Clear upper 64 bits
mov rbx, 21569 ; Divide by m=21569
div rbx
mov [rand], rdx ; Set the value at rand as the remainder
mov rbx, [rbp - 8]
mov rsp, rbp
pop rbp
ret
; Returns a new random number
; Uses a linear congruential generator formula with the following constants:
; a = 14359
; b = 9654
; m = 21569
; Prototype: long next_rand()
; Returns a random number
next_rand:
push rbp
mov rbp, rsp
push rbx
mov rax, [rand] ; Copy the current random number to rax
mov rbx, 14359 ; Multiply it by a=14359
mul rbx
add rax, 9654 ; Add b=9654
xor rdx, rdx
mov rbx, 21569 ; Divide it by m=21569
div rbx
mov [rand], rdx ; Set the remainder as the new rand
mov rax, rdx
pop rbx
pop rbp
ret