微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ARMv7 程序集,我如何改进这个伪随机数生成器?

如何解决ARMv7 程序集,我如何改进这个伪随机数生成器?

我有这个伪随机函数,它基于 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,#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,#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

解决方法

我将 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,#1

    MOV PC,LR          @once it's an integer less than 5,go to top. 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。