Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 5:43 am

Question: I am coding up the ladder game on my Raspberry Pi 3. This program is in assembly language. I have...
I am coding up the ladder game on my Raspberry Pi 3.

This program is in assembly language. I have a breadboard hooked up to the PI

There are a set of 6 LEDS and one button.

The LEDS will flash, and if you push the button as they flash you will move up to the next LED.

Ideally, I would like each LED to turn solid and stop blinking to show where you are currently at.

Maybe not have them all flash at once, but one at a time and have each one turn solid as you move up... whatever would be easier.

As of right now I have them all flashing together at the same time but can't figure out how to incorporate the button. Maybe some kind of interrupt to make the button turn solid.

Here is the code

Code: Select all

.equ INPUT, 0
.equ OUTPUT, 1
.equ LOW, 0
.equ HIGH, 1

.data
.balign 4
Intro: .asciz "Ladder Game\n"
ErrMsg: .asciz "Didnt work... Exiting Now.\n"
pin2:   .int 2 //WiringPi pin 2 is equal to BCM 27
pin3:    .int    3 //WiringPi pin 3 is equal to BCM 22
pin4:    .int    4 //WiringPi pin 4 is equal to BCM 23
pin5:    .int    5 //WiringPi pin 5 is equal to BCM 24
pin6:    .int    6 //WiringPi pin 6 is equal to BCM 25
pin7: .int 7 //WiringPi pin 7 is equal to BCM 4
end:   .int 5
button: .int 26
i: .int 0
delayMs: .int 250
OUTPUT = 1

.text
.global main
.extern printf
.extern wiringPiSetup
.extern delay
.extern digitalWrite
.extern pinMode
.extern digitalRead

main: push {lr}

// printf( "Ladder Game..." ) ;
ldr r0, =Intro
bl printf

// if (wiringPiSetup() == -1) {
// printf( "Didnt work... Exiting Now" ) ;
// exit (1) ;
// }
bl wiringPiSetup
mov r1,#-1
cmp r0, r1
bne init
ldr r0, =ErrMsg
bl printf
b done

// pinMode(pin, OUTPUT) ;
init:
   ldr    r0, =button
   ldr    r0, [r0]
   mov    r1, #OUTPUT
   bl    pinMode

ldr r0, =pin2
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

   ldr r0, =pin3
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

   ldr r0, =pin4
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

   ldr r0, =pin5
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

   ldr r0, =pin6
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

   ldr r0, =pin7
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

// for ( i=0; i<10; i++ ) {
ldr r4, =i
ldr r4, [r4]
mov r5, #10
forLoop:
cmp r4, r5
bgt done

// digitalWrite(pin, 1) ;
ldr r0, =pin2
ldr r0, [r0]
mov r1, #1
bl digitalWrite

   ldr r0, =pin3
ldr r0, [r0]
mov r1, #1
bl digitalWrite

   ldr r0, =pin4
ldr r0, [r0]
mov r1, #1
bl digitalWrite

   ldr r0, =pin5
ldr r0, [r0]
mov r1, #1
bl digitalWrite

   ldr r0, =pin6
ldr r0, [r0]
mov r1, #1
bl digitalWrite

   ldr r0, =pin7
ldr r0, [r0]
mov r1, #1
bl digitalWrite

// delay(250) ;
ldr r0, =delayMs
ldr r0, [r0]
bl delay

   ldr r0, =delayMs
ldr r0, [r0]
bl delay

   ldr r0, =delayMs
ldr r0, [r0]
bl delay

   ldr r0, =delayMs
ldr r0, [r0]
bl delay

   ldr r0, =delayMs
ldr r0, [r0]
bl delay

   ldr r0, =delayMs
ldr r0, [r0]
bl delay


// digitalWrite(pin, 0) ;
ldr r0, =pin2
ldr r0, [r0]
mov r1, #0
bl digitalWrite

   ldr r0, =pin3
ldr r0, [r0]
mov r1, #0
bl digitalWrite

   ldr r0, =pin4
ldr r0, [r0]
mov r1, #0
bl digitalWrite

   ldr r0, =pin5
ldr r0, [r0]
mov r1, #0
bl digitalWrite

   ldr r0, =pin6
ldr r0, [r0]
mov r1, #0
bl digitalWrite

   ldr r0, =pin7
ldr r0, [r0]
mov r1, #0
bl digitalWrite


// delay(250) ;

ldr r0, =delayMs
ldr r0, [r0]
bl delay

add r4, #1
b forLoop

done:
pop {pc}

LdB
Posts: 1004
Joined: Wed Dec 07, 2016 2:29 pm

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 1:10 pm

You are using wiring pi library (all be it calling it from assembly) and there are already piles of examples out there ... Do a search "wiringpi button example" it will give you a pile of ideas and code.

There is no right and wrong way to do it, you basically just read a GPIO port that is setup with a resistor and the button.

The physical setup is shown here
https://www.cl.cam.ac.uk/projects/raspb ... _switches/

User avatar
DavidS
Posts: 3791
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 2:45 pm

+1 on what LdB said.

Also you may learn a good bit more about assembly if you directly accessed the port registers instead of using WiringPi, though learn the basics first.

I would recommend getting it working in C first (so you can learn the library) then go to assembly (which will be easier, though not as WiringPi like).
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

jahboater
Posts: 3758
Joined: Wed Feb 04, 2015 6:38 pm

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 2:58 pm

DavidS wrote:
Thu Dec 06, 2018 2:45 pm
I would recommend getting it working in C first (so you can learn the library) then go to assembly (which will be easier, though not as WiringPi like).
That's a good idea.

Then if you want it in assembler instead, convert it with the "-Os -S -fverbose-asm" options for GCC.

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 3:41 pm

LdB wrote:
Thu Dec 06, 2018 1:10 pm
You are using wiring pi library (all be it calling it from assembly) and there are already piles of examples out there ... Do a search "wiringpi button example" it will give you a pile of ideas and code.

There is no right and wrong way to do it, you basically just read a GPIO port that is setup with a resistor and the button.

The physical setup is shown here
https://www.cl.cam.ac.uk/projects/raspb ... _switches/
Yes I seen examples but they all seem to be in python wich I have no expirience with yet.

LdB
Posts: 1004
Joined: Wed Dec 07, 2016 2:29 pm

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 4:29 pm

As suggested do a search for C code .. and compile it to verbose assembler so you can work with it

If you take a C polled version it does something like this

Code: Select all

// scanButton .. wait for the button to be let-go
void scanButton (int button)
{
  if (digitalRead (button) == HIGH)	// Low is pushed
    return ;
  while (digitalRead (button) == LOW)	// Wait for release
    delay (10) ;
}
Do what jahboater said compile it an look at the assembler you will recognize it immediately and be able to work out what it does.
It is going to look something like this (give me some latitude here I am just guessing here what the compiler will do)

Code: Select all

/* Entry R0 will have button id */
scanButton:
   mov r4, r0      // hold the button ID so it does not get trashed on digital read call
   bl digitalRead  // Call digital read will return with r0 set
   cmp r0, #1       // Check if return was high
   bz exit_scanButton   // Exit on if high
scan_loop:
   mov r0, r4         // Restore the button ID so r0 is again set
   bl digitalRead  // Call digital read will return with r0 again
   cmp  r0, #0      // check for low
   bz scan_loop   // Loop until release
   mov r0, #10     // We want a delay of 10 whatever they are
   bl delay            // call the delay
exit_scanButton:
   bx  lr                 // return from call to scanButton

jahboater
Posts: 3758
Joined: Wed Feb 04, 2015 6:38 pm

Re: HELP! ARM ASSEMBLY PROJECT

Thu Dec 06, 2018 5:57 pm

If the compiler is GCC version 7 or later then the original C source lines will be intermixed with the assembler, making it very easy to find out whats going on. -Os (compile for small size) produces assembler more like a human would write. -O3 can produced very obscure code.

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Sat Dec 08, 2018 8:02 pm

LdB wrote:
Thu Dec 06, 2018 4:29 pm
As suggested do a search for C code .. and compile it to verbose assembler so you can work with it

If you take a C polled version it does something like this

Code: Select all

// scanButton .. wait for the button to be let-go
void scanButton (int button)
{
  if (digitalRead (button) == HIGH)	// Low is pushed
    return ;
  while (digitalRead (button) == LOW)	// Wait for release
    delay (10) ;
}
Do what jahboater said compile it an look at the assembler you will recognize it immediately and be able to work out what it does.
It is going to look something like this (give me some latitude here I am just guessing here what the compiler will do)

Code: Select all

/* Entry R0 will have button id */
scanButton:
   mov r4, r0      // hold the button ID so it does not get trashed on digital read call
   bl digitalRead  // Call digital read will return with r0 set
   cmp r0, #1       // Check if return was high
   bz exit_scanButton   // Exit on if high
scan_loop:
   mov r0, r4         // Restore the button ID so r0 is again set
   bl digitalRead  // Call digital read will return with r0 again
   cmp  r0, #0      // check for low
   bz scan_loop   // Loop until release
   mov r0, #10     // We want a delay of 10 whatever they are
   bl delay            // call the delay
exit_scanButton:
   bx  lr                 // return from call to scanButton
Can i use this piece of code on my program?

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 1:50 am

I just need a piece of code that I can add to the program to make it work how it should! I’m struggling a bit!

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 5:25 am

.
Last edited by Gabgorozco on Mon Dec 10, 2018 9:04 am, edited 1 time in total.

fruitoftheloom
Posts: 18794
Joined: Tue Mar 25, 2014 12:40 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 6:30 am

Gabgorozco wrote:
Mon Dec 10, 2018 5:25 am
HELP PLEASE

This forum is moderated by volunteers, answered by volunteers and no one gets paid, so demanding is bad Netiquette...
adieu

My other Computer is an Asus CS10 ChromeBit
https://www.asus.com/uk/Mini-PCs/Chromebit-CS10

LdB
Posts: 1004
Joined: Wed Dec 07, 2016 2:29 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 8:01 am

I am not sure how anyone can help more as you do not appear to have tried anything suggested.

1.) You are clearly working in linux with wiringpi library so for many of us here not something we generally play with as
we would do it raw baremetal ... no linux no wiringpi. Given your current problem that level is way beyond your ability.

2.) So you were told to simply compile the C code and look at the assembler output
- If you are on raspbian the C compiler is installed all you need do is compile the samples
- Gordon (author of wiringPi) has a whole pile of stuff already done which work
https://projects.drogon.net/raspberry-p ... -a-button/
- There are also a lot of other c code out there.
- Effort required 5 minutes and a couple of file downloads or text cut and paste
- You should be able to easily change any C code to the ports you want and compile the C code and check it works another 5 minutes.
- Then all you have a working sample to look at the assembler code for 10 minutes work.

3.) I gave you code which likely works or is at least close enough you should be able to debug

However for for the record again ... to read a button on a GPIO you need simply two things
1.) set the gpio mode to input
2.) read the gpio
There are no other requirements so limit your code to that,

So using your code lets assume you want to read a button on "somepin" being a wiringpi number
I am taking a leap of faith you realize "somepin" needs to be replaced by the pin number you wish to use and don't type it literally.
First set the port to input .. you only need ever do this once
ldr r0, =somepin
ldr r0, [r0]
mov r1, #INPUT
bl pinMode
Now read the pin ... the result of the read will come back in r0
anytime you want to read the pin you just need those lines of code
ldr r0, =somepin
ldr r0, [r0]
bl digitalRead
.
That is the only 7 lines of code required to at least get something working via polling

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 9:10 am

LdB wrote:
Mon Dec 10, 2018 8:01 am
I am not sure how anyone can help more as you do not appear to have tried anything suggested.

1.) You are clearly working in linux with wiringpi library so for many of us here not something we generally play with as
we would do it raw baremetal ... no linux no wiringpi. Given your current problem that level is way beyond your ability.

2.) So you were told to simply compile the C code and look at the assembler output
- If you are on raspbian the C compiler is installed all you need do is compile the samples
- Gordon (author of wiringPi) has a whole pile of stuff already done which work
https://projects.drogon.net/raspberry-p ... -a-button/
- There are also a lot of other c code out there.
- Effort required 5 minutes and a couple of file downloads or text cut and paste
- You should be able to easily change any C code to the ports you want and compile the C code and check it works another 5 minutes.
- Then all you have a working sample to look at the assembler code for 10 minutes work.

3.) I gave you code which likely works or is at least close enough you should be able to debug

However for for the record again ... to read a button on a GPIO you need simply two things
1.) set the gpio mode to input
2.) read the gpio
There are no other requirements so limit your code to that,

So using your code lets assume you want to read a button on "somepin" being a wiringpi number
I am taking a leap of faith you realize "somepin" needs to be replaced by the pin number you wish to use and don't type it literally.
First set the port to input .. you only need ever do this once
ldr r0, =somepin
ldr r0, [r0]
mov r1, #INPUT
bl pinMode
Now read the pin ... the result of the read will come back in r0
anytime you want to read the pin you just need those lines of code
ldr r0, =somepin
ldr r0, [r0]
bl digitalRead
.
That is the only 7 lines of code required to at least get something working via polling
I tried what you suggested. But for some reason the button doesn’t seem to respond. That’s why I asked again! But thanks for your help!

LdB
Posts: 1004
Joined: Wed Dec 07, 2016 2:29 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 12:25 pm

Gabgorozco wrote:
Mon Dec 10, 2018 9:10 am
I tried what you suggested. But for some reason the button doesn’t seem to respond. That’s why I asked again! But thanks for your help!
What exactly did you try (description required with code it should probably be less than 10 lines of code) and what did it do (description required)?

We can only help what we can have information on.

Gabgorozco
Posts: 14
Joined: Wed Oct 10, 2018 6:08 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 2:57 pm

LdB wrote:
Mon Dec 10, 2018 12:25 pm
Gabgorozco wrote:
Mon Dec 10, 2018 9:10 am
I tried what you suggested. But for some reason the button doesn’t seem to respond. That’s why I asked again! But thanks for your help!
What exactly did you try (description required with code it should probably be less than 10 lines of code) and what did it do (description required)?

We can only help what we can have information on.
I added the piece of code u recommended

Code: Select all

/* Entry R0 will have button id */
scanButton:
   mov r4, r0      // hold the button ID so it does not get trashed on digital read call
   bl digitalRead  // Call digital read will return with r0 set
   cmp r0, #1       // Check if return was high
   bz exit_scanButton   // Exit on if high
scan_loop:
   mov r0, r4         // Restore the button ID so r0 is again set
   bl digitalRead  // Call digital read will return with r0 again
   cmp  r0, #0      // check for low
   bz scan_loop   // Loop until release
   mov r0, #10     // We want a delay of 10 whatever they are
   bl delay            // call the delay
exit_scanButton:
   bx  lr                 // return from call to scanButton
This piece here and the button was still not responding!!!

LdB
Posts: 1004
Joined: Wed Dec 07, 2016 2:29 pm

Re: HELP! ARM ASSEMBLY PROJECT

Mon Dec 10, 2018 11:42 pm

Ok there is problems here, remember the two requirements above
1.) set the gpio mode to input
2.) read the gpio

That code does not deal with the setting of the port to input it assumes you have done it ...did you do it?
You also do not show us what you set in register r0 it is reliant on that .. so what value did you set for the call?

Finally why don't you simply clear out all other code and just stick to the minimal 7 lines of code plus display what
comes back in r0 from those 7 lines.

I guess what I am struggling to gauge is your assembler programming ability, your initial code shows a different level of ability to your later answers. So we are answering at a level of your code and what I am wondering if it isn't your code and you are struggling with what we are saying.

User avatar
DavidS
Posts: 3791
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: HELP! ARM ASSEMBLY PROJECT

Wed Dec 12, 2018 1:22 pm

Also if you did set the pinmode and you are otherwise calling correctly (which I do not think you have done both):

How is your button hooked up? If it is hooked directly from a source to the GPIO you would likely fry the GPIO (and maybe the whole block) on the RPi. You need a resistor in there at minimal, to limit current, and you will have best of luck if you are also using an external pullup (or pulldown depending on your circuit).
RPi = Way for me to have fun and save power.
100% Off Grid.
Household TTL Electricity Usage = 1.4KW/h per day.
500W Solar System, produces 2.8KW/h per day average.

Return to “Bare metal, Assembly language”