首页 > 解决方案 > ARM Assembly increasing number with push button

问题描述

I want to ask you a question about ARM Assembly,

.global _start
_start:

.equ COOL, 0x393f3f38
.equ KEY_BASE_ADRESS, 0xFF200050
.equ S_SEGMENT_BASE_ADRESS, 0xFF200020

LDR R1, =KEY_BASE_ADRESS
LDR R2, =S_SEGMENT_BASE_ADRESS
MOV R8, #0x0000000F // Register that holds the number
MOV R5, #0// R5 holds the number obtanined from player  

CHECK_SWITCH:   
LDR R6, [R1]
CMP R6, #2
BEQ ADD
B CHECK_SWITCH  

ADD:
ADD R5, R5, #1
CMP R5, R8  
BNE CHECK_SWITCH

DISPLAY:
LDR R5, =COOL
STR R5, [R2]

.end

I am trying make a game (guess the number but not very meaningfull so far) for de1-soc.I want to increase "1" player's number in R5, when he pushes KEY1. Also 7 segment will display "COOL" when the number in R5 equals to the R8. Problem is when I push and release the KEY1, program executes so fast and finish the program. What I need is, every push and and release KEY1, should increase only "1" How can I handle this? Thanks!

By the way you can execute code from here https://cpulator.01xz.net/?sys=arm-de1soc

标签: assemblyarm

解决方案


您的代码流是

check for key press:
while key not pressed goto check for key press

add one
go to check for key press:

一旦开关改变状态然后按键被按下它就不再等待它只是不断地通过外部(添加一个以检查按键)循环。

你想要的是检查未按下状态作为外循环中的某处

check for key not pressed:
while key pressed goto check for key not pressed

check for key press:
while key not pressed goto check for key press

add one
go to check for key not pressed

或者

check for key press:
while key not pressed goto check for key press

check for key not pressed:
while key pressed goto check for key not pressed

add one
go to check for key press

或者

check for key press:
while key not pressed goto check for key press

add one

check for key not pressed:
while key pressed goto check for key not pressed

goto check for key press

您可以将加一和检查最大计数分成两件事,然后将其洒在...

但是,一旦开关位断言,您将撕开外循环,因为您不能足够快地将开关放回原处。如果要计算开关的单独按下次数,则需要轮询循环中的断言和取消断言状态。

还可以考虑使用 tst 而不是 cmp。在文档中查找。


推荐阅读