assembly 05. outline bit mapping boolean logic (review) bitwise logic bit masking bit shifting...

72
Assembly 05

Upload: kory-lindsey

Post on 19-Jan-2016

222 views

Category:

Documents


2 download

TRANSCRIPT

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

Assembly 05

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

2

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

Page 3: Assembly 05. 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

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

4

Bit Mapping

• Bit numbering starts at 0 for LSB, starts on right side• Bit number increases going right to left

bit 0

bit number increases

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

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, …

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

6

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

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

7

Boolean Logic (review)

• Logical operations AND, OR, XOR, NOT• Same logic as before (with gates)• Compare individual bits…

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

8

Boolean Logic (review)

• For bitwise logical operations, each pair of bits get evaluated

01101100AND

1101100001001000

00 0

11 1

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

9

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

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

10

Bitwise Logic Mnemonics

and xor

notor

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

11

and Mnemonic

and -> logical AND two operands

and al, bl; AND two 8-bit values, store in aland ax, bx; AND two 16-bit values, store in axand eax, ebx; AND two 32-bit values, store in eax

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

12

and Mnemonic

mov al, 01101100b;mov bl, 11011000b;and al, bl;

Note that you can input binary numbers directly…

al

bl

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

13

and Mnemonic

mov al, 01101100b;mov bl, 11011000b;and al, bl;

01101100al

bl

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

14

and Mnemonic

mov al, 01101100b;mov bl, 11011000b;and al, bl;

01101100al

11011000bl

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

15

and Mnemonic

mov al, 01101100b;mov bl, 11011000b;and al, bl;

01001000al

11011000bl

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

16

or Mnemonic

or -> logical OR two operands

or al, bl; OR two 8-bit values, store in alor ax, bx; OR two 16-bit values, store in axor eax, ebx; OR two 32-bit values, store in eax

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

17

or Mnemonic

mov al, 01101100b;mov bl, 11011000b;or al, bl;

al

bl

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

18

or Mnemonic

mov al, 01101100b;mov bl, 11011000b;or al, bl;

01101100al

bl

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

19

or Mnemonic

mov al, 01101100b;mov bl, 11011000b;or al, bl;

01101100al

11011000bl

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

20

or Mnemonic

mov al, 01101100b;mov bl, 11011000b;or al, bl;

11111100al

11011000bl

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

21

xor Mnemonic

xor -> logical XOR between two operands

or al, bl; XOR two 8-bit values, store in alor ax, bx; XOR two 16-bit values, store in axor eax, ebx; XOR two 32-bit values, store in eax

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

22

xor Mnemonic

mov al, 01101100b;mov bl, 11011000b;xor al, bl;

al

bl

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

23

xor Mnemonic

mov al, 01101100b;mov bl, 11011000b;xor al, bl;

01101100al

bl

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

24

xor Mnemonic

mov al, 01101100b;mov bl, 11011000b;xor al, bl;

01101100al

11011000bl

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

25

xor Mnemonic

mov al, 01101100b;mov bl, 11011000b;xor al, bl;

10110100al

11011000bl

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

26

not Mnemonic

not -> logical NOT on single operand

not al; NOT the 8-bit value, store in alnot ax; NOT the 16-bit value, store in axnot eax; NOT the 32-bit value, store in eax

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

27

not Mnemonic

mov al, 01101100b;not al;

al

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

28

not Mnemonic

mov al, 01101100b;not al;

01101100al

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

29

not Mnemonic

mov al, 01101100b;not al;

10010011al

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

30

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

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

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”

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

32

Bit Masking

• Example: isolate bits #4 and #5

mov al, 10011101b; value to inspectmov bl, 00110000b; bit maskand al, bl; isolate bits

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

33

Bit Masking

• Example: isolate bits #4 and #5

1

0

0

1

1

1

0

1

0

0

1

1

0

0

0

0

value mask result

LSB

MSB

AND =

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

34

Bit Masking

• Example: isolate bits #4 and #5

1

0

0

1

1

1

0

1

0

0

1

1

0

0

0

0

0

0

0

1

0

0

0

0

value mask result

LSB

MSB

mask allows two bits to

“pass through”

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

35

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

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

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

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

37

Bit Shifting Example: Shift Left 1 Unit

0 1 0 0 1 1 0 0

01 0 0 1 1 0 0

zero paddingMSB “falls off”

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

38

Bit Shifting Example: Shift Right 3 Units

0 1 0 0 1 1 0 0

0 1 0 0 10 0 0

these bits “fall off”

“new” bits zero padded

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

39

Bit Shifting

shl -> shift left

shl <operand>, <count>

register or memory always cl register or

immediate value

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

40

Bit Shifting

mov al, 00000001b;mov cl, 4;shl al, cl;

al

cl

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

41

Bit Shifting

mov al, 00000001b;mov cl, 4;shl al, cl;

00000001al

cl

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

42

Bit Shifting

mov al, 00000001b;mov cl, 4;shl al, cl;

00000001

4

al

cl

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

43

Bit Shifting

mov al, 00000001b;mov cl, 4;shl al, cl;

00010000

4

al

cl

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

44

Bit Shifting

shr -> shift right

shr <operand>, <count>

register or memory always cl register or

immediate value

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

45

Bit Shifting

x: db 01000000b; declare x in .data section..shr byte [x], 3; shift [x] right 3 places

; (in .text section)

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

46

Bit Shifting

x: db 01000000b

shr byte [x], 3

01000000x:

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

47

Bit Shifting

x: db 01000000b

shr byte [x], 3

00001000x:

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

48

Bit Shifting (Rotate)

• Bits that “fall off” appear at other end• E.g., rotate left by 3:

0 1 0 0 1 1 0 0

0 1 1 0 0 0 1 0

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

49

Bit Shifting (Rotate)

rol -> rotate left

rol <operand>, <count>

register or memory always cl register or

immediate value

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

50

Bit Shifting (Rotate)

ror -> rotate right

ror <operand>, <count>

register or memory always cl register or

immediate value

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

51

Bit Shifting (Rotate)

mov al, 10000000b;rol al, 1;

al

?CF

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

52

Bit Shifting (Rotate)

mov al, 10000000b;rol al, 1;

10000000al

?CF

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

53

Bit Shifting (Rotate)

mov al, 10000000b;rol al, 1;

00000001al

1CF

CF set

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

54

Bit Shifting (Rotate)

rcl -> rotate left w/ carry flag (CF)- CF used as “extra” bit

rcl <operand>, <count>

register or memory always cl register or

immediate value

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

55

Bit Shifting (Rotate)

rcr -> rotate right w/ carry flag (CF)- CF used as “extra” bit

rcr <operand>, <count>

register or memory always cl register or

immediate value

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

56

Bit Shifting (Rotate)

• E.g., rotate left 1 with carry (rcl)

1 0 1 1 0 0 1 00

CF

00 1 1 0 0 1 01

CF

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

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)

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

58

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

al

?CF

bl

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

59

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

0al

?CF

bl

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

60

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

0al

?CF

0bl

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

61

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

0al

1CF

0bl

CF set

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

62

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

00000001al

0CF

0bl

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

63

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

00000001al

1CF

0bl

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

64

Bit Shifting (Rotate)

mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;

00000001al

0CF

10000000bl

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

65

Outline

• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table

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

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)

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

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;

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

68

Lookup Table

digits: db 8,6,7,5,3,0,9

mov al, [digits];mov bl, [digits + 4];mov [digits + 6], bl;

al

bl

digits:

0 1 2 3 4 5 6index

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

69

Lookup Table

digits: db 8,6,7,5,3,0,9

mov al, [digits];mov bl, [digits + 4];mov [digits + 6], bl;

al

bl

8 6 7 5 3 0 9digits:

0 1 2 3 4 5 6index

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

70

Lookup Table

digits: db 8,6,7,5,3,0,9

mov al, [digits];mov bl, [digits + 4];mov [digits + 6], bl;

8al

bl

8 6 7 5 3 0 9digits:

0 1 2 3 4 5 6index

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

71

Lookup Table

digits: db 8,6,7,5,3,0,9

mov al, [digits];mov bl, [digits + 4];mov [digits + 6], bl;

8al

3bl

8 6 7 5 3 0 9digits:

0 1 2 3 4 5 6index

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

72

Lookup Table

digits: db 8,6,7,5,3,0,9

mov al, [digits];mov bl, [digits + 4];mov [digits + 6], bl;

8al

3bl

8 6 7 5 3 0 3digits:

0 1 2 3 4 5 6index