1

I am trying to write a procedure that generates random numbers in assembly. I found the next code:

   MOV  AH, 00h  ; interrupts to get system time                                                                   
   INT  1AH      ; CX:DX now hold number of clock ticks since midnight                                             
   mov  ax, dx                                                                                                    
   xor  dx, dx                                                                                                    
   mov  cx, 10                                                                                                    
   div  cx       ; here dx contains the remainder of the division - from 0 to 9                                   
   add  dl, '0'  ; to ascii from '0' to '9'
   mov  ah, 2h   ; call interrupt to display a value in DL             
   int  21h

But when I try to generate more than one the number doesn't change. What to do?

ecm
  • 2,583
  • 4
  • 21
  • 29
tal
  • 19
  • 2
  • That 'tick' increments at only ~ 18.25 Hz. You can use it is 'seed' a PRNG which you can implement fairly simply in the code. – Weather Vane Mar 20 '22 at 15:04

1 Answers1

3

Is it possible that the number doesn't change because the system time stays the same between the two executions?

Random numbers based on the system time only work well when the timer is very precise and when the numbers are generated in response to external events.

If you want something that will give you a new number every time you run it, you need an actual RNG algorithm. You can find a list on Wikipedia.

Leo
  • 1,273
  • 9
  • 14