When programming in assembly language, it is often helpful to manipulate individual bits.
Shift and Rotate
- ROR and ROL rotate 9 bits (8 bits plus carry flag) to the right or left
- LSL and LSR: logical shifts are like the rotates except the values don't wrap around.
- The least significant bit is cleared when doing a LSL.
- The most significant bit is cleared when doing a LSR.
- ASR: Arithmetic shift is like LSR except it retains the value of the most significant bit.
- SBI port, bit: Set bit in port
sbi PORTB, 7
- The above is equivalent to:
in r16, PORTB ori r16, 0b10000000 out PORTB, r16
- CBI: Clear bit in port
- CBI and SBI only operates on one bit at a time.
- CBR Rd, mask: Clear Bit in Register (R16-R31)
- mask — contains 1's in positions to be cleared.
cbr r16, 0x0f ; Clears the four least significant bits
- CLR Rd — clears all bits in Rd.
- SER Rd — sets all bits in Rd (R16-R31).
- To do the equivalent on R0-R15, we could do this:
clr r0 com r0 ; Perform 2's complement on R0
- SBIC PORT, bit — skip if bit clear
- The following will continue to loop until the most significant bit is cleared:
poll: sbic PIND, 7 ; skips next instruction if bit 7 is cleared rjmp poll
- SBIS PORT, bit — skip if bit set
- SBIS only works on the first 32 I/O ports (which includes PORTA - PORTD).