Message board for the users of flat assembler.
> OS Construction > Pseudorandom Number Generation Code On 8086
Dark Dude 24 May 2008, 00:43
How on Earth does one go about programming a random number generator on the 8086?
I've tried to disassemble the Microsoft's C Runtime Library to see how they've gone about it, but my disassembler crashes when trying to follow the procedure tree, probably because of various anti-disassembler techniques MS have acquired...
But I just cant see how one goes about it...
I've read online that apparently, you use the time as a seed, but I dont pretend to be a professional at pseudorandom processes, so I dont understand how this mechanism achieves randomness all the time... o_O
Also, I dont understand how you use time on a computer, I'm guessing through some BIOS interrupts? So I guess supplying information there might help also <.<
Currently use FASM along with Emu8086, if that's needed to know...
|24 May 2008, 00:43||
asmdemon 24 May 2008, 01:23
http://support.microsoft.com/kb/28150 this is the official article from microsoft on the formula they used in quick basic. It's a simple linear-congruential type of RNG.
x1 = ( x0 * a + c ) MOD 2^16
example: mov ax, [seed] mul ax, [a_value] adc ax, [carry] mov [carry], dx mov [seed], ax ax = output rnd number... optional loop to repeat process
"MOD" part not needed because ax is 16 bit so output will be 0 to (2^16)-1
the trick however is choosing the a_value and seed value to produce apperant random numbers.
http://en.wikipedia.org/wiki/Linear_congruential_generator this list's microsoft's a_value and carry starting values.
|24 May 2008, 01:23||
Dark Dude 24 May 2008, 03:17
Snap, ok, that's great, and it's pretty much entirely answered my main point...
However, I see most main algorithms using the timestamp as the seed... How do you get this timestamp in 8086? I've looked everywhere, and all I'm told is to use the MS-DOS Get System Timer, but I dont wish to use this, as I wont have access to MS-DOS on this system...
|24 May 2008, 03:17||
f0dder 24 May 2008, 04:12
1) Microsoft don't really apply anti-disasm except to the core that handles validation.
2) you DO NOT use time as a seed, unless you want to be prisonbait. Using time as seed opens you to SO many attacks... iirc, asprotect was broken because of something similar to srand(time(0)).
3) use proper crypto, and forget 16bit unless you HAVE to for a course or whatever.
|24 May 2008, 04:12||
roboman 25 May 2008, 17:03
is a list of BIOS interrupts for time functions.
|25 May 2008, 17:03||
DOS386 01 Jun 2008, 22:22
> but my disassembler crashes when trying to follow the procedure tree
> I see most main algorithms using the timestamp as the seed...
> How do you get this timestamp in 8086?
8086 regrettably doesn't have RDTSC but nevertheless the "Intel" guys did have space for POPE CS , AAM , DAS , MOV AX,AX , PUSH SP, ... Simply: no way. You need external hardware for this.
> I've looked everywhere, and all I'm told is to use
> the MS-DOG Get System Timer, but I dont wish to use this,
> as I wont have access to MS-DOG on this system...
What OS do you have ?
2) you DO NOT use time as a seed, unless you want to be prisonbait. Using time as seed opens you to SO many attacks... iirc, asprotect was broken because of something similar to srand(time(0)). 3) use proper crypto
Nice, but topic starter didn't request high-end crypto security
> and forget 16bit unless you HAVE to for a course or what
I can confirm that the MUL algo works. You perfectly can use Mersenne Twister or whatever on 8086 also, just registers are 16-bit in size only and there is the 64 KiB limit. See also: http://board.flatassembler.net/topic.php?t=8690
|01 Jun 2008, 22:22||
< Last Thread | Next Thread >
Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.