Chapter 7 Bit Manipulation. 7.1 Logical Operations.
-
Upload
justin-arnold -
Category
Documents
-
view
261 -
download
0
description
Transcript of Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Chapter 7Bit Manipulation
7.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 1’s complement operation, 1- bit
80x86 Instruction Formatsand destination, sourceor destination, sourcexor destination, sourcenot destination
• Operands can be bytes, words, doublewords or quadwords
• 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 8, 16, 32 or 64 bits that replace the value at destination
• For not, the 1’s complement of each destination bit replaces the original bit
Logical Instructions and Flags
• not doesn’t 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 • Instructionand ax,cx
• Operation on pairs of bits1110 0010 0111 01011010 1001 1101 01111010 0000 0101 0101
• AfterAX: A055 SF 1 ZF 0
or Example• Before
DX: E275 value: A9D7 • Instructionor dx,value
• Operation on pairs of bits1110 0010 0111 01011010 1001 1101 01111110 1011 1111 0111
• AfterDX: EBF7 SF 1 ZF 0
xor Example• Before
BX: E275 • Instructionxor bx,0a9d7h
• Operation on pairs of bits1110 0010 0111 01011010 1001 1101 01110100 1011 1010 0010
• AfterBX: 4BA2 SF 0 ZF 0
not Example• Before
AX: E275 • Instructionnot ax
• Operation on bits1110 0010 0111 01010001 1101 1000 1010
• AfterAX: 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
• Useand eax, 0ffffff0fh
• This “ands” with 0 bits where you want to ensure bits are 0 and with 1 bits where they aren’t to be changed
Using or to Set Selected Bits• Suppose you want to set bit 2 of AL to 1
without changing other bits• Useor 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 24=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
(3016 to 3916), thenand eax, 0000000fhgives the corresponding doubleword integer value in EAX
• If EAX contains 00000000 to 00000009, thenor al, 30hgives the corresponding ASCII character in AL
test Instruction
• test destination, sourceperforms 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
7.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• Byte, word,
doubleword or quadword 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 • Instructionshl cx,1
• Operation on pairs of bits1010 1001 1101 0111
0101 0011 1010 1110
• AfterCX: 53AE SF 0 CF 1 ZF 0 OF 1
shifted off (copied to CF) fill
shr Example• Before
DX: A9D7 • Instructionshr dx,4
• Operation on pairs of bits1010 1001 1101 0111
0000 1010 1001 1101
• AfterDX: 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 • Instructionsar bx,1
• Operation on pairs of bits1010 1001 1101 0111
1101 0100 1110 1011
• AfterBX: 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 EAXshl eax,1 multiplies value by 2shl eax,n multiplies value by 2n
shr eax,1 divides value by 2shr eax,n divides value by 2n
• more efficient than mul 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 • Instructionror 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, doubleword or quadword
7.3 Converting an ASCII String to a 2's Complement Integer
atod Macro
• Expands to code that calls atodproc– Parameter is the address of the string of
ASCII characters to be scanned– atodproc returns value in EAX
Basic Design of atodproc
value :=0;while pointing at code for a digit loop
multiply value by 10;convert ASCII character code to integer;add integer to value;point at next byte in memory;
end while;
Additional Features of atodproc
• Scan skips over leading spaces• If first non-space is a +, it is ignored• If first non-space is a -, a multiplier of -1 is
applied after the basic algorithm is finished• Results of atodproc are not meaningful if
the string does not represent a valid integer (e.g., too many digits or two leading signs)