-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.s
55 lines (51 loc) · 1.61 KB
/
random.s
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
;Random number generator;
; Linear congruential generator
; from Numerical Recipes by Press et al.
; Jonathan Valvano
; How to use:
; 1) call Random_Init once with a seed
; Random_Init(1);
; Random_Init(NVIC_CURRENT_R);
; 2) call Random over and over to get a new random number
; n = Random(); // 32 bit number
; m = (Random()>>24)%60; // a number from 0 to 59
THUMB
AREA DATA, ALIGN=2
M SPACE 4
ALIGN
AREA |.text|, CODE, READONLY, ALIGN=2
EXPORT Random_Init
EXPORT Random
EXPORT Random32
Random_Init
LDR R2,=M ; R4 = &M, R4 points to M
MOV R0,#1 ; Initial seed
STR R0,[R2] ; M=1
BX LR
;------------Random32------------
; Return R0= random number
; Linear congruential generator
; from Numerical Recipes by Press et al.
Random32 LDR R2,=M ; R2 = &M, R4 points to M
LDR R0,[R2] ; R0=M
LDR R1,=1664525
MUL R0,R0,R1 ; R0 = 1664525*M
LDR R1,=1013904223
ADD R0,R1 ; 1664525*M+1013904223
STR R0,[R2] ; store M
BX LR
;------------Random------------
; Return R0= random number, 0 to 255
; Linear congruential generator
; from Numerical Recipes by Press et al.
Random LDR R2,=M ; R2 = &M, R4 points to M
LDR R0,[R2] ; R0=M
LDR R1,=1664525
MUL R0,R0,R1 ; R0 = 1664525*M
LDR R1,=1013904223
ADD R0,R1 ; 1664525*M+1013904223
STR R0,[R2] ; store M
LSR R0,R0,#24 ; top 8 bits of number
BX LR
ALIGN
END