我想创建一个程序,生成4个随机数,将它们插入到数组中,然后将它们打印出来,但问题是它总是插入相同的数字,因为我生成的数字与时钟,它的速度太快,这是我的代码:如何使装配延迟
IDEAL
MODEL small
STACK 100h
DATASEG
Clock equ es:6Ch
EndMessage db 'Done',13,10,'$'
divisorTable db 10,1,0
randoms db 4 dup(11)
CODESEG
proc printNumber
push ax
push bx
push dx
mov bx,offset divisorTable
nextDigit:
xor ah,ah ; dx:ax = number
div [byte ptr bx] ; al = quotient, ah = remainder
add al,'0'
call printCharacter ; Display the quotient
mov al,ah ; ah = remainder
add bx,1 ; bx = address of next divisor
cmp [byte ptr bx],0 ; Have all divisors been done?
jne nextDigit
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
pop dx
pop bx
pop ax
ret
endp printNumber
proc printCharacter
push ax
push dx
mov ah,2
mov dl, al
int 21h
pop dx
pop ax
ret
endp printCharacter
start:
mov ax, @data
mov ds, ax
; initialize
mov ax, 40h
mov es, ax
mov cx, 4
mov bx, offset randoms
RandLoop:
; generate random number, cx number of times
mov ax, [Clock] ; read timer counter
mov ah, [byte cs:bx] ; read one byte from memory
xor al, ah ; xor memory and counter
and al, 00001111b ; leave result between 0-15
mov [bx],al ;move the random number to the array
inc bx
loop RandLoop
; print exit message
mov cx, 4
mov bx, offset randoms
PrintOptions:
mov dl,[bx]
call printNumber
inc bx
loop PrintOptions
mov dx, offset EndMessage
mov ah, 9h
int 21h
exit:
mov ax, 4c00h
int 21h
END start
我觉得现在的问题是在这里:
RandLoop:
; generate random number, cx number of times
mov ax, [Clock] ; read timer counter
mov ah, [byte cs:bx] ; read one byte from memory
xor al, ah ; xor memory and counter
and al, 00001111b ; leave result between 0-15
mov [bx],al ;move the random number to the array
inc bx
;call printNumber
loop RandLoop
这是一个X-Y问题。你的问题是你没有得到好随机生成的数字,所以你认为解决方案是插入一个延迟。不是这样。解决方案是使用更好的随机数发生器。更重要的是,只用“一次*”的时间“种下”RNG,而不是每次循环。 –
@CodyGray你能给我一些代码吗? –
http://stackoverflow.com/questions/35583343/generating-random-numbers-in-assembly – vitsoft