Path: Home ==> AVR-EN ==> Micro beginner ==> Expressions     Diese Seite in Deutsch (extern): Flag DE Logo
ATtiny13

List of math expressions in AVR assembler


Number formats

The following number formats are valid in assembler:
Number formatMarkExample
Decimal number(none).equ Number = 123
Binary format0b.equ Number = 0b1111011
Hexa decimal format0x.equ Number = 0x7B

Small letters in assembler are converted to capital letters, so 0x converts to 0X. Within text strings no conversion takes place, e.g. .db "abcd" is not equal to "ABCD".

In expressions blanks and tab characters can be used to beautify the expression for the human reader. This is not allowed within operators that consist of more than one character, such as "!=" or "<=". Blanks and tab characters are completely removed from the assembler prior to expression processing.

Math expressions

Math expressions allow to calculate formulas or allow to express logical decisions.

It is essential to understand that only the assembler performs such expressions, and that only the final result of those expressions produces executable code. The controller is completely unaware of those calculations, as those calculations as such are not producing any code. The controller is only getting results of expressions if instruction words such as LDI Register,expression or a table consisting of such results, e. g.
.db expression1,expression2,expression3,expression4 ; Inserts a table with four bytes (two words)

or
.dw expression1,expression2,expression3,expression4 ; Inserts a table with four words

is inserted to the code.

Note that in assembler all operations are pure integer. All numbers are integers and all calculations, even interim results within one formula, are rounded down. If you want rounding, you'll have to formulate like this:

.equ N1 = 2345
.equ N2 = 56
.equ Erg1 = N1 / N2 ; Division result is 41
.equ Erg = (N1 + N2 / 2) / N2 ; Rounded result is 42 
 


Different assemblers handle integer values different: either with 32 or with 64 bit length. The number ranges that can be handled depend from that.

The following table lists all types of expressions in assembler.

Result typeSymbol
Expression
DescriptionExample
Logical!Not, negation of the logical value .equ f2 = ! f1 ; yields true (1) if f1 is false (0)
&&Combines the left and the right logical value with AND .equ f = 1 && 0 ; yields false (0)
||Combines the left and the right logical value with OR .equ f = 0 || 1 ; yields true (1)
Compare, logical< Compare if left expression is smaller than right expression .equ f = 234 < 567 ; yields false (0)
<=Compare if left expression is smaller than or equal to right expression .equ f = 234 /lt;= 234 ; yields true (1)
>Compare if left expression is smaller than right expression .equ f = 234 > 567 ; yields false (0)
>=Compare if left expression is larger than or equal to right expression .equ f = 234 >= 234 ; yields true (1)
==Compare if left expression is equal to right expression .equ f = 234 == 234 ; yields true (1)
!=Compare if left expression is unequal to right expression .equ f = 234 != 234 ; yields false (0)
Binary number ~Bit-wise NOT (Bit-by-bit negation) .equ 0b1010 ~ 0011 ; yields 0b1001
&Bit-wise AND (Bit-by-bit-AND) .equ f = 0b1010 & 0b0010 ; yields 0b0010
|Bit-wise OR (Bit-by-bit-OR) .equ f = 0b1010 | 0b0011 ; yields 0b1011
^Bit-wise XOR (Bit-by-bit exclusive-OR) .equ f = 0b1010 | 0b0011 ; yields 0b1001
<<Left shift (Multiplication by 2n) .equ f = 1 << 7 ; yields 0b1000000
>>Right shift (Division by 2n) .equ f = 0b11000000 >> 2 ; yields 0b00110000
Arithmetic, binary number +Plus, Addition .equ f = 123 + 24 ; yields 147
-Subtraction, Minus .equ f = 123 - 24 ; yields 99
*Malnehmen, Multiplizieren .equ f = 123 * 24 ; ergibt 2952
/Teilen, Dividieren .equ f = 123 / 24 ; ergibt 5!
%Modulo, Division rest .equ f = 123 % 24 ; yields 3

Priority rules

The higher the expression is listed in the above table, the higher is the priority of execution. In the formula
.equ result = 1<<3 + 1<<5

the left shifts are higher rated than the plus and are performed first.

If you are not sure or if you want to override the standard handling, use brackets: expressions in brackets are always processed first, e. g.:
.equ result = (1<<3) + (1<<5)

Functions

The following funktions can be used in calculations

FunctionyieldsExample
LOWthe lower 8 bits .equ f = Low(0x1234) ; yields 0x34
HIGHthe upper 8 bits .equ f = High(0x1234) ; yields 0x12
BYTE2Bits 8 to 15 .equ f = Byte2(0x1234) ; ergibt 0x12
BYTE3Bits 16 to 23 .equ f = Byte3(0x123456) ; yields 0x12
BYTE4Bits 24 to 31 .equ f = Byte4(0x12345678) ; yields 0x12
LWRDBits 0 to 15 .equ f = Lwrd(0x12345678) ; yields 0x5678
HWRDBits 16 to 31 .equ f = Hwrd(0x12345678) ; yields 0x1234
EXP2Two powered by number .equ f = Exp2(16) - 1 ; yields 65535
LOG2Logarithm of number by base 2 .equ f = Log2(65535) ; yields 15 (!)


©2018 by www.gsc-elektronic.net