1

I want to know if there a routine or an instruction to generate a random number using assembly on 8086. any help will be appreciated.

Mina Fouad
  • 79
  • 1
  • 4
  • 11
  • See also https://www.felixcloutier.com/x86/rdrand for modern x86 CPUs where `rdrand ax` is a valid instruction. Besides the classic LCG mentioned in answers and the linked duplicate, there are things like xorshift+ https://en.wikipedia.org/wiki/Xorshift or https://en.wikipedia.org/wiki/Linear-feedback_shift_register – Peter Cordes May 08 '22 at 06:47

3 Answers3

2

The most common way is to use the timestamp. In 32 bit mode it can be done by rdtsc instruction, in 16 bit mode: by using function 0 of BIOS interrupt 1A.

Because it's a timestamp, avoid using it frequently (because of lack of proper dispersion), and use it as seed for an pseudo-random number generator. When you need just one random value, you can use the timestamp directly.

Usually a simple pseudo-random number generator is enough:

static int seed = now();
seed = (seed * LARGE_PRIME1) % LARGE_PRIME2;

And there is also wiki

ruslik
  • 14,714
  • 1
  • 39
  • 40
1

There is a good answer to this exact question on Yahoo! Answers:

I suspect the point here is to learn to write assembly language. Here's the instruction set (http://www.emu8086.com/assembler_tutorial/8086_instruction_set.html) You have all the instructions you need to perform the equation you need to generate pseudo-random numbers except the 'mod' instruction, which you'll have to write a subroutine for. Pay particular attention to the limitations on the MUL and DIV instructions. "store in a separate file" isn't implicit in the instruction set. You'll have to better understand what's wanted here. "File" and "Print" are concepts related to operating system; they're forms of output in this case.

from http://answers.yahoo.com/question/index?qid=20081030112909AAmjEsp

Community
  • 1
  • 1
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
0

You might be interested in Agner Fog's pseudo random number generators (some of which are written in assembly) here.

PhiS
  • 4,540
  • 25
  • 35