UMBC CMSC 211

UMBC | CSEE


Introduction

Computer hardware often requires that several pieces of information be encoded into a single word or byte. Many programs also pack the information to save memory, disk space or communications time. There for bit operations consist of extracting or altering packed information, with either Boolean operatons and shifts.

Boolean Operations

There are four Boolean operations that are the same as in the bitwise logical operators in C: AND, OR, XOR (eXclusive OR), and NOT. These operations can be expressed in the form of truth tables, where false is zero and true is one:

AND Truth Table

A B A and B
0 0 0
0 1 0
1 0 0
1 1 1

Examples

        1011 1010
    AND 0000 1111
        0000 1010
  

        1111 1111
    AND 0000 1111
        0000 1111
  

OR Truth Table

A B A or B
0 0 0
0 1 1
1 0 1
1 1 1

Examples

        1011 1010
    OR  0000 1111
        1011 1010
  

        1111 1111
    OR  0000 1111
        0000 1111
  

XOR Truth Table

A B A xor B
0 0 0
0 1 1
1 0 1
1 1 0

Examples

        1011 1010
    XOR  0000 1111
        1011 0000
  

        1111 1111
    OR  0000 1111
        1111 0000
  

NOT Truth Table

A not A
0 1
1 0

Examples

   NOT 1011 1010
       0100 0101
  

   NOT 0000 1111
       1111 0000
  

Format

These instructions come in the form (No more than one operand is a memory reference):
and/or/xor reg/mem, reg/mem/constant
not reg/mem
The functionality is:
and dest, source ; dest := dest AND source
or dest, source ; dest := dest OR source
xor dest, source ; dest := dest XOR source
not dest ; dest := NOT dest
The not operation is a 1's complement.

The and operation is used to clear bits and the or operation is used to set bits. The xor operation is used to toggle bits to the opposite set. The source is known as the mask for the operation.

AND,OR, XOR set the zero and sign flags according to the result and the overflow and carry flags are cleared.

The most common usage is to AND with a mask and then do a jz to determine if any of the bits corresponding to the mask were a 1. It is so common that they built a special instruction for it:

test reg/mem, reg/mem/constant
right to left, starting with zero. That means that bit n has the numerical value of 2n.

An Example

We have already talked about how the keyboard hardware does not know anything about ASCII code, it just knows about scan codes. It is simply a number (using the low seven bits in a byte). The high bit is used to show if the key was pressed (set to 0) or released (set to 1). Then the BIOS (BASIC Input-Output System) has to do its magic. In addition to keeping track of which key was pressed, the BIOS also keeps track of additional information which is stored at memory address 0417h. This is the keyboard status information:

Bit Mask Interpretation
7 10000000B Insert mode on
6 01000000B Caps lock on
5 00100000B Num lock on
4 00010000B Scroll lock on
3 00001000B Alt key down (either if more than one)
2 00000100B Ctrl key down (either if more than one)
1 00000010B Left shift key down
0 00000001B Right shift key down

If the state is on (Insert mode is set) then the corresponding bit in the keyboard status will be true (1). Forunately for us, the BIOS does all the work to keep the keyboard status up-to-date.

Suppose that we are writing a program where we will do one thing if a certain key is pressed, but do something slightly different if the combination of Alt and a certain key is press. We will want to set a flag based on the Alt key and then proceed with the processing. We could do something like:

.MODEL small
.STACK 100h
ALTKEY EQU 00001000B
.DATA
kbdStatus DB ?
altFlag DB 0 ; Initialized to off
.CODE
example1 PROC
mov ax, @data
mov ds, ax
; ... ; Do something to get the keyboard status
mov ax, kbdStatus
and ax, ALTKEY
jne noAlt
mov bx, 1 ; Set the flag because the Alt key was pressed
mov bx, altFlag
noAlt:
mov al,0
mov ah, 4ch
int 21h
example1 ENDP
END example1

Another Example

The directory information for a disk file constains the date of last modification in a single work in the following format:
year - 1900 bits 9 - 15 (leftmost seven bits)
month bits 5 - 8 (January is 1)
day bits 0 - 4 (rightmost five bits)

Now we can do the following:

.MODEL small
.STACK 100h
YEARMASK EQU 1111111000000000B
MONTHMASK EQU 0000000111100000B
DAYMASK EQU 0000000000011111B
.DATA
modDate DW ? ; The year is still in bits 9 - 15
funnyYear DW ? ; The month is still in bits 5 - 8
funnyMonth DW ? ; The day is in bits 0 - 4 and
; all other bits are off
OKDay DW ?
.CODE
example2 PROC
mov ax, @data
mov ds, ax
; ... ; Do something to get the
; modification date
mov ax, modDate
and ax, YEARMASK
mov funnyYear, ax
mov ax, modDate
and ax, MONTHMASK
mov funnyMonth, ax
mov ax, modDate
and ax, DAYMASK
mov ax, OKDay
; At this point, the values
; are not usualable yet
mov al,0
mov ah, 4ch
int 21h
example2 ENDP
END example2


UMBC | CSEE