Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting...
-
Upload
kory-lindsey -
Category
Documents
-
view
222 -
download
2
Transcript of Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting...
Assembly 05
2
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
3
Bit Mapping
• Assign special meaning to individual bits within bytes• E.g., EFLAGS register
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
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, …
6
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
7
Boolean Logic (review)
• Logical operations AND, OR, XOR, NOT• Same logic as before (with gates)• Compare individual bits…
8
Boolean Logic (review)
• For bitwise logical operations, each pair of bits get evaluated
01101100AND
1101100001001000
00 0
11 1
9
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
10
Bitwise Logic Mnemonics
and xor
notor
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
12
and Mnemonic
mov al, 01101100b;mov bl, 11011000b;and al, bl;
Note that you can input binary numbers directly…
al
bl
13
and Mnemonic
mov al, 01101100b;mov bl, 11011000b;and al, bl;
01101100al
bl
14
and Mnemonic
mov al, 01101100b;mov bl, 11011000b;and al, bl;
01101100al
11011000bl
15
and Mnemonic
mov al, 01101100b;mov bl, 11011000b;and al, bl;
01001000al
11011000bl
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
17
or Mnemonic
mov al, 01101100b;mov bl, 11011000b;or al, bl;
al
bl
18
or Mnemonic
mov al, 01101100b;mov bl, 11011000b;or al, bl;
01101100al
bl
19
or Mnemonic
mov al, 01101100b;mov bl, 11011000b;or al, bl;
01101100al
11011000bl
20
or Mnemonic
mov al, 01101100b;mov bl, 11011000b;or al, bl;
11111100al
11011000bl
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
22
xor Mnemonic
mov al, 01101100b;mov bl, 11011000b;xor al, bl;
al
bl
23
xor Mnemonic
mov al, 01101100b;mov bl, 11011000b;xor al, bl;
01101100al
bl
24
xor Mnemonic
mov al, 01101100b;mov bl, 11011000b;xor al, bl;
01101100al
11011000bl
25
xor Mnemonic
mov al, 01101100b;mov bl, 11011000b;xor al, bl;
10110100al
11011000bl
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
27
not Mnemonic
mov al, 01101100b;not al;
al
28
not Mnemonic
mov al, 01101100b;not al;
01101100al
29
not Mnemonic
mov al, 01101100b;not al;
10010011al
30
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
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”
32
Bit Masking
• Example: isolate bits #4 and #5
mov al, 10011101b; value to inspectmov bl, 00110000b; bit maskand al, bl; isolate bits
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 =
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”
35
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
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
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”
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
39
Bit Shifting
shl -> shift left
shl <operand>, <count>
register or memory always cl register or
immediate value
40
Bit Shifting
mov al, 00000001b;mov cl, 4;shl al, cl;
al
cl
41
Bit Shifting
mov al, 00000001b;mov cl, 4;shl al, cl;
00000001al
cl
42
Bit Shifting
mov al, 00000001b;mov cl, 4;shl al, cl;
00000001
4
al
cl
43
Bit Shifting
mov al, 00000001b;mov cl, 4;shl al, cl;
00010000
4
al
cl
44
Bit Shifting
shr -> shift right
shr <operand>, <count>
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)
46
Bit Shifting
x: db 01000000b
shr byte [x], 3
01000000x:
47
Bit Shifting
x: db 01000000b
shr byte [x], 3
00001000x:
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
49
Bit Shifting (Rotate)
rol -> rotate left
rol <operand>, <count>
register or memory always cl register or
immediate value
50
Bit Shifting (Rotate)
ror -> rotate right
ror <operand>, <count>
register or memory always cl register or
immediate value
51
Bit Shifting (Rotate)
mov al, 10000000b;rol al, 1;
al
?CF
52
Bit Shifting (Rotate)
mov al, 10000000b;rol al, 1;
10000000al
?CF
53
Bit Shifting (Rotate)
mov al, 10000000b;rol al, 1;
00000001al
1CF
CF set
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
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
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
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)
58
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
al
?CF
bl
59
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
0al
?CF
bl
60
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
0al
?CF
0bl
61
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
0al
1CF
0bl
CF set
62
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
00000001al
0CF
0bl
63
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
00000001al
1CF
0bl
64
Bit Shifting (Rotate)
mov al, 0;mov bl, 0;stc;rcl al,1;stc;rcr bl,1;
00000001al
0CF
10000000bl
65
Outline
• Bit mapping• Boolean logic (review)• Bitwise logic• Bit masking• Bit shifting• Lookup table
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)
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;
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
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
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
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
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