# LOGIC SHIFT AND ROTATE INSTRUCTIONS Syntax for AND OR

Document Sample

LOGIC, SHIFT AND ROTATE INSTRUCTIONS

• Syntax for AND, OR, XOR and TEST instructions OpCode Destination Source

• They perform Boolean bitwise operations and store the result into Destination TEST is just an AND but the result is not stored. TEST aﬀects the ﬂags just like AND does

• Both operands must be of the same type Either byte, word or dword

• Operands cannot be both Mem Again: Mem to Mem operations are forbidden

• They clear (i.e. put to 0) CF and OF

• They aﬀect SF and ZF according to the result of the operation (as usual)

Logic Instructions (Continued)

• The source is often an Imm operand called a bit mask: used to ﬁx certain bits to 0 or 1

• To clear a certain bit b we use an AND since 1. 0 AND b = 0 2. 1 AND b = b b is cleared b is conserved

Ex: to clear the sign-bit of AL without aﬀecting the others, we do: AND AL, 7Fh ;since 7Fh = 0111 1111b

• To set (i.e. six to 1) a certain bit we use an OR since 1. 1 OR b = 1 2. 0 OR b = b b is set b is conserved

Ex: to set the sign-bit of AH without aﬀecting the others, we do: OR AH, 80h ;since 80h = 1000 0000b. To test if ECX = 0 we can do: OR ECX, ECX Since this does not change the number in ECX and sets ZF (=1) iﬀ ECX= 0

Logic Instructions (Continued)

• XOR can be used to inverse certain bits 1. b AND 1 = NOT(b) 2. b AND 0 = b b is complemented b is conserved

• To initialize a register to 0 we can use XOR AX, AX 1. Since b XOR b = 0 b is cleared

2. This instruction uses only 2 bytes of space 3. The next instruction uses 3 bytes of space MOV Ax, 0 4. Compilers prefer the XOR method

Logic Instructions (Continued)

• To convert from uppercase letter to lowercase letter we can use the usual method ADD DL, 20h

• But 20h = 0100 0000b and bit #5 is always 0 for characters from ’A’ (41h) to ’Z’ (5Ah)
Uppercase ’A’ → ’Z’ ⇓ 41h → 5Ah 0 1 ↓ 0 0 X ⇓ 0 1 0 ↑ 1 X X X X 0 1 1 ↑ 1 X X X 0 1 Lowercase ’a’ → ’z’ ⇓ 61h → 7Ah ↓ 1 0 X ⇓ X X X X X X X

• Hence, adding 20h gives the same result as setting this bit #5 to 1. Thus OR DL, 20h ;convert from uppercase to lowercase AND DL, 0FDh ;convert from lowercase to uppercase Since DFh = 1101 1111b

Logic Instructions (Continued)

• NOT Destination → inverts all the bits (one’s complement) Does not aﬀect any ﬂag, and Destination cannot be an Imm operand

• Recall that to perform two’s complement, we use NEG Destination 1. Aﬀect SF and ZF according to result 2. CF is set to 1 unless the result is 0 3. OF = 1 iﬀ there is a signed overﬂow
• Exercise #1: Use only one instruction among AND, OR, XOR and TEST to do the following task 1. Convert the ASCII code of a decimal digit (’0’ to ’9’) contained in AL to its numerical value 2. Set to 1 the odd numbered bits in EAX (i.e. the bits numbered 1,3,5 . . . ) without changing the even numbered bits 3. Clear to 0 the most signiﬁcant bit (MSB) and the least signiﬁcant bit (LSB) of BH without changing the other bits. 4. Invert the LSB of EBX without changing the other bits

Shifting Bits to the Left • SHL Destination, 1 → shifts one bit to the left 1. Each bit is shifted one position to the left 2. The LSB is cleared to 0 3. The MSB is moved to CF (so the previous content of CF is lost) 4. Destination can be either byte, word or dword, and Mem or Reg (for all shift and rotate instructions) MOV BX, 80h ;BX = 0080h SHL BL, 1 ;BX = 0000h, CF = 1 ;(only BL is affected)

• There are two forms of multiple left-shift SHL Destination, CL ;CL contains number of shifts SHL Destination, Imm8 Where Source should be CL or Imm8 (for all shift and rotate instructions)

Shift’s Eﬀects on Flags • SF and ZF are aﬀected according to result • CF contains the last bit shifted out MOV BH, 82h ;BH = 1000 0010b SHL BH, 2 ;BH = 0000 1000b, CF = 0 • Eﬀect on OF for all (left and right) shift/rotate 1. For any single-bit shift/rotate: OF = 1 iﬀ the shift/rotate changes the sign-bit 2. For multiple-bit shift/rotate: OF is undeﬁned • Ex: Fast Multiplication Each left-shift multiplies by 2 the operand for both signed and unsigned interpretations MOV MOV SHL SHL AX, BX, AX, BX, 4 -1 2 3 ;AX ;BX ;AX ;BX = = = = 0004h FFFFh 0010h = 16 FFF8h = -8

Very fast. Factor your multiplier into powers of 2 1. BX * 36 = BX * (32 + 4) = BX*32 + BX*4 2. So add (BX shifted by 5) to (BX shifted by 2)

Shifting Bits to the Right

• To shift to the right, use either SHR Destination, CL ;CL contains number of shifts SHR Destination, Imm8 1. The MSB of Destination is cleared to 0 2. The LSB of Destination is moved into CF

• Each single-bit right-shift divides the unsigned value by 2 MOV BH, 13 ;BH = 0000 1101b = 13 SHR BH, 2 ;Bh = 0000 0011b = 3 (div by 4), CF = 0 (The remainder of the division is lost)

Arithmetic Shift: SAR and SAL • To divide a signed valued by 2, use either SAR Destination, CL ;CL contains number of shifts SAR Destination, Imm8 1. The MSB of Destination is ﬁlled with its previous value (so the sign is preserved) 2. The LSB of Destination is moved into CF MOV AH, -15 ;AH = 1111 0001b SAR AH, 2 ;AH = 1111 1000b = -8 3. The result is rounded to the smallest integer (−8 instead of −7) 4. In contrast SHR AH, 1 ;gives AH = 0111 1000b = 78h 5. SAL = SHR

• The arithmetic left-shift, SAL, is equivalent to the logical left-shift, SHL. That is they produce the same result, for either signed or unsigned integer multiplication by two

Rotation without Carry Flag

• ROL Destination, Source → rotates bits to the left CF gets a copy of the MSB

• ROR Destination, Source → rotates bits to the right CF gets a copy of the LSB

• Destination should be Mem or Reg

• Source should be CL or Imm8

• CF reﬂects the action of the last rotate

Examples of ROL

MOV AH, 40h ROL AH, 1 ROL AH, 1 ROL AH, 1

;AH = 0100 0000b ;AH = 1000 0000b ;AH = 0000 0001b ;AH = 0000 0010b

MOV AX, 1234h ;AX = 0001 0010 0011 0100b ROL AX, 4 ROL AX, 4 ROL AX, 4 ;AX = 2341h ;AX = 3412h ;AX = 4123h

Rotation with Carry Flag • RCL Destination, Source → rotates to the left with participation of CF

• RCR Destination, Source → rotates to the right with participation of CF

• Destination should be Mem or Reg • Source should be CL or Imm8 • CF reﬂects the action of the last rotate • Inverting the content of AL Ex: AL = 11000001b AL = 10000011b

MOV ECX, 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

Exercise #2
• Give the hexadecimal content of AX immediately after the execution of each instruction below (consider AX = B3CAh before each instruction) 1. SHL AL, 2 ;AX = 2. SAR AH, 2 ;AX = 3. ROR AX, 4 ;AX = 4. ROL AX, 3 ;AX = 5. SHL AL, 8 ;AX =

Application: Binary Output

• To display the binary number in EAX MOV ECX, 32 ;count 32 binary characters start: ROL EAX, 1 ;CF gets MSB JC one ;if CF = 1 MOV EBX, ’0’ JMP display one: MOV EBX, ’1’ display: PUTCH EBX LOOP start

Application: Binary Input

• To load EAX with the numerical value of a binary string (ex: 101100101. . . ) entered at the keyboard XOR EBX, EBX ;clear EBX to hold entry next: GETCH CMP EAX, 10 ;EOLN reached? JE exit ;yes! then exit AND AL, 0Fh ;no! then convert to binary value SHL EBX, 1 ;make room for new value OR BL, AL ;put value in LSB of BL JMP next exit: MOV EAX, EBX ;EAX holds binary value

• In AL we have either 30h or 31h (ASCII code of ’0’ and ’1’)

• Hence, AND AL, 0Fh converts AL to either 0h or 1h

• Hence, OR BL, AL possibly changes only the LSB of BL

Algorithms for Hexadecimal Input/Output • To display EAX’s content in hexadecimal Repeat 8 times ROL EAX, 4 ;the 4LSB gets the 4MSB of EAX MOV DL, AL AND DL, 0Fh ;DL contains numeric value of ;4LSB of AL If DL < 10 then Convert to ’0’ ... ’9’ Else Convert to ’A’ ... ’F’ End Repeat • To load EAX with numerical value of the hexadecimal string entered at the keyboard XOR EBX, EBX ;EBX will hold result While (Input char = <CR>) Do Convert Char into numerical Value Left-shift EBX by 4 bits Insert Value into 4LSB of EBX End While MOV EAX, EBX • The complete ASM code is left to the reader

DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 56 posted: 3/13/2009 language: English pages: 15