So Im working on a battleship project in asm for uni and I want to generate some random numbers between 1-100 for the game in order to place the ships in the matrix. I was told that for the random number generator I could use the rdstc function but it gives me errorA2085: instruction or register not accepted in current CPU mode when I try to use it. What should I change to make it work or what other options I could use to generate numbers?
Asked
Active
Viewed 479 times
0
-
1`rdtsc` doesn't give random numbers, it gives deterministic timestamps. On reasonably modern x86 you might use `rdrand` or `rdseed`. – EOF May 24 '21 at 10:21
-
I can't use any of those, Im on a 386. – Octavian Alixandrescu May 24 '21 at 10:46
-
1For a literal "80386 PC" there's almost nothing useful you can use to obtain enough randomness. You'd have to build up entropy from multiple sources (current "time of day" and date, PIT timer count, subtle differences in when the user presses keys) and use the "very little entropy" to improve a pseudo-random number generator. – Brendan May 24 '21 at 11:58
-
4Since you don't need the numbers for cryptography and just For a simple game like that you could use RDTSC to seed a pseudo random number generator (PRNG) which can be simple to complex (LCG, Mersenne, etc). Your specific error suggests you haven't told MASM you have a late enough processor. If on a pre-Pentium x86 you could use a (timer ticks) clock source as a seed to a PRNG. If you are using Pentium or later add `.586` to top of your ASM code to enable Pentium features at assemble time. – Michael Petch May 24 '21 at 12:28
-
2Which operating system? – Margaret Bloom May 24 '21 at 14:00
-
I used rdtsc to generate numbers with the .586 added to the code for it to work. Thanks for the answers. – Octavian Alixandrescu May 24 '21 at 18:51
-
If you're running native code in 32-bit mode, you can use `rdrand` if your CPU has it (IvyBridge for Intel, I forget when for AMD). You might need to encode it manually with `db` if your assembler is too old to know about it (like in [How to add RDRAND instruction into 64-bit code compiled with VS 2008?](https://stackoverflow.com/q/38044484) shows `db 0fh, 0c7h, 0f2h` for `rdrand edx`). If you're running 16-bit code in DOSBox, IDK whether DOSBox emulates / passes through `rdrand`. – Peter Cordes May 24 '21 at 19:55