Assembly 05 Outline • • • • • • 1 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table Bit Mapping • Assign special meaning to individual bits within bytes • E.g., EFLAGS register 2 Bit Mapping • Bit numbering starts at 0 for LSB, starts on right side • Bit number increases going right to left bit number increases bit 0 3 Bit Mapping • • • • 4 Many x86 instructions to manipulate individual bits Bitwise logical operations: and, or, xor, not Bit-shifting operations: shl, shr, … Bit rotation operations: ror, rol, … Outline • • • • • • 5 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table Boolean Logic (review) • Logical operations AND, OR, XOR, NOT • Same logic as before (with gates) • Compare individual bits… 6 Boolean Logic (review) • For bitwise logical operations, each pair of bits get evaluated 01101100 AND 11011000 01001000 7 0 0 0 1 1 1 Outline • • • • • • 8 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table Bitwise Logic Mnemonics 9 and xor or not and Mnemonic and and al, bl; and ax, bx; and eax, ebx; 10 -> logical AND two operands AND two 8-bit values, store in al AND two 16-bit values, store in ax AND two 32-bit values, store in eax and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; al bl Note that you can input binary numbers directly… 11 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 12 al bl 01101100 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 13 al 01101100 bl 11011000 and Mnemonic mov al, 01101100b; mov bl, 11011000b; and al, bl; 14 al 01001000 bl 11011000 or Mnemonic or or al, bl; or ax, bx; or eax, ebx; 15 -> logical OR two operands OR two 8-bit values, store in al OR two 16-bit values, store in ax OR two 32-bit values, store in eax or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 16 al bl or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 17 al bl 01101100 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 18 al 01101100 bl 11011000 or Mnemonic mov al, 01101100b; mov bl, 11011000b; or al, bl; 19 al 11111100 bl 11011000 xor Mnemonic xor or al, bl; or ax, bx; or eax, ebx; 20 -> logical XOR between two operands XOR two 8-bit values, store in al XOR two 16-bit values, store in ax XOR two 32-bit values, store in eax xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 21 al bl xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 22 al bl 01101100 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 23 al 01101100 bl 11011000 xor Mnemonic mov al, 01101100b; mov bl, 11011000b; xor al, bl; 24 al 10110100 bl 11011000 not Mnemonic not not al; not ax; not eax; 25 -> logical NOT on single operand NOT the 8-bit value, store in al NOT the 16-bit value, store in ax NOT the 32-bit value, store in eax not Mnemonic mov al, 01101100b; not al; 26 al not Mnemonic mov al, 01101100b; not al; 27 al 01101100 not Mnemonic mov al, 01101100b; not al; 28 al 10010011 Outline • • • • • • 29 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table Bit Masking • • • • 30 Bit mask : used to isolate certain bits Use and instruction to mask bits Set unwanted bits to 0 Allows wanted bits to “pass through” Bit Masking • Example: isolate bits #4 and #5 mov al, 10011101b; mov bl, 00110000b; and al, bl; 31 value to inspect bit mask isolate bits Bit Masking • Example: isolate bits #4 and #5 MSB 1 0 0 0 0 1 1 LSB 32 AND 1 1 0 1 0 0 0 1 0 value mask = result Bit Masking • Example: isolate bits #4 and #5 MSB LSB 33 1 0 0 0 0 0 0 1 0 1 1 1 1 0 1 0 0 0 0 1 0 0 value mask result mask allows two bits to “pass through” 0 0 Outline • • • • • • 34 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 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 Bit Shifting Example: Shift Left 1 Unit 0 1 0 0 1 1 0 0 1 0 0 1 1 0 0 0 MSB “falls off” 36 zero padding Bit Shifting Example: Shift Right 3 Units 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 1 these bits “fall off” “new” bits zero padded 37 Bit Shifting shl -> shift left shl <operand>, <count> register or memory 38 always cl register or immediate value Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; al cl 39 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; al cl 40 00000001 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 41 al 00000001 cl 4 Bit Shifting mov al, 00000001b; mov cl, 4; shl al, cl; 42 al 00010000 cl 4 Bit Shifting shr -> shift right shr <operand>, <count> register or memory 43 always cl register or immediate value Bit Shifting x: db 01000000b; . . shr byte [x], 3; ; 44 declare x in .data section shift [x] right 3 places (in .text section) Bit Shifting x: db 01000000b shr byte [x], 3 45 x: 01000000 Bit Shifting x: db 01000000b shr byte [x], 3 46 x: 00001000 Bit Shifting (Rotate) • Bits that “fall off” appear at other end • E.g., rotate left by 3: 47 0 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0 Bit Shifting (Rotate) rol -> rotate left rol <operand>, <count> register or memory 48 always cl register or immediate value Bit Shifting (Rotate) ror -> rotate right ror <operand>, <count> register or memory 49 always cl register or immediate value Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; al C F 50 ? Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; al C F 51 10000000 ? Bit Shifting (Rotate) mov al, 10000000b; rol al, 1; al C F CF set 52 00000001 1 Bit Shifting (Rotate) rcl -> rotate left w/ carry flag (CF) - CF used as “extra” bit rcl <operand>, <count> register or memory 53 always cl register or immediate value Bit Shifting (Rotate) rcr -> rotate right w/ carry flag (CF) - CF used as “extra” bit rcr <operand>, <count> register or memory 54 always cl register or immediate value Bit Shifting (Rotate) • E.g., rotate left 1 with carry (rcl) 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 0 0 C F 1 C F 55 Set / Clear Carry Flag (CF) • How to manually clear or set CF? clc stc 56 -> -> clear CF (takes no operands) set CF (takes no operands) Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 57 al bl C F ? Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 58 al 0 bl C F ? Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 59 al 0 bl 0 C F ? Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; al 0 bl 0 C F 1 CF set 60 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 61 al 00000001 bl 0 C F 0 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 62 al 00000001 bl 0 C F 1 Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 63 al 00000001 bl 10000000 C F 0 Outline • • • • • • 64 Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 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 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 66 ; declared in .data Lookup Table digits: db 8,6,7,5,3,0,9 index 0 1 2 3 4 5 6 digits: mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; al bl 67 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; index 0 1 2 3 4 5 6 digits: 8 6 7 5 3 0 9 al bl 68 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; index 0 1 2 3 4 5 6 digits: 8 6 7 5 3 0 9 al bl 69 8 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 70 index 0 1 2 3 4 5 6 digits: 8 6 7 5 3 0 9 al 8 bl 3 Lookup Table digits: db 8,6,7,5,3,0,9 mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 71 index 0 1 2 3 4 5 6 digits: 8 6 7 5 3 0 3 al 8 bl 3