UMBC CMSC 391 -- Programming Microcontrollers  


Logical Operations

In C, there is two types of Boolean operations, logical and bitwise. In the logical version, the entire expression will result in the value of zero or non-zero (false or true). In the bitwise operations, the bits in the same position will be combines separately, giving a zero or non-zero result. In the 8051 assembly language, the terminology is different. The C logical is handled by the CJNE and DJNZ instructions. The C bitwise is the 8051 logical and operates on an entire byte. The 8051 bitwise only operates on a single bit.

BooleanMnemonicExample
ANDANL11001100
11010000
------------
11000000
OR ORL11001100
11010000
------------
11011100
XORXRL11001100
11010000
------------
00011100
NOTCPL11001100
------------
00110011

Rotates

Each Instruction assumes the carry is zero
MnemonicOperationValueBecomesCarry
RLRotate Left0000100100010010
RLCRotate Left with Carry00001001000100100
RRRotate Right0000100110000100
RRCRotate Right with Carry00001001000001001
SWAP Exchange the low and high nibbles1111000000001111

Bit-Level Boolean Operations

When dealing with one single bit, we have ANL, ORL, CPL, MOV, CLR, and SETB. MOV allows us to copy the bit (especially the Carry) into another location. CLR gives the bit the value zero, while SETB gives the bit the value of one.

Note: A slash ("/") preceding the operand in the assembly language indicates that the logical complement of the addressed bit is used as the source value, but the source bit itself is not affected.

Examples

2000:                   .org    0x2000
                   
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                   ;; Equates                                  ;;
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                   
2000:                      .equ    bank0, 00000000h
2000:                      .equ    bank1, 00001000b
2000:                      .equ    bank2, 00010000b
2000:                      .equ    bank3, 00011000b
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                   ;; Code                                      ;;
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                   
                   begin:
2000: A2 00                mov     C, 00h    ; clear the carry flag
2002: 92 7F                mov     7Fh, C    ; mov the carry into
                                             ; byte 2Fh see p.141
                   ;;
                   ;;  The next three instructions do the same
                   ;;    exact thing.
                   ;;
2004: 92 AF                mov     EA, C     ; IE.EA gets the carry
                                             ; see p.328
2006: 92 AF                mov     IE.7, C
2008: 43 A8 80             orl     IE, #80h  ;
                   
                   ;;
                   ;; end of string is null or high bit turned on
                   ;;
                   ;;  We are in external memory because of .org 2000h
                   ;;  so we have to use indirect and the A register.
                   ;;
200B: 90 20 2B             mov     DPTR, #nr
200E: E0                   movx    A,@DPTR
200F: 54 80                anl     A, #80h  ; If not EOS, A is
                                            ;  zero
                   ;;
                   ;; Just a "bit" more
                   ;;
2011: D2 D4                setb    RS1
                   ;        setb    PSW.RS1 ; This is an error
2013: D2 D4                setb    PSW.4
                   ;;
                   ;;      change register banks
                   ;;
                           ;; Each instruction is 1 byte, 1 cycle
                           ;; Total:  2 bytes, 2 cycles
2015: D2 D3                setb    RS0
2017: D2 D4                setb    RS1      ;  11 -- gives bank 3
                   
                           ;; Total 2 bytes, 1 cycle, but lousy style
2019: 43 D0 18             orl    PSW, #00011000b
                   
                           ;; same thing, but better style because it
                           ;; it is more obvious what you are trying
                           ;; to do.
201C: 43 D0 18             orl    PSW, #bank3
                   
                           ;; Dumb but doable -- 3 bytes, 3 cycles
201F: D3                   setb    C       ; 1 byte, 1 cycle
2020: 92 D3                mov     RS0, C  ; 2 bytes, 2 cycles
                   
                           ;; Toggle between bank 3 and bank 1
2022: B2 D3                cpl     RS0
                   ;;
                   ;; If we can set it, we can clear it
2024: C2 D3                clr     RS0
                   
                   ;; The last item is a point that is not really clear
                   ;; in the documentation.  The only difference is
                   ;; the slash in the front of the addressed bit.
                   ;; At run time, the bit is complemented and used
                   ;; in the OR operation, but the bit itself is not
                   ;; changed.
2026: A0 7F                orl     C, /7Fh
2028: 72 7F                orl     C, 7Fh
202A: 22                   ret
                   
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                   ;; Data                                      ;;
                   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202B: C1           nr:     .db     'A' + 80h



©2004, Gary L. Burt