Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Bit Manipulation. 7.1 Logical Operations.

Similar presentations


Presentation on theme: "Chapter 7 Bit Manipulation. 7.1 Logical Operations."— Presentation transcript:

1 Chapter 7 Bit Manipulation

2 7.1 Logical Operations

3 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

4 and Operation bit1bit2bit1 and bit2 000 010 100 111

5 or Operation bit1bit2bit1 or bit2 000 011 101 111

6 xor Operation bit1bit2bit1 xor bit2 000 011 101 110 only difference from or

7 not Operation bitnot bit 01 10 not is the 1’s complement operation, 1- bit

8 80x86 Instruction Formats and destination, source or destination, source xor destination, source not destination Operands can be bytes, words, doublewords or quadwords Destination in memory or register Source in memory, in register or immediate

9 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

10 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

11 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

12 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

13 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

14 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

15 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 aren’t to be changed

16 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

17 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

18 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

19 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

20 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

21 7.2 Shift and Rotate Instructions

22 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 leftright logical shlshr arithmetic salsar

23 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

24 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

25 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 (copied to CF) fill

26 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

27 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

28 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

29 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 mul or div instructions

30 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

31 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

32 Other Applications Shift and rotate instructions can be used in conjunction with logical instructions to manipulate bits within a byte, word, doubleword or quadword

33 7.3 Converting an ASCII String to a 2's Complement Integer

34 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

35 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;

36 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)


Download ppt "Chapter 7 Bit Manipulation. 7.1 Logical Operations."

Similar presentations


Ads by Google