| UMBC | CMSC 313 -- Boolean Operations Segment | Previous | Next |
| A | B | A and B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
1011 1010
AND 0000 1111
0000 1010
1111 1111
AND 0000 1111
0000 1111
| A | B | A or B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
1011 1010
OR 0000 1111
1011 1010
1111 1111
OR 0000 1111
0000 1111
| A | B | A xor B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
1011 1010
XOR 0000 1111
1011 0000
1111 1111
OR 0000 1111
1111 0000
| A | not A |
|---|---|
| 0 | 1 |
| 1 | 0 |
NOT 1011 1010
0100 0101
NOT 0000 1111
1111 0000
| and/or/xor | reg/mem, reg/mem/constant |
| not | reg/mem |
| 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 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
| 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
| 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