0

so i have to build a dice game in assembly using raspberry pi 3 and gpio. i have build most of it but what i am stuck is the range part of the the random number part. i need the number to fall between 1-6 when the user pushes the button. i have looked at other examples but one of them seem to help me. using linear congenital generator seems like over kill.

.text
.global main

main:
    push {lr}
    bl wiringPiSetup

     push {lr}
        bl rand // will get a random number and put it in r0
// some wher in here is were you need to do the math to make the number
// fall inbwtween 0- 5 +1 to the result
        mov r1,r0 // will take r0 value and put it in to r1
        ldr r0,=pattern // will take the vaule in patter and give it to r0
        bl printf
        pop {pc}
.data
        pattern: .asciz  "%d\n" 



cmp r0, #1
//bleq moves you back to where you camefrom
beq roll1

cmp r0, #2
beq roll2

cmp r0, #3
beq roll3

cmp r0, #4
beq roll4

cmp r0, #5
beq roll5

cmp r0, #6
beq roll6

roll1:
    // this is to tell the singal to go out
        mov r0, #0  // this is writing to pin 17
        mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
        bl pinMode
        // this is to tell the pins to power up
        mov r0, #0
        mov r1, #0
        bl digitalWrite

roll2:
    // this is to tell the singal to go out
        mov r0, #0  // this is writing to pin 17
        mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
        bl pinMode
        // this is to tell the pins to power up
        mov r0, #0
        mov r1, #0
        bl digitalWrite

        mov r0, #2  // this is writing to pin 27
        mov r1, #0
        bl pinMode

        mov r0, #2
        mov r1, #0
        bl digitalWrite

roll3:
    // this is to tell the singal to go out
        mov r0, #0  // this is writing to pin 17
        mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
        bl pinMode
        // this is to tell the pins to power up
        mov r0, #0
        mov r1, #0
        bl digitalWrite

        mov r0, #2  // this is writing to pin 27
        mov r1, #0
        bl pinMode

        mov r0, #2
        mov r1, #0
        bl digitalWrite

        mov r0, #3 // this is writing to pin 22
        mov r1, #0
        bl pinMode

        mov r0, #3
        mov r1, #0
        bl digitalWrite

roll4:
    // this is to tell the singal to go out
        mov r0, #0  // this is writing to pin 17
        mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
        bl pinMode
    // this is to tell the pins to power up
        mov r0, #0
        mov r1, #0
        bl digitalWrite

        mov r0, #2  // this is writing to pin 27
        mov r1, #0
        bl pinMode

        mov r0, #2
        mov r1, #0
        bl digitalWrite

        mov r0, #3 // this is writing to pin 22
        mov r1, #0
        bl pinMode

        mov r0, #3
        mov r1, #0
        bl digitalWrite

        mov r0, #21  // this is writing to pin 5
        mov r1, #0
        bl pinMode

        mov r0, #21
        mov r1, #0
        bl digitalWrite

roll5:
    // this is to tell the singal to go out
        mov r0, #0  // this is writing to pin 17
        mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
        bl pinMode
// this is to tell the pins to power up
        mov r0, #0
        mov r1, #0
        bl digitalWrite

        mov r0, #2  // this is writing to pin 27
        mov r1, #0
        bl pinMode

        mov r0, #2
        mov r1, #0
        bl digitalWrite

        mov r0, #3 // this is writing to pin 22
        mov r1, #0
        bl pinMode

        mov r0, #3
        mov r1, #0
        bl digitalWrite

        mov r0, #21  // this is writing to pin 5
        mov r1, #0
        bl pinMode

        mov r0, #21
        mov r1, #0
        bl digitalWrite

        mov r0, #22  // this is wriritin  to pin 6
        mov r1, #0
        bl pinMode

        mov r0, #22
        mov r1, #0
        bl digitalWrite


roll6:
// this is to tell the singal to go out
    mov r0, #0  // this is writing to pin 17
    mov r1, #0  //  if it is a one you want power to go to it if not then enter 0
    bl pinMode
// this is to tell the pins to power up
    mov r0, #0
    mov r1, #0
    bl digitalWrite

    mov r0, #2  // this is writing to pin 27
    mov r1, #0
    bl pinMode

    mov r0, #2
    mov r1, #0
    bl digitalWrite

    mov r0, #3 // this is writing to pin 22
        mov r1, #0
        bl pinMode

        mov r0, #3
        mov r1, #0
        bl digitalWrite

    mov r0, #21  // this is writing to pin 5
        mov r1, #0
        bl pinMode

        mov r0, #21
        mov r1, #0
        bl digitalWrite

    mov r0, #22  // this is wriritin  to pin 6
        mov r1, #0
        bl pinMode

        mov r0, #22
        mov r1, #0
        bl digitalWrite

    mov r0, #23  // this is writing to pin 13
        mov r1, #0
        bl pinMode

        mov r0, #23
        mov r1, #0
        bl digitalWrite

button:
// these botton two leave them on at all times they are the power to use the button.
    mov r0, #7
        mov r1, #1
        bl pinMode

        mov r0, #7
        mov r1, #1
        bl digitalWrite

    pop {pc}
// somewhere after pop or before add a deley so that all leds can be turned off
Tommylee2k
  • 2,683
  • 1
  • 9
  • 22
Onsketch
  • 1
  • 6

1 Answers1

1

If you wish to use syscalls, one idea is to call the gettimeofday syscall then use the 3 most significant bits of the microseconds to emulate a dice roll. You should do some randomness testing to determine if this could be a reasonable pseudo-random generator.

.bss
        t:      .zero           // syscall storage
...
ldr r9,=t
...
mov r7, #78                     // <syscall name="gettimeofday" number="78"/>
ldr r0,=t                       // pointer
mov r1, #0                      // 
svc #0
ldr r0,[r9,#1]                  // microseconds in second element
InfinitelyManic
  • 760
  • 1
  • 6
  • 13
  • note: if you use 3 bits of a randomnumber generator, and only use 1-6 values, be sure to REROLL if you get 0 or 7, and not map them anywhere. Otherwise you would change the (by default equal) probabilites – Tommylee2k Dec 13 '16 at 06:57
  • @Tommylee2k, I was thinking about that; namely, whether to roll again or map to 1 or 6, respectfully. I'll do some distribution analysis on both scenarios over a larger number of trials then compare. Thanks! – InfinitelyManic Dec 13 '16 at 13:30
  • @Tommylee2k - good enough for a simple die toss? 1 10787 17.0% 2 10660 16.8% 3 10433 16.4% 4 10420 16.4% 5 10622 16.7% 6 10566 16.6% – InfinitelyManic Dec 13 '16 at 20:45
  • @Maniac yes, that looks real good; is that the numbers rerolling the 0s and 7s? – Tommylee2k Dec 14 '16 at 07:11
  • @Tommylee2k - yes, rerolling for 0 and 7. – InfinitelyManic Dec 14 '16 at 14:02