I am using assembly 8086emu and I need a numbers generator for 8 numbers.
I tried to use this piece of code by @johnfound:
RANDGEN: ; generate a rand no using the system time
RANDSTART:
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
RET
but it is useful only when you generate one number. Repeated calls get the same number, because that clock only ticks 18.2 times per second.
I've tried to create pseudo-random function but I am pretty new to assembly and I did not succeed.
I would like to know if there's a way to do something similar to java's Math.random()
function in emu8086.
rdrand ax
is a valid instruction. Besides the classic LCG mentioned in answers and the linked duplicate, there are things like xorshift+ en.wikipedia.org/wiki/Xorshift or en.wikipedia.org/wiki/Linear-feedback_shift_register. A cut-down implementation of either of those using uint16_t could be reasonable. For actual 8086, keep in mind thatvar << 15
can be done with aror ax, 1
/and ax, 8000h
which could be faster without a barrel shifter. – Kurdish