Sei sulla pagina 1di 32

Chapter 6

Bit Manipulation
6.1 Logical Operations
Boolean Operations
Design and high-level languages
Boolean values true and false
Boolean variables
Boolean operations and, or, exclusive or
and not
80x86
Uses 1 bit for true and 0 bit for false
Has and, or, xor and not instructions

and Operation
bit1 bit2 bit1 and bit2
0 0 0
0 1 0
1 0 0
1 1 1
or Operation
bit1 bit2 bit1 or bit2
0 0 0
0 1 1
1 0 1
1 1 1
xor Operation
bit1 bit2 bit1 xor bit2
0 0 0
0 1 1
1 0 1
1 1 0
only difference from or
not Operation
bit not bit
0 1
1 0
not is the 1s complement operation, 1- bit
80x86 Instruction Formats
and destination, source
or destination, source
xor destination, source
not destination
Operands can be doublewords, words or
bytes
Destination in memory or register
Source in memory, in register or
immediate
Logical Instruction Execution
For and, or and xor, the logical operation
is performed on pairs of bits in
corresponding positions from the two
operands
This produces 32, 16 or 8 bits that replace
the value at destination
For not, the 1s complement of each
destination bit replaces the original bit

Logical Instructions and Flags
not doesnt change flags
and, or and xor alter some flags,
including
Sign flag SF and the zero flag ZF are set or
reset according to the value of the result of
the operation
Carry flag CF and overflow flag OF flags are
both always reset to 0
and Example
Before
AX: E275 CX: A9D7
Instruction
and ax,cx
Operation on pairs of bits
1110 0010 0111 0101
1010 1001 1101 0111
1010 0000 0101 0101
After
AX: A055 SF 1 ZF 0
or Example
Before
DX: E275 value: A9D7
Instruction
or dx,value
Operation on pairs of bits
1110 0010 0111 0101
1010 1001 1101 0111
1110 1011 1111 0111
After
DX: EBF7 SF 1 ZF 0
xor Example
Before
BX: E275
Instruction
xor bx,0a9d7h
Operation on pairs of bits
1110 0010 0111 0101
1010 1001 1101 0111
0100 1011 1010 0010
After
BX: 4BA2 SF 0 ZF 0
not Example
Before
AX: E275
Instruction
not ax
Operation on bits
1110 0010 0111 0101
0001 1101 1000 1010
After
AX: 1D8A SF & ZF unchanged
Using and to Mask Selected Bits
Suppose you want to make bits 8-15 of
EAX zero without changing other bits
Use
and eax, 0ffffff0fh
This ands with 0 bits where you want to
ensure bits are 0 and with 1 bits where
they arent to be changed

Using or to Set Selected Bits
Suppose you want to set bit 2 of AL to 1
without changing other bits
Use
or al, 00000100b
This ors with a 1 bit in position 2 to
ensure this bit becomes a 1 and with 0 bits
in all other bit positions so that they are
unchanged

Implementing High Level Language
Boolean Operations
Bits of an operand can be interpreted as
discrete true/false values
Use or to change selected bits to 1 (true)
without affecting other bits
Use and to change selected bits to 0
(false) without affecting other bits
Use xor to flip selected bits without
affecting other bits
Using and to Implement the
Modulus Operation
Suppose you want to calculate value mod 16
where value is an unsigned number in EAX
Use and eax, 0000000fh
This works since bits 4-31 represent parts of
value that are divisible by 2
4
=16, so that the
remainder upon division by 16 comes from the
low order four bits
Similar technique with appropriate mask works
for any power of 2

Character-Integer Conversions
If AL contains the ASCII code for a digit
(30
16
to 39
16
), then
and eax, 0000000fh
gives the corresponding doubleword integer
value in EAX
If EAX contains 00000000 to 00000009, then
or al, 30h
gives the corresponding ASCII character in AL
test Instruction
test destination, source
performs an and operation, but only to set
the flags the result is not stored at
destination
Similar to the cmp instruction that performs
a subtraction operation, but only to set
flags
6.2 Shift and Rotate
Instructions
Shift Instructions
Slide bits left or
right in a register or
memory location
Types of shifts
left and right
logical and
arithmetic
Doubleword, word
or byte operands

left right
logical
shl shr
arithmetic
sal sar
Shift Formats
s-- destination, 1
shift of exactly one position within the
destination location
s-- destination, immediate8
shift by the number of positions specified in
the immediate operand
s-- destination, cl
shift by the number of positions specified by
the value in CL
Logical Shifts
Specified number of bits slides off one end
Other end filled by 0 bits
Last bit shifted off saved in CF
SF and ZF set according to result in destination
OF
for single bit shift, set to 1 if sign bit of result is different
from sign bit of original value; reset to 0 if same
undefined for multibit shifts

shl Example
Before
CX: A9D7
Instruction
shl cx,1
Operation on pairs of bits
1010 1001 1101 0111

0101 0011 1010 1110
After
CX: 53AE SF 0 CF 1 ZF 0 OF 1
shifted off
fill
shr Example
Before
DX: A9D7
Instruction
shr dx,4
Operation on pairs of bits
1010 1001 1101 0111

0000 1010 1001 1101
After
DX: 0A9D SF 0 CF 0 ZF 0 OF ?
shifted off
fill
Arithmetic Shifts
sal same as shl
For sar
Specified number of bits slides off right end
Other end filled by copies of original sign bit
Last bit shifted off saved in CF
SF and ZF set according to result in
destination
OF
for single bit shift, OF cleared to 0
undefined for multibit shifts

sar Example
Before
BX: A9D7
Instruction
sar bx,1
Operation on pairs of bits
1010 1001 1101 0111

1101 0100 1110 1011
After
BX: D4EB SF 1 CF 1 ZF 0 OF 0
shifted off
fill
sign bit
Using Shifts for
Multiplication and Division
Suppose value is an unsigned number in
EAX
shl eax,1 multiplies value by 2
shl eax,n multiplies value by 2
n
shr eax,1 divides value by 2
shr eax,n divides value by 2
n
more efficient than mult or div
instructions

Rotate Instructions
Similar to shifts, except that bits that fall off
one end are used to fill at the other end
rol used to rotate left
ror used to rotate right
Same operands as for shifts


ror Example
Before
BX: A9D7
Instruction
ror bx,1
Operation on pairs of bits

1010 1001 1101 0111

1101 0100 1110 1011
After
BX: D4EB
Other Applications
Shift and rotate instructions can be used in
conjunction with logical instructions to
manipulate bits within a byte, word or
doubleword

Potrebbero piacerti anche