$\require{cancel}$
A Random number library and tester.
- I use random numbers in a surprising number of exercises.
- So I want a random number generator.
- But there is not one in assembly.
- So we need to make our own.
- A very old and simple method is the Linear congruential generator
- Store a seed
- Each time rand is called
- seed = (a*seed + c) mod m
- m = 231, a = 1103515245, c = 12345 (glibc)
- This is not good for anything but playing.
- One problem is that this needs a random seed.
- We have used
srand(time(null))
- This is a system call (201)
- Here is a better reference .
- System calls are discussed in chapter 13 of Jorgensen.
- we have covered most of this.
- We will do files soon and get the rest.
- But there is some good example code here if you want it.
- By the way, time takes a single argument, the address to store the return
- This is should be stored in rdi.
- A second option is a the
rdtsc
instruction
- a reference.
- The intel chip has an internal counter.
- This is apparently impacted by all cores.
- The call returns a unsigned 64 bit "count" of actions.
- A third way is to use the os' internal entropy buffer.
- getrandom, a system call (318)
- rdi contains the address to write results.
- rsi contains the number of bytes to get.
- You can look at all three in action in myrandom.asm.
- I have implemented these, as well as the rng routine in lcg.asm.
- I have modified MY_IO library
- PRINT_STRING:
- rdi string address
- rsi string length
- PRINT_NEWLINE
- PRINT_INT now no longer prints the newline
- And I have a reasonable example in randTest.asm