jeudi 8 avril 2021

ARMv7 Assembly, how could I improve on this pseudorandom number generator?

I have this pseudorandom function that's based off the time since unix epotch as a seed, I need to get 4 numbers between 1-5, currently what I have this doing is getting a single digit at a time and printing it. This was originally for numbers between 0-9 and changing it to be 1-5 I realized that the numbers 6-9 will always result in being a 5, meaning that 5 has a higher overall probability of occuring than any other number.

This is just for a simple guessing game so it's not doing anything cirtical

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. 

I rather than continuously subtract 1 from a value greater than 5 and add 1 to a value of 0 I tried the following and just got "garbage" as the output:

timeofday and fakemod loop are the same.

CMP r1, #5
BGT timeofday

CMP r1, #0
BEQ timeofday



Aucun commentaire:

Enregistrer un commentaire