所以我必须使用raspberry pi 3和gpio在程序集中构建一个骰子游戏。我已经构建了大部分内容,但我遇到的是随机数部分的范围部分。当用户按下按钮时,我需要数字介于1-6之间。我看过其他例子,但其中一个似乎对我有帮助。使用线性先天发电机似乎过度杀死。
.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
答案 0 :(得分:1)
如果你想使用系统调用,一个想法是调用gettimeofday系统调用,然后使用微秒的3个最高位来模拟掷骰子。您应该进行一些随机性测试,以确定这是否是一个合理的伪随机生成器。
.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