Bit Manipulation

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.

Bit Manipulation

  • SBI port, bit: Set bit in port
      sbi  PORTA, 7
  • The above is equivalent to:
      ldi  r16, PORTA
      
      out PORTA, 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 (doesn't work on all I/O registers).
cs280/bitmanip.txt · Last modified: 2009/06/03 11:22 (external edit)
 

This website is not owned or managed by the Milwaukee School of Engineering.

© 2003-2010 Dr. Christopher C. Taylor, et. al. • Office: L-343 • Phone: 277-7339 • npǝ˙ǝosɯ@ɹolʎɐʇ • -> RSS <-