I am using a typical x86-16 assembly random number generator, using the BIOS timer and the current milliseconds.
random_number:
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
mov bl, dx
ret
I am using this in order to print strings in random rows on the screen in the function:
generate_enemies:
mov ah, 0x13
mov al, 0x01
mov bh, 0
mov bl, 0x07
mov cx, 1
mov bp, enemy_pixel
mov dh, bl
mov dl, 79
int 0x10
ret
The issue is in:
mov bl, dx
Because I need the number to get into a register that matches the size of DH, and of course, for BL it is not the case. Also it cannot be AL or AH as it would mess with the int 10h ah 13h
function. Just for you to know, I am using FASM as an assembler, and running it on qemu-system-x86_64.
EDIT
I just realized the issue relies on the fact that the DX register that stores the remainder of the division is 16-bit size, while intuitively the size of DH and DL are both 8-bit. So trying to use the value of the DX register as a row for the DH register is just simply impossible with this setup. So I need an alternative method to get a random number for the rows.
mov bl, dx
If random_number should return in the BL register, then move DL to BL.
generate_enemies: mov ah, 0x13 mov al, 0x01 mov bh, 0 mov bl, 0x07 <<<< you already loaded bl with 7 mov cx, 1 mov bp, enemy_pixel mov dh, bl <<<< bl does no longer hold your 'random' number mov dl, 79 int 0x10 ret
You should have written the mov dh, bl
instruction before issueing mov bl, 0x07
.
Your self-answer claims to have solved it by using another interrupt. That's misleading at best! Both the DOS function 2Ch and the BIOS function 1Ah/00h are based on the same timer that changes about 18 times per second.
What does make a difference is what part of the division's result you keep.
; OUT (bl)
random_number:
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
mov bl, dl
ret
; IN (bl)
generate_enemies:
mov dh, bl
mov dl, 79
mov ah, 0x13
mov al, 0x01
mov bh, 0
mov bl, 0x07
mov cx, 1
mov bp, enemy_pixel
int 0x10
ret