Wednesday, September 06, 2006

Random number generator in 16-bit DOS assembly

Most code for randomizing is not as short as this one. Try to incorporate this procedure in your code and see how it works!



randomize:
in al, 40h ; read micro-clock for initial seed
mov ah, al
in al, 40h
xchg al, ah
or ax, 1
mov rnum, ax
ret

Random number will be stored in rnum. Fairly straightforward isn’t it?


[Edit]


Here’s another solution by sir Eugene Kanindot. This is a 3-digit random number generator.



jmp start

xxx:
mov ah, 02ch
int 21h
and dl, 0fh
cmp dl, 9
ja xxx
add dl, 30h
mov [di], dl
ret

delay:
mov cx, 0

yyy:
mov dx, 0a00h

zzz:
xor ax, ax
dec dx
cmp dx, 0
jne zzz

loop yyy
ret

start:
lea di, numb
call xxx
inc di
call delay
call xxx
inc di
call delay
call xxx
mov ah, 09h
lea dx, numb
int 21h
int 20h

numb db 4 dup('$')

No comments: