Assembly 05
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1
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 3 bit 0 bit number increases
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
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 5
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 AND
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 8
Bitwise Logic Mnemonics 9 andxor notor
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
and Mnemonic mov al, b; mov bl, b; and al, bl; 11 Note that you can input binary numbers directly… al bl
and Mnemonic mov al, b; mov bl, b; and al, bl; al bl
and Mnemonic mov al, b; mov bl, b; and al, bl; al bl
and Mnemonic mov al, b; mov bl, b; and al, bl; al bl
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
or Mnemonic mov al, b; mov bl, b; or al, bl; 16 al bl
or Mnemonic mov al, b; mov bl, b; or al, bl; al bl
or Mnemonic mov al, b; mov bl, b; or al, bl; al bl
or Mnemonic mov al, b; mov bl, b; or al, bl; al bl
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
xor Mnemonic mov al, b; mov bl, b; xor al, bl; 21 al bl
xor Mnemonic mov al, b; mov bl, b; xor al, bl; al bl
xor Mnemonic mov al, b; mov bl, b; xor al, bl; al bl
xor Mnemonic mov al, b; mov bl, b; xor al, bl; al bl
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
not Mnemonic mov al, b; not al; 26 al
not Mnemonic mov al, b; not al; al
not Mnemonic mov al, b; not al; al
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 29
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
Bit Masking Example: isolate bits #4 and #5 mov al, b;value to inspect mov bl, b;bit mask and al, bl;isolate bits 31
Bit Masking Example: isolate bits #4 and # valuemaskresult LSB MSB AND=
Bit Masking Example: isolate bits #4 and # valuemaskresult LSB MSB mask allows two bits to “pass through”
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 34
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 zero padding MSB “falls off”
Bit Shifting Example: Shift Right 3 Units these bits “fall off” “new” bits zero padded
Bit Shifting shl-> shift left shl, 38 register or memory always cl register or immediate value
Bit Shifting mov al, b; mov cl, 4; shl al, cl; 39 al cl
Bit Shifting mov al, b; mov cl, 4; shl al, cl; al cl
Bit Shifting mov al, b; mov cl, 4; shl al, cl; al cl
Bit Shifting mov al, b; mov cl, 4; shl al, cl; al cl
Bit Shifting shr-> shift right shr, 43 register or memory always cl register or immediate value
Bit Shifting x: db b; declare x in.data section. shr byte [x], 3;shift [x] right 3 places ;(in.text section) 44
Bit Shifting x: db b shr byte [x], x:
Bit Shifting x: db b shr byte [x], x:
Bit Shifting (Rotate) Bits that “fall off” appear at other end E.g., rotate left by 3:
Bit Shifting (Rotate) rol-> rotate left rol, 48 register or memory always cl register or immediate value
Bit Shifting (Rotate) ror-> rotate right ror, 49 register or memory always cl register or immediate value
Bit Shifting (Rotate) mov al, b; rol al, 1; 50 al ? CFCF
Bit Shifting (Rotate) mov al, b; rol al, 1; al ? CFCF
Bit Shifting (Rotate) mov al, b; rol al, 1; al 1 CFCF CF set
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
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
Bit Shifting (Rotate) E.g., rotate left 1 with carry (rcl) CFCF CFCF
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
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 57 al ? CFCF bl
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 58 0 al ? CFCF bl
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; 59 0 al ? CFCF 0 bl
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
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; al 0 CFCF 0 bl
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; al 1 CFCF 0 bl
Bit Shifting (Rotate) mov al, 0; mov bl, 0; stc; rcl al,1; stc; rcr bl,1; al 0 CFCF bl
Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 64
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 ;declared in.data mov al, [digits]; mov bl, [digits + 4]; mov [digits + 6], bl; 66
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: index
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 digits: index
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 digits: index
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 digits: index
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 digits: index