Title: Assembly Language Lecture 7
1Assembly LanguageLecture 7
Bit manipulation and Bit operation
2Overview
- Arithmetic Instruction
- Boolean Instruction
3Arithmetic Instruction
- Shift Instruction
- Rotate Instruction
- Sample Applications
4Shift Instruction
- Shifting The bits a shifted left or right.
- Some bits may be lost.
- Example Shift 00001101 1 bit
rightAdded 00000110 Lost
5Shifting bits to the left (SHL)
- To shift 1 bit to the left we use
- SHL dest, value
- the msb (most significant bit) is moved into CF
- (so the previous content of CF is lost)
- each bit is shifted one position to the left
- the lsb (least significant bit) is filled with 0
- dest can be either byte, word or dword
6Example Shifting bits to left
- Ex
- mov bl,82h BX _____
- shl bl,1 BX _____, CF ____
(only BL and CF are changed)
Before
BL
CF
After SHL BL, 1
7Shifting multiple times to the left
- Two forms are permitted
- SHL dest,CL value in CL number of shifts
- SHL dest, imm8 needs .286 or higher (except
"1") - SHL affects SF, PF, ZF according to the result
- CF contains the last bit shifted from the
destination - OF 1 iff the last shift changes the sign bit
- mov bh,D2h BH 1011 0010bshl bh, 1 BH
___________b, CF__, OF__shl bh, 2 BH
___________b, CF__, OF__ - Overflows are signaled only for the last bit
shifted - SAL is the same instruction as SHL but used with
the signed interpretation (more later)
8Fast Multiplication
- Each left shift multiplies by 2 the operand for
both signed and unsigned interpretations. - Ex
- mov al,4 AL 04h 0000 0100b 4
- shl al,1 AL 08h ____________b ____
- shl al,2 AL 20h ____________b ____
- mov bl,-1 BL FFh ____________b ____
- shl bl,3 BL F8h ____________b ____
9Fast Multiplication (cont.)
- Cycles
- Instruction 8088 80486
PentiumSHL reg, 1 2 3
1SHL reg, immed - 2
1SHL reg, CL 8 4n 3 4MUL
reg byte 70-77 13-18 11
word 118-13 13-26 11
doubleword N/A 13-42 10
10Fast Multiplication (cont.)
- Multiplication by shifting is very fast. Try to
factor your multiplier into powers of 2 - AX 36 AX (32 4) AX32 AX4
- So add (AX shifted by 5) to (AX shifted by 2)
- That is, we might replace a multiply by 36
instruction by copy AX to BXshift AX left 5
bits (32 AX)shift BX left 2 bits (4
AX)add BX to AX (36 AX)
11Fast Multiplication (cont.)
- Multiplication using shifts on 8088 mov BX,
AXmov CL, 5shl AX, CLshl BX, 1shl BX,
1add AX, BX
Replace by 5 shl AX, 1commands total
19
12Fast Multiplication (cont.)
- Multiplication using shifts on 486 Pentium mov
BX, AXshl AX, 5shl BX, 2add AX, BX
486 cycles Pentium 1
1 2
1 2 1
1 1Total 6 4
13-26 11
13Shifting bits to the right (SHR)
- To shift to the right use
- SHR dest, value
- the msb of dest is filled with 0
- the lsb of dest is moved into CF
14Example Shifting bits to right
- Each single-bit right shift divides the unsigned
- value by 2. Ex
- mov bh,13 BH ______b 13
- shr bh,2 BH ______b 3 (div by 4), CF
0 - (the remainder of the division is lost)
15Arithmetic Shift SAR
- Is needed to divide the signed value by 2
- SAR, dest, value
- the msb of dest is filled with its previous value
- (so the sign is preserved)
- the lsb of dest is moved into CF
16Example Arithmetic Shift SAR
- mov ah,-15 AH 1111 0001bsar ah,1
AH ______________b -8the result is rounded
down (-8 instead of -7) - In contrast, shr ah,1 gives ________b 78h
- SAL is the same as SHLIt is just interpreted as
being - for signed arithmetic
17Rotate Instruction
- Rotating The bits shift out of one end of the
data are placed at the other end of the data so
nothing is lost. - Example Rotate 10001101 2 bits to
the left - 00110110
18Rotate (without the CF)
- ROL rotates the bits to the left (same syntax)
- CF gets a copy of the original msb
- ROR rotates the bits to the right (same syntax)
- CF gets a copy of the original lsb
19Examples of ROL and ROR
- CF and OF reflect the action of the last rotate
- mov ah,40h ah 0100 0000b
- rol ah,1 ah 1000 0000b, CF 0
- rol ah,1 ah 0000 0001b, CF 1
- rol ah,1 ah ____________b, CF ___
- mov ax,1234h ax 0001 0010 0011 0100 1234h
- ror ax,4 ax 4123h
- ror ax,4 ax 3412h
- ror ax,4 ax 2341h
20Rotate with CF
- RCL rotates to the left with participation of CF
- RCR rotates to the right with participation of CF
21Application Reversing the content of AL
- Ex if AL 1100 0001b, we want to reverse the
- order of the bits so AL 1000 0011b
- mov cx,8 number of bits to rotate
- start
- shl al,1 CF msb of AL
- rcr bl,1 push CF into msb of BL
- loop start repeat for 8 bits
- mov al,bl store result into AL
22Application Binary Output
- To display the number in BX in binary
- MOV AH,2 display
- MOV CX,16 16 chars
- START ROL BX,1 CF gets msb
- JC ONE if CF 1
- MOV DL,0 print '0'
- JMP DISP
- ONE MOV DL,1 print '1'
- DISP INT 21h print
23Multiplying with shifts and adds
- Some early CPUs lacked hardware multiply
- Solution Shift and add routines
- Advantage "Cheaper"
- Disadvantage Slow
- Algorithm show here is simple, faster ones are
available. Neat feature it calculates a double
width product using single width operations
except for rotating
24Multiplying with shifts and adds (cont.)
- AlgorithmAssumes BL contains multiplicand
DL contains multiplier 1.
Initialize Clear accumulator AX Put 8
in CX 2. Repeat 8 times (once per bit of
multiplier) Shift DL right by 1 bit into
CF If CF 1, Add BL to AH with
carry in CF Rotate AX (include CF) The
result is in AX.
25Multiplying with shifts and adds (cont.)
- Multiply PROC XOR AX, AX
MOV CX, 8Repeat1 SHR DL, 1 JNC
Lskip ADD AH, BLLSkip RCR
AX, 1 LOOP Repeat1
RETMultiply ENDP
26Multiplying with shifts and adds (cont.)
- 4 bit example 0110 1010DL 1010
0101 0010 shiftCF
? 0 1BL 0110
0110 0110 AX 0000 0000
0110 0000 addCF
0AX
0000 0000 0011 0000 rotate CX 4
3 2
27Multiplying with shifts and adds (cont.)
- 4 bit example 0110 1010 continuedDL
0001 0000 shift CF
0 1 BL 0110
0110 AX 0111
1000 addCF 0 AX
0001 1000 0011 1100 rotateCX 1
0
28Boolean Instruction
29AND Instruction
- Each bit in the result is created by ANDing
- each of the input bits in the same position
- op-1 1 1 0 1 0 0 1 1
- op-2 0 1 0 0 1 1 0 1
- result 0 1 0 0 0 0 0 1
30AND Instruction (cont.)
- Performs a boolean AND operation between each of
the corresponding bits in two operands and places
the result in the first operand - and ax, bx
- and var1, dx
- and bl, var2
- and dx, 02FAh
- and al, 00001111b
target
source
31OR Instruction
- Each bit in the result is created by ORing
- each of the input bits in the same positions
- op-1 1 1 0 1 0 0 1 1
- op-2 0 1 0 0 1 1 0 1
- result 1 1 0 1 1 1 1 1
32OR Instruction (cont.)
- Performs a boolean OR operation between each of
the corresponding bits in two operands and places
the result in the first operand - or ax, bx
- or var1, dx
- or bl, var2
- or dx, 02FAh
- or al, 00001111b
target
source
33XOR Instruction
- Each bit in the result is created by XORing
- each of the input bits in the same positions
- op-1 1 1 0 1 0 0 1 1
- op-2 0 1 0 0 1 1 0 1
- result 1 0 0 1 1 1 1 0
- (XOR yields a 1 only when the two input bits are
different.)
34XOR Instruction (cont.)
- XORing any bit with 1 toggles (inverts) the bit
- mov al,10110011b
- xor al,11111111b AL 01001100
- XORing any bit with 0 leaves the bit unchanged
- mov al,10110011b
- xor al,00000000b AL 10110011
35XOR Instruction (cont.)
- The XOR operation reverses itself.
Same
mov al, 10110011b xor al, 10101100b AL
00011111 xor al, 0101100b AL 10110011
36NOT Instruction
- Performs a boolean NOT (inversion)
- operation on each of the bits in the operand.
- mov bh, 11001101b
- not bh bh 00110010
37NEG Instruction
- Converts the operand to its twos complement.
- It's a good idea to check the Overflow flag after
- performing a NEG operation.
- mov bl, 00000001b
- neg bl bl 11111111
- mov ah, 11001101b
- neg ah ah 00110011
- mov al, -128
- neg al al 80h, OF1
38TEST Instruction
- Performs an implied AND operation between
- corresponding bits in the two operands.
- Only the flags are affected.
- mov bl, 00000001b
- test bl, 00000001b ZF 0
- mov ah, 11001101b
- test ah, 00110010b ZF 1
39TEST Instruction (cont.)
- Example the following will jump to the label
- PrinterReady if either bit 0, 1, or 2 is set in
the AL - register
- test al, 00000111b
- jnz PrinterReady
- .
- .
- PrinterReady
40BSF/BSR Bit scan forward/reverse
Syntax BSF destination, source BSR
destination, source Description Scans an
operand to find the first set bit. If a set bit
is found, the zero flag is cleared and the
destination operand is loaded with the bit index
of the first set bit encountered. If no set bit
is found, the zero flag is set. BSF (Bit Scan
Forward) scans from bit 0 to the most significant
bit. BSR (Bit Scan Reverse) scans from the most
significant bit of an operand to bit 0.
41Example BSF/BSR
CX BX CX
(before) (before) (after)
Scan Forward BSF CX,BX 00001010
11101000 5d
543210 ltlt Scan Reverse BSR CX,BX
00001010 11101000 2d
gtgt 012
42BT/BTS/BTR/BTCBit test (with Set/Reset/Complement
)
Syntax BT destination, source BTS destination,
source BTR destination, source BTC destination,
source Description Copies the value of a
specified bit into the carry flag, where it can
be tested by a JC or JNC instruction. The
destination operand specifies the value in which
the bit is located the source operand specifies
the bit position. BT copies the bit to the
flag. BTC copies complements (toggles) it in the
destination. BTR copies the bit and resets
(clears) it in the destination. BTS copies the
bit and sets it in the destination.
43Example BT/BTS/BTR/BTC
AX AX Carry (before)
(after) FlagTest OnlyBT
AX,01001000b 11110000b 11110000b 0BT
AX,01001000b 10110000b 10110000b 0BT
AX,01001000b 11111000b 11111000b
1 Test Complement (toggle)BTC
AX,01001000b 11110000b 10111000b 0BTC
AX,01001000b 10110000b 11111000b 0BTC
AX,01001000b 11111000b 10110000b 1
44Example BT/BTS/BTR/BTC (cont.)
AX AX Carry (before)
(after) Flag Test Reset (Clear)
(0) BTR AX,01001000b 11110000b 10110000b
0BTR AX,01001000b 10110000b 10110000b
0BTR AX,01001000b 11111000b 10110000b
1 Test Set (1) BTS AX,01001000b
11110000b 11111000b 0BTS AX,01001000b
10110000b 11111000b 0BTS AX,01001000b
11111000b 11111000b 1
45Many way to write use
BT BX,00000100b BTS AX,01001000b BTS
AX,4d BTR BX,17h BTR DWORD PTR
SI,2d BTC COLOR DI,4d BTC DWORD PTR
BX,2d BTC MASKIT,4d BTR COLOR DI,4d BT
AX,BX BTS BX,AX
46Many way to write use (cont.)
BTR CX,DI BT BX,DX BTS FLAGS
BX,CX BTR ROTATE,CX BTC BP8,SI
47SETcondition Instruction
- Syntax
- SETcondition destination
- Description
- Sets the byte specified in the operand to 1 if
ltconditiongt - is true or to 0 if ltconditiongt is false. The
condition is tested - by checking the flags. See the Set Conditions
Table in the - next Page. This instruction is use to set Boolean
flag - conditionally.
48SETcondition Instruction (cont.)
MNEMONIC FLAGS CHECKED DESCRIPTION SETB/S
ETNAE CF1 Set if below/not above
or equal (unsigned comp) SETAE/SETNB CF0
Set if above or equal/not below (unsigned
comp) SETBE/SETNA CF1 or ZF1 Set if
below or equal/not above (unsigned) SETA/SETNBE
CF0 and ZF0 Set if above/not below or
equal (unsigned) SETE/SETZ ZF1
Set if equal/zero SETNE/SETNZ ZF0
Set if not equal/not zero SETL/SETNGE
SFltgtOF Set if less/not greater or equal
(signed compare) SETGE/SETNL SFOF
Set if greater or equal/not less (signed
compare) SETLE/SETNG ZF1 or SFltgtOF Set if
less or equal/not greater or equal
(sign) SETG/SETNLE ZF0 and SFOF Set if
greater/not less or equal (signed)
49SETcondition Instruction (cont.)
MNEMONIC FLAGS CHECKED DESCRIPTION SETS
SF1 Set if
sign SETNS SF0 Set
if not sign SETC CF1
Set if carry SETNC CF0
Set if not carry SETO OF1
Set if overflow SETNO
OF0 Set if not overflow SETP/SETPE
PF1 Set if parity/parity
even SETNP/SETPO PF0 Set if no
parity/parity odd
50Example SETcondition Instruction
IF Flag Is AL(before) AL(after) SETB AL
CF1 32H (or any) 01H SETB AL CF0 53H (or
any) 00H SETNAE AL CF1 11H (or
any) 01H SETNAE AL CF0 25H (or
any) 00H SETAE AL CF0 75H (or any) 01H SETAE
AL CF1 23H (or any) 00H SETNB AL CF0 14H
(or any) 01H SETNB AL CF1 89H (or
any) 00H SETBE AL CF1 or ZF1 65H (or
any) 01H SETBE AL CF0 or ZF0 32H (or
any) 00H SETNA AL CF1 or ZF1 16H (or
any) 01H SETNA AL CF0 or ZF0 78H (or any) 00H
51Example SETcondition Instruction
IF Flag Is AL(before) AL(after) SETA AL
CF0 and ZF0 46H (or any) 01H SETA AL CF1
and ZF1 73H (or any) 00H SETNBE AL CF0 and
ZF0 23H (or any) 01H SETNBE AL CF1 and ZF1
58H (or any) 00H SETE AL ZF1 32H (or
any) 01H SETE AL ZF0 24H
(or any) 00H SETZ AL
ZF1 35H (or any) 01H SETZ AL
ZF0 32H (or any) 00H SETNE
AL ZF0 58H (or any) 01H SETNE AL ZF1 99H
(or any) 00H SETNZ AL ZF0 64H (or
any) 01H SETNZ AL ZF1 23H (or any) 00H
52Example SETcondition Instruction
IF Flag Is AL(before) AL(after) SETL
AL SFltgtOF 25H (or any) 01H SETL AL SFOF 68H
(or any) 00H SETNGE AL SFltgtOF 54H (or
any) 01H SETNGE AL SFOF 32H (or
any) 00H SETGE AL SFOF 45H (or any) 01H SETGE
AL SFltgtOF 67H (or any) 00H SETNL AL SFOF 86H
(or any) 01H SETNL AL SFltgtOF 43H (or any) 00H