Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.

Similar presentations


Presentation on theme: "Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1."— Presentation transcript:

1 Assembly 05

2 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1

3 Bit Mapping Assign special meaning to individual bits within bytes E.g., EFLAGS register 2

4 Bit Mapping Bit numbering starts at 0 for LSB, starts on right side Bit number increases going right to left 3 bit 0 bit number increases

5 Bit Mapping Many x86 instructions to manipulate individual bits Bitwise logical operations: and, or, xor, not Bit-shifting operations: shl, shr, … Bit rotation operations: ror, rol, … 4

6 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 5

7 Boolean Logic (review) Logical operations AND, OR, XOR, NOT Same logic as before (with gates) Compare individual bits… 6

8 Boolean Logic (review) For bitwise logical operations, each pair of bits get evaluated 7 01101100 AND 11011000 01001000 0 0 0 1 1 1

9 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 8

10 Bitwise Logic Mnemonics 9 andxor notor

11 and Mnemonic and -> logical AND two operands and al, bl;AND two 8-bit values, store in al and ax, bx;AND two 16-bit values, store in ax and eax, ebx; AND two 32-bit values, store in eax 10

12 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 11 Note that you can input binary numbers directly… al bl

13 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 12 01101100 al bl

14 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 13 01101100 al 11011000 bl

15 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 14 01001000 al 11011000 bl

16 or Mnemonic or->logical OR two operands or al, bl;OR two 8-bit values, store in al or ax, bx;OR two 16-bit values, store in ax or eax, ebx; OR two 32-bit values, store in eax 15

17 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 16 al bl

18 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 17 01101100 al bl

19 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 18 01101100 al 11011000 bl

20 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 19 11111100 al 11011000 bl

21 xor Mnemonic xor->logical XOR between two operands or al, bl;XOR two 8-bit values, store in al or ax, bx;XOR two 16-bit values, store in ax or eax, ebx; XOR two 32-bit values, store in eax 20

22 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 21 al bl

23 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 22 01101100 al bl

24 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 23 01101100 al 11011000 bl

25 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 24 10110100 al 11011000 bl

26 not Mnemonic not->logical NOT on single operand not al; NOT the 8-bit value, store in al not ax; NOT the 16-bit value, store in ax not eax; NOT the 32-bit value, store in eax 25

27 not Mnemonic mov al, 01101100b; not al; 26 al

28 not Mnemonic mov al, 01101100b; not al; 27 01101100 al

29 not Mnemonic mov al, 01101100b; not al; 28 10010011 al

30 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 29

31 Bit Masking Bit mask : used to isolate certain bits Use and instruction to mask bits Set unwanted bits to 0 Allows wanted bits to “pass through” 30

32 Bit Masking Example: isolate bits #4 and #5 mov al, 10011101b;value to inspect mov bl, 00110000b;bit mask and al, bl;isolate bits 31

33 Bit Masking Example: isolate bits #4 and #5 32 1 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 valuemaskresult LSB MSB AND=

34 Bit Masking Example: isolate bits #4 and #5 33 1 0 0 1 1 1 0 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 valuemaskresult LSB MSB mask allows two bits to “pass through”

35 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 34

36 Bit Shifting Shift bits to left or right Shift left => multiply by powers of 2 Shift right => divide by powers of 2 “New” bits are set to 0 (zero padding) Bits can “fall off” the left or right Bits that “fall off” are lost If you bump a 1 off the left side, carry flag (CF) will be set Numbers shifted as binary, not decimal or hex 35

37 Bit Shifting Example: Shift Left 1 Unit 36 01001100 01001100 zero padding MSB “falls off”

38 Bit Shifting Example: Shift Right 3 Units 37 01001100 01001000 these bits “fall off” “new” bits zero padded

39 Bit Shifting shl-> shift left shl, 38 register or memory always cl register or immediate value

40 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 39 al cl

41 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 40 00000001 al cl

42 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 41 00000001 4 al cl

43 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 42 00010000 4 al cl

44 Bit Shifting shr-> shift right shr, 43 register or memory always cl register or immediate value

45 Bit Shifting x: db 01000000b; declare x in.data section. shr byte [x], 3;shift [x] right 3 places ;(in.text section) 44

46 Bit Shifting x: db 01000000b shr byte [x], 3 45 01000000 x:

47 Bit Shifting x: db 01000000b shr byte [x], 3 46 00001000 x:

48 Bit Shifting (Rotate) Bits that “fall off” appear at other end E.g., rotate left by 3: 47 01001100 01100010

49 Bit Shifting (Rotate) rol-> rotate left rol, 48 register or memory always cl register or immediate value

50 Bit Shifting (Rotate) ror-> rotate right ror, 49 register or memory always cl register or immediate value

51 Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; 50 al ? CFCF

52 Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; 51 10000000 al ? CFCF

53 Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; 52 00000001 al 1 CFCF CF set

54 Bit Shifting (Rotate) rcl-> rotate left w/ carry flag (CF) - CF used as “extra” bit rcl, 53 register or memory always cl register or immediate value

55 Bit Shifting (Rotate) rcr-> rotate right w/ carry flag (CF) - CF used as “extra” bit rcr, 54 register or memory always cl register or immediate value

56 Bit Shifting (Rotate) E.g., rotate left 1 with carry (rcl) 55 101100100 CFCF 001100101 CFCF

57 Set / Clear Carry Flag (CF) How to manually clear or set CF? clc -> clear CF (takes no operands) stc -> set CF (takes no operands) 56

58 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 57 al ? CFCF bl

59 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 58 0 al ? CFCF bl

60 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 59 0 al ? CFCF 0 bl

61 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 60 0 al 1 CFCF 0 bl CF set

62 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 61 00000001 al 0 CFCF 0 bl

63 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 62 00000001 al 1 CFCF 0 bl

64 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 63 00000001 al 0 CFCF 10000000 bl

65 Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 64

66 Lookup Table Basically an array Declared / initialized in.data section Commas to separate array items Data access is NOT : array[ index ] Data access IS : [ array + index ] “array” is declared label name “index” is index into the array (either immediate value OR 32-bit register) 65

67 Lookup Table digits: db 8,6,7,5,3,0,9 ;declared in.data mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 66

68 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 67 al bl digits: 0123456 index

69 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 68 al bl 8675309 digits: 0123456 index

70 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 69 8 al bl 8675309 digits: 0123456 index

71 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 70 8 al 3 bl 8675309 digits: 0123456 index

72 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 71 8 al 3 bl 8675303 digits: 0123456 index


Download ppt "Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1."

Similar presentations


Ads by Google