Electronic – Best way to compare value in register with constant assembly AVR

assemblyavrmicrocontroller

I am new to assembly and AVR. What is the best way to compare two values like in C:

#define SOME_VAL 55
int current_counter = 0;
...
...
if(current_counter==SOME_VAL) {

}

In assembly I have the following code:

.EQU SOME_VAL = 55

.def current_counter = r19

What is the best way to compare the current value with a defined constant?

Best Answer

The instruction you need has the mnemonic CPI (compare with immediate).

This will set or clear a flag depending on the result.

You can then branch after checking the flag.

Have a look at the following example. It's from the [8-bit] AVR instruction summary (http://www.atmel.com/images/doc0856.pdf).

The example below is from page 24:

Example:

cpi r20,5 ; Compare r20 to the value 5
brbc 1,noteq ; Branch if Zero Flag cleared
...
noteq:nop ; Branch destination (do nothing)

There are other instructions that will do slightly different comparisons (like compare two registers, instead of comparing a register with a literal).