【问题标题】:ARMv7 Assembly, how could I improve on this pseudorandom number generator?ARMv7 程序集,我该如何改进这个伪随机数生成器?
【发布时间】:2021-04-08 18:38:18
【问题描述】:

我有这个伪随机函数,它基于自 unix epotch 作为种子以来的时间,我需要得到 1-5 之间的 4 个数字,目前我所做的是一次获取一个数字并打印它。这最初是针对 0-9 之间的数字并将其更改为 1-5 我意识到数字 6-9 将始终导致为 5,这意味着 5 比任何其他数字具有更高的总体出现概率。

这只是一个简单的猜谜游戏,所以它没有做任何事情

timeofday:

    MOV r7, #78         @gettimeofday returns time
    LDR r0, =time       @address of holder for time_t
    MOV r1, #0          @timezone not needed
    SWI #0
    
    LDR r0, =time       @set r0 back to label addr
    LDRB r1, [r0, #4]   @load epotch value from r0 into r1
    
    MOV PC, LR          @back to calling location, Then it will branch to the function below.

fakemod:
    CMP r1, #10         @Ensuring we don't create a negative
    SUBGE r1, r1, #10   @decrease r1 by 10 until it's a single digit
    CMP r1, #10
    BGE fakemod         @if the number in r1 is still greater than 10 loop
    
singledigit:            @Need a value between 1-5
    CMP r1, #5          @Checks in r1 has a value larger than 5
    SUBGT r1, r1, #1    @subtracts if r1 is a integer greater than 5
    CMP r1, #5          @compares if the number is greater than 5 still
    BGT singledigit     @if greater than 5 go back to loop start.
    
    CMP r1, #0       @needs to be between 1-5, so if it's 0, just add 1
    ADDEQ r1, r1, #1
     
    MOV PC, LR          @once it's an integer between 1-5, go back to call location. 

我没有连续从大于 5 的值中减去 1 并将 1 加到 0 的值上,而是尝试了以下操作,得到了“垃圾”作为输出:

timeofday and fakemod loop are the same.

CMP r1, #5
BGT timeofday

CMP r1, #0
BEQ timeofday

【问题讨论】:

    标签: random raspberry-pi arm armv7


    【解决方案1】:

    我将 fakemod 函数更改为以下,并得到了我理想中想要的结果。在随机数生成器中将 1 添加到 0 并不理想,但它足以满足我的需求。几周前写的,所以我有点忘记了目的。

    fakemod:
        CMP r1, #5       @Ensuring we don't create a negative
        SUBGT r1, r1, #5 @decrease r1 by 5 until it's a single digit
        CMP r1, #5
        BGT fakemod         @if the number in r1 is still greater than 10 loop
    
        CMP r1, #0
        ADDEQ r1, r1, #1
    
        MOV PC, LR          @once it's an integer less than 5, go to top. 
    

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 2011-09-16
      • 2014-05-18
      • 2011-11-03
      • 1970-01-01
      • 2016-02-27
      • 2014-10-30
      • 2015-08-07
      • 1970-01-01
      相关资源
      最近更新 更多