在x86 8086 Assembly中生成0-9范围内的随机数 [英] Generating a random number within range of 0-9 in x86 8086 Assembly

查看:215
本文介绍了在x86 8086 Assembly中生成0-9范围内的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对8086汇编非常陌生,要掌握这些知识非常困难.尽管如此,我会尽力而为.

First of all, I am very new to 8086 Assembly and it has been pretty difficult for me the grab the knowledge. Nevertheless, I'll do my best.

我一直在尝试编写代码以生成0-9范围内的随机数.在研究了几个示例和建议之后,我最终得出了这一结论.为了简单起见,我没有对检索到的时钟计数应用任何数学函数,并且我认为这是不必要的.由于某种原因,我最终生成的数字比1,3和9之类的数字少6,7倍.我相信这是因为我采用的是时钟滴答的低位,因此数值会发生变化.迅速.

I have been trying to write a code to generate a random number within range of 0-9. After looking into several examples and suggestion, this is what I ended up with. I did not apply any mathematical function on the retrieved clock count, for simplicity and also I thought it was unnecessary. I ended up with, for some reasons, generating certain number like 6,7 fewer times than numbers such as 1,3 and 9. I believe it is because I'm taking the lower order of the clock ticks, where the values change so rapidly.

我的目的是模拟骰子掷骰子,后来将下面代码的范围更改为1-6. 我的问题是,这足以满足我的目的吗?还是有更好的方法可以做到这一点?

My purpose is to simulate a dice roll which later ill change the range of below codes to 1-6. My question is, Is this adequate enough for my purpose? or is there any better way to do this?

代码:

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      
                ; lets just take the lower bits of DL for a start..
   MOV BH, 57   ; set limit to 57 (ASCII for 9) 
   MOV AH, DL  
   CMP AH, BH   ; compare with value in  DL,      
   JA RANDSTART ; if more, regenerate. if not, continue... 

   MOV BH, 49   ; set limit to 48 (ASCII FOR 0)
   MOV AH, DL   
   CMP AH, BH   ; compare with value in DL
   JB RANDSTART ; if less, regenerate.   


   ; if not, this is what we need 
   mov ah, 2h   ; call interrupt to display a value in DL
   int 21h    
RET

答案,@ johnfound:

Answer, by @johnfound:

我发现他的方法更简单,并且花费更少的时间来生成随机数.他提到只有当您需要一个随机数,或者随机数之间的间隔包括人为输入的停顿时,此方法才有效.如果不是这样,这些数字将根本不会是随机的(我相信由于我们最初采取的时间种子不会改变).就我的情况而言,这很好,因为我正在模拟掷骰子,并且需要用户干预(另一个掷骰),然后才能再次运行代码.

I found his way is simpler, and takes less time to generate the random number. He mentioned that this works only if you need one single random numbers, or the interval between the random numbers includes pauses for human input. If not, the numbers will not be random at all (I believe due to the time seed that we initially take doesn't change). It is fine for my case, since I am simulating a roll dice, and I need the user intervention (another roll) before I will be running the code again.

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    

他做了什么: 1.我们将DX中的值移至AX 2.我们清除了DX. 3.我们将12月10日移至CX. 4.我们将AX除以CX,因此得到12月0-9日的余数,该余数存储在DX中.5.最后,我们在DX中添加了ASCII'0'(12月48日),以将它们转换为ASCII'0'至'9'

What he did: 1.We moved value in DX to AX 2.We cleared DX. 3.We moved 10 dec to CX. 4.We divided AX by CX hence we get a remainder within 0-9 Dec which is stored in DX 5.Finally, we added ASCII '0' (dec 48) to DX to get them into ASCII '0' to '9'.

推荐答案

仅当您需要一个随机数,或者随机数之间的间隔包含用于人工输入的暂停时,此技巧才有效.在所有其他情况下,这些数字根本不是随机的.

This trick works only if you need one single random numbers, or the interval between the random numbers includes pauses for human input. In all other cases, the numbers will not be random at all.

如果您需要许多随机数,那么可以使用不同的伪随机数算法.

If you need many random numbers, then there are different pseudo-random number algorithms available.

另一个要注意的是,有一种更简单的方法可以在所需的时间间隔内获取数字:

Another note is that there is more easy way to get the number in the needed interval:

    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'

您当然可以对每个随机数生成器使用此方法.

You can use this method for every random number generator of course.

这篇关于在x86 8086 Assembly中生成0-9范围内的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆