The Stack
Notes
- When a function is called, it normally needs to manage the stack
- It creates something know as
- an activation record
- a stack frame
- These two are equivalent.
- The activation record contains
- Information needed to manage the stack (old base pointer)
- Local variables
- Values of preserved that will be used locally
- parameters to functions to be called
- the return address
- Let's write some code
- A Linear congruential generator is a way to generate pseudo random numbers
- Xn+1 = (a * Xn + c) mod m
- We need integer division
- (Jorgensen 7.5.4)
-
div divisor (the thing we are dividing by)
- rdx:rax contains the dividend (the thing we are dividing)
- after: rax contains the quotient, rdx contains the remainder
- Or adjust the registers as you see fit.
- Parameters
- X0 is the initial seed value, default 1
- glibc uses
- m = 231
- a = 1103515245
- c = 12345
- Implement RAND
- A global SEED
- Constants for M, A, and C
- No parameters
- Return the value in rax
- Test this with RAND_TEST
- Pass in n, the number of times to call the RNG
- Print out these values.
- Write RAND_IN_RANGE
- Pass in the size of the range in rdi
- Pass in the minimum value in rsi
- Return rand % range + min in rax
- It is probably not needed in this case, but to be safe
- In the preamble
- set up a stack frame
- save rsi and rdi
- In the epilogue
- restore the stack if necessary
- Restore the previous stack frame
- Write SIMPLE_RANGE_TEST
- Pass the number of times to test in rdi
- Pass the low value of the range to test in rsi
- Pass the high value of the range to test in rdx
- In the preamble
- Set up the stack frame
- Work in R12, r13, r14 so save these on the stack.
- treat n, the number of iterations as a local variable, so put this on the stack.
- Do the computation: print n random numbers in the range low to high
- Restore the stack
- remove rdi
- Restore r14, r13, r12
- Restore the old stack frame
- Write BALANCED_DIE_CHECK
- Pass in the number of sides: rdi
- Pass in the number of trials: rsi
- Set up space for the count array on the stack
- Roll the die n times, counting the number of times each side appears.
- Print the resulting table.
- Clean up in the epilogue