Comp 255 - Computer Organization
Intel 80x86 Boolean and Arithmetic Instructions


1. Shifts - Logical and Arithmetic

    A. Logical Shift

    Left: SHL dest, 1  or  SHL dest,cl

        |__| <- |__|__|__|__|__|__|__|__| <- 0
         CF      7  6  5  4  3  2  1  0

    Right: SHR dest, 1  or  SHR dest, cl

          0  -> |__|__|__|__|__|__|__|__| -> |__|
                 7  6  5  4  3  2  1  0       CF

    B. Arithmetic Shift

    Right: SAR dest, 1  or  SAR dest, cl

           <----+
         |      |
         +---> |__|__|__|__|__|__|__|__| -> |__|
                7  6  5  4  3  2  1  0       CF

        Note: Sign bit is propagated!

        Left: SAL is equivalent to SHL

    C. Details and Variants
        i. destination may be byte length or word length, register or memory.
        ii. with CL register as source, shift by amount in CL register

    D. Effect & Uses of Shifts
        i. SHL/SAL equivalent to multiplication by 2
        ii. SAR equivalent to division by 2
        iii. bit testing via CF (use jc or jnc)


2. Rotate

 A. Rotate Left: ROL dest, 1  or  ROL dest, cl

              + - - - - - - - - - - - - +
              |                         |
    |__| <- |__|__|__|__|__|__|__|__| <-+
     CF      7  6  5  4  3  2  1  0
 

 B. Rotate Right: ROR dest, 1  or  ROR dest, cl

        + - - - - - - - - - - - - +
        |                         |
        +-> |__|__|__|__|__|__|__|__| -> |__|
             7  6  5  4  3  2  1  0       CF

C. Details and Variants
    i. destination may be byte or word length, register or memory
    ii. with CL register as source, rotate by amount in CL register
    iii. "non-destructive shift"


3. Rotate Through Carry

A. Rotate through Carry Left: RCL dest, 1  or  RCL dest,cl

          + - - - - - - - - - - - - - - +
          |                             |
        |__| <- |__|__|__|__|__|__|__|__|
         CF      7  6  5  4  3  2  1  0
 

B. Rotate through Carry Right: RCR dest, 1  or  RCR dest, cl

          + - - - - - - - - - - - - - - - - +
          |                                 |
          +-> |__|__|__|__|__|__|__|__| -> |__|
               7  6  5  4  3  2  1  0       CF
 

C. Details and Variants
    i. destination may be byte or word length, register or memory
    ii. with CL register as source, rotate by amount in CL register


4. Boolean Operations

A.    AND destination, source
        TEST destination, source (non-destructive AND)
            i. using AND as Set to Zero
            ii. masking bits using source mask
            iii. testing bits
B.    OR destination, source
        XOR destination, source
            i. using OR as Set to One
            ii. using XOR as clear instruction
            iii. using XOR to test for bit pattern (followed by jz)
C.    AND, OR, XOR, and TEST affect SF, ZF, and PF
D.    NOT destination


5. Applications and Shifts, Rotates, and Boolean Operations

A.    Converting Integers to Strings of Hex Digits
 
; DX contains integer to display
 
    mov cx, 0004h ; Initialize loop counter to 4
Loop1:
    push cx  ; Save loop counter
 
; Rotate DX

    mov cl, 04   ; Set up rotation for 4 bits
    rol dx, cl   ;    and rotate DX left four bits
    push dx  ; Save DX
 
; Extract Right-Most Digit and Convert to ASCII

    and dl, 0Fh  ; Mask out upper 4 bits in DL
    or  dl, 30h  ;    and convert to ASCII code
    cmp dl, '9'  ; Is code > '9'
    jbe PrintIt  ; No - Print It
    add dl, 07h  ; Yes - Add 7 get ASCII 'A' .. 'F'

; Display Digit

PrintIt:
    mov ah, 02h  ; Display digit
    int 21h
    pop dx  ; Restore DX
    pop cx  ; Restore loop counter
    loop Loop1  ; Loop control

B    Packing Hex Digits into an Integer

    xor dx, dx

; Read a character

Loop2:
    mov ah, 01h  ; read character
    int 21h

; Is it between '0' and '9' ?

    cmp al, '0'  ; Is AL < '0'?
    jb Done   ;
    cmp al, '9'  ; Is AL <= '9'
    jbe Pack_EM
 
; Convert to lower case - Is AL between 'a' and 'f'?

    or  al, 00100000b ; Upper to Lower Case
    cmp al, 'a'  ; Is AL < 'a'?
    jb Done
    cmp al 'f'  ; Is AL > 'f'
    ja Done
    add al, 09h  ; Convert ASCII 61h..66h to 6A..6Fh

; Pack Integer Value into Right 4 bits of DX

Pack_EM:
    mov cx, 04h  ; Left shift DX
    shl dx, cl  ;    4 bits
    and ax, 000Fh ; Convert ASCII to integer
    or  dx, ax  ; Insert new digit into DX
    jmp Loop2
Done:

;  DX contains integer value
 


6. Unsigned Arithmetic Operations

A. ADD destination, source
B. SUB destination, source
C. MUL operand (Note - operand must be register or memory; no immediate mode!)
        i. If operand is type Byte
            AL is other source
            AX is destination
        ii. Else If operand is type Word
            AX is other operand
            DX, AX pair is destination
D. DIV operand (Note - operand must be register or memory; no immediate mode!)
        i If operand is type Byte
            AX (AH:AL pair) is dividend
            AH is remainder
            AL is quotient
        ii. Else if operand is type Word
            DX, AX pair is dividend
            DX is remainder
            AX is quotient
        iii. INT 00h - Division Overflow Interrupt
            To test for division overflow make sure  divisor > high order dividend



7. Signed Arithmetic Operations

A.    ADD and SUB; no change
B.    Signed Multiplication : IMUL
        i. sign propagation thru high order byte (word)
C.    Signed Division : IDIV
        i. use CBW (or CWD) to extend sign of AL thru AX (or AX thru DX)
        ii. non-integral quotients truncated toward 0
        iii. remainder is same sign as dividend
        iv. abs(remainder) < abs(divisor)