Section 15.4 Bit Manipulation Instructions
Bit manipulation instructions consist of instructions that allow registers to be manipulated on the bit level. This consists of shifting, swapping, setting, and clearing instructions.
Subsection 15.4.1 Shift Instructions
Shift instructions come in a few varities: logical shift, arithmetic shift, and rotate through carry.
A logical shift is equivalent to bitshift operations in C. When a register is logically shifted right (using the
LSR instruction), the MSB is replaced with a 0. When a register is logically shifted left (using the LSL instruction), the LSB is replaced by a 0. While C code is able to perform multiple bitshifts at a time, each shift instruction in assembly executes a single position at a time. Iterative control flow can be used to execute multiple bit shifts.
In an arithmetic shift right (which is the
ASR instruction, no such arithmetic shift left exists as it would be equivalent to logical shift left), the sign bit is shifted in to the MSB of the register. This is equivalent to dividing a signed or unsigned binary value by two by preserving the sign bit.
Rotate through carry instructions can occur to the left (using the
ROL instruction) or right (using the ROR instruction), and execute a rotation (where the register wraps around so the MSB is moved to the LSB, or vice versa) through the previous value of the carry bit. These instructions are particularly useful when dealing with values that are stored in two registers (i.e., the numbers are greater than 8 bits). When rotating, the value that is rotated out is saved into the carry flag of SREG and is therefore ready to be rotated in to the next byte without the need for additional processing.
Subsection 15.4.2 Swap Instruction
The swap nibbles instruction (
SWAP) swaps the high and low nibbles in the operand register.
Subsection 15.4.3 Store, Set, and Clear Instructions
A single bit of storage is available in the bit copy storage flag (
T) in SREG. Storing data to this flag is accomplished using the bit store from register to T instruction (BST), while retrieving data from this flag is accomplished using the bit load from T to register instruction (BLD).
The last set of bit manipulation instructions pertain to setting and clearing bits. Bits can be set and cleared individually in an I/O register using the set bit in I/O register instruction (
SBI) and clear bit in I/O register instruction (CBI).
Individual bits in
SREG can be set and cleared using the flag set instruction (BSET) and flag clear instruction (BCLR).
Each individual flag in
SREG can be set or cleared using their own individual instructions.
-
The global interrupt enable flag (
I) can be set with the global interrupt enable instruction (SEI) and cleared with the global interrupt disable instruction (CLI). -
The bit copy storage flag (
T) can be set with the set T in SREG instruction (SET) and cleared with the clear T in SREG instruction (CLT). -
The half carry flag (
H) can be set with the set half carry flag in SREG instruction (SEH) and cleared with the clear half carry flag in SREG instruction (CLH). -
The sign flag (
S) can be set with the set signed test flag instruction (SES) and cleared with the clear signed test flag instruction (CLS). -
The twoβs complement overflow flag (
V) can be set with the set twoβs complement overflow instruction (SEV) and cleared with the clear twoβs complement overflow instruction (CLV). -
The negative flag (
N) can be set with the set negative flag instruction (SEN) and cleared with the clear negative flag instruction (CLN). -
The zero flag (
Z) can be set with the set zero flag instruction (SEZ) and cleared with the clear zero flag instruction (CLZ). -
The carry flag (
C) can be set with the set carry instruction (SEC) and cleared with the clear carry instruction (CLC).
