flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Pseudorandom Number Generation Code On 8086

Author
Thread Post new topic Reply to topic
Dark Dude



Joined: 21 Jul 2007
Posts: 6
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...

Thanks p_o
Post 24 May 2008, 00:43
View user's profile Send private message Reply with quote
asmdemon



Joined: 18 Jan 2004
Posts: 97
Location: Virginia Beach, VA
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.

Code:
x1 = ( x0 * a + c ) MOD 2^16    

Code:
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.
Post 24 May 2008, 01:23
View user's profile Send private message Visit poster's website Reply with quote
Dark Dude



Joined: 21 Jul 2007
Posts: 6
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...
Post 24 May 2008, 03:17
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
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.
Post 24 May 2008, 04:12
View user's profile Send private message Visit poster's website Reply with quote
roboman



Joined: 03 Dec 2006
Posts: 122
Location: USA
roboman 25 May 2008, 17:03
http://www.ctyme.com/intr/int-1a.htm
is a list of BIOS interrupts for time functions.
Post 25 May 2008, 17:03
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1898
DOS386 01 Jun 2008, 22:22
> but my disassembler crashes when trying to follow the procedure tree

Bad disassembler Sad

> 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 Sad but nevertheless the "Intel" guys did have space for POPE CS , AAM , DAS , MOV AX,AX , PUSH SP, ... Sad 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 ?

Quote:
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 Wink

> 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
Post 01 Jun 2008, 22:22
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.