ARM Assembly increasing number with push button

0

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

assembly
arm
asked on Stack Overflow May 27, 2020 by Mert Genco

1 Answer

0

Your code flow is

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

add one
go to check for key press:

Once the switch changes state then the key is pressed it doesn't wait any more it just keeps whipping through the outer (add one to check for key press) loop.

What you want is to check for the not pressed state as somewhere in the outer loop

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

Or

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

or

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

You can separate the add one and the check for max count into two things and sprinkle that about as well...

But as written once the switch bit asserts you will rip through the outer loop, because you cant put the switch back fast enough. If you want to count individual presses of the switch you need to poll for both asserted and deasserted states in the loop.

Also consider using tst instead of cmp. Look it up in the documentation.

answered on Stack Overflow May 27, 2020 by old_timer • edited May 27, 2020 by halfer

User contributions licensed under CC BY-SA 3.0