【发布时间】: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