Inline Assembly if statements

Hi guys, I need to write the following code as inline assembly :

if (digitalRead(btnChange) == HIGH) {
variable++;
if (variable > 4) {
variable = 0;
}
How can I even compare a variable with another value?
I'm thinking of somehow loading the value of the variable in a register, compare it with the register with the value of 4, then set the register which contains the variable to 0, but I have no idea how to do that

Why?

How can I even compare a variable with another value?

Use the appropriate machine instructions. Have you studied them?

It's for a uni project that I have to finish soon and one of the requirements is to write a part of the code as inline assembly, I tried to do a blinking LED that starts blinking when an rtc alarm in my project goes off but I cant get the LED to blink, it just stays turned on and my LCD doesn't display anything anymore :slight_smile:

I somewhat "understand" the commands listed here https://create.arduino.cc/projecthub/yeshvanth_muniraj/accessing-i-o-in-atmega328p-arduino-using-assembly-and-c-10e063

You need to complete that learning process, as I imagine the course requirement intends.

For the ATmega series, use the machine instruction cmp to compare values.

There are much better sources of information and instruction about assembly language programming than that short tutorial.

You're missing a closing brace. I assume it's at the end?

Pick something simple, like a pinMode or a digitalWrite :wink:

You could compile it and hand in the compiler's work? :person_gesturing_ok: Don't say I told you though... also be ready for a drossing if you're asked to explain how it works...
...and at exam time
...and when you get a job
...

Yes I forgot to select it when i copy pasted it

I tried but it messes up my LCD for some reason and that would be way too easy( i will keep it like that if I can't find a solution to this)

I'm trying to use this code in assembly and I need the variable "slot" for further use

 if (digitalRead(btnChange) == HIGH) {
     slot++;
      if (slot > 4) {
        slot = 0;     
        }

I wrote it like in the screenshot but I get these errors? What did I do wrong?


volatile int slot = 0;
setup() {
asm(
"LDI R18, (slot) \n"
"LDI R19, 0 \n"
"LDI R20, 5 "

  );
}
}
and as replacement for the if statement in loop()
asm(
"start: \n"
"SBIS 0x09, 1 \n"
"INC R18 \n"           //inc R18
"CPI R18, R20 \n" // cmpi R18, 0x0005
"BREQ sub \n"
"RET "

"sub: \n"
"DEC R18 \n"
"CPI R18, R19 \n"
"BREQ end \n"
"RJMP sub \n"

"end: \n"
`"RET"`

);

Hi, @grimmiejaq

To add code please click this link;

This will let us easily view your code.

Tom... :smiley: :+1: :coffee: :australia:

edit your post. paste code . use Ctrl+E to embed it.

Done

@grimmiejaq

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Please also post the error messages as text, in code tags also. Obviously, the screen shots are useless to read.

Also, there is code in one screenshot that you didn't include in your text post. So please correct that.

Are there any other unmentioned constraints that were not mentioned in the original post?

So i modified my code to this and I don't get any errors, but on my LCD where the change of the variable should be displayed nothing happens and my LCD freezes

 if(digitalRead(btnChange) == HIGH) {

    asm(
"start:       \n"
//"SBIS 0x09, 1 \n"
"CPI %0, 5 \n"
"BREQ mid \n"
"CPI %0, 4 \n"
"BREQ mid \n"
"INC %0 \n"
"CPI %0, 5 \n"
"BREQ mid \n"
"RET \n"
"mid: \n"
"DEC %0 \n"
"CPI %0, 0 \n"
"BREQ end \n"
"RJMP mid \n"
"end: \n"
"RET"


 : : "r" (slot) 
);
}

no, just that the change of the value of this variable affects the rest of the program

What is your question?

I just need to write this code to assembly code

if (digitalRead(btnChange) == HIGH) {
     slot++;
      if (slot > 4) {
        slot = 0;     
        }

I defined "slot" globally as
volatile int slot = 0;