Skip to main content

Microcontrollers

Section 15.2 Arithmetic and Logic Instructions

Arithmetic and logic instructions allow the microcontroller to add, subtract, and multiply. Data can be complemented using either one’s or two’s complement. Logical operations include AND, OR, and XOR. Some of these instructions have the option to be used with or without a carry and using signed, unsigned, or fractional values. All of these instructions are carried out within one or two clock cycles. Most all of these instructions affect flags in SREG.
Of the arithmetic and logic instructions, many of them either use direct, two register addressing (Subsection 13.4.1) or immediate addressing with direct, one register addressing (Subsection 13.4.2). For example, the AND instruction is used to perform the logical AND operation on the contents of two general purpose registers (a source and destination) and save the results back into the destination register. The ANDI instruction is used to perform the logical AND operation on the contents of one general purpose register with an immediate and save the results back into the same general purpose register.
The logic instructions are essentially equivalent to bitwise operators (described in Section 14.5). Table 15.2.1 lists the logical assembly instructions and their equivalent C counterparts. It is possible to set an individual bit in a general purpose register using SBR, which is similar to using a bitwise OR with a single high bit. Similarly, an individual bit in a general purpose register can be cleared using CBR, which is similar to using a bitwise AND with a single low bit. (Shifting bits is discussed in Section 15.4, as bitshifting is accomplished using bit manipulation instructions.)
Table 15.2.1. Logical instructions and their C counterparts.
Operation Instruction(s) C Operation
AND AND, ANDI, CBR &
OR OR, ORI, SBR |
XOR EOR ^
NOT COM ~
Addition and subtraction operations can take place with or without a carry. For example, ADD simply adds the contents of two general purpose registers (a source and a destination) and saves the result into the destination register. ADC adds the contents of two general purpose registers (a source and a destination) and the contents of the C flag in SREG, and then saves the result in the destination register.
The arithmetic and logic instructions also contain instructions that can
  • increment a register: Rd ← Rd + 1,
  • decrement a register: Rd ← Rd - 1,
  • clear a register: Rd ← 0x00, and
  • set a register: Rd ← 0xFF.