# Logic Shift and Rotate instructions 1 CAP 221

- Slides: 83

Logic, Shift, and Rotate instructions 1 CAP 221 2/22/2021

Logic instruction • the ability to manipulate individual bits is one of the advantages of assembly language. • bitwise logical operations are performed at bit-by-bit basis. • AND destination, source • OR destination, source • XOR destination, source • NOT destination 2 CAP 221 2/22/2021

AND Instruction • Performs a Boolean AND operation between each pair of matching bits in two operands AND 3 CAP 221 2/22/2021

OR Instruction • Performs a Boolean OR operation between each pair of matching bits in two OR operands 4 CAP 221 2/22/2021

XOR Instruction • Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands XOR is a useful way to toggle (invert) the bits in an operand. 5 CAP 221 2/22/2021

NOT Instruction • Performs a Boolean NOT operation on a single destination operand NOT 6 CAP 221 2/22/2021

Logic instruction • • 7 AND destination, source OR destination, source XOR destination, source The result of the operation is stored in the Destination n, which must be a general register or a memory location. The Source may be an constant value, register, or memory location. The Destination and Source CANNOT both be memory locations. CAP 221 2/22/2021

Logic instruction • Instruction: AND AH, AL ; --> means: AH = AH AND AL AH = 01001100 AL = 00101101 ------- AND result = 00001100 (= 12) is stored in AH 8 CAP 221 2/22/2021

Logic instruction • Instruction: OR AH, AL ; --> means: AH = AH OR AL AH = 01001100 AL = 00101101 ------- OR result = 01101101 (= 6 Dh) is stored in AH 9 CAP 221 2/22/2021

Logic instruction • Instruction: XOR AH, AL ; --> means: AH = AH XOR AL AH = 01001100 AL = 00101101 ------- OR result = 01100001 (= 61 h) is stored in AH 10 CAP 221 2/22/2021

AND, OR, XOR Effects on Status Flag • Zero flag (ZF), Sign flag (SF), Parity flag (PF) are affected • carry flag (CF) and overflow flag (OF) are cleared. • AF is undefined 11 CAP 221 2/22/2021

AND, OR, XOR The main usage of bitwise logical instructions is: Øto set some selected bits in the Destination operand. Øto clear some selected bits in the Destination operand. Øto invert some selected bits in the Destination operand. To do this, a Source bit pattern known as a mask is constructed. 12 CAP 221 2/22/2021

AND, OR, XOR • The mask bits are chosen so that the selected bits are modified in the desired manner when an instruction of the form: LOGIC_INSTRUCTION Destination , Mask is executed. The Mask bits are chosen based on the following properties of AND, OR, and XOR: If b represents a bit (either 0 or 1) then: b AND 1 = b b OR 1 = 1 b XOR 1 = b b AND 0 = 0 b OR 0 = b b XOR 0 = b AND OR XOR 13 CAP 221 2/22/2021

AND, OR, XOR • The AND instruction can be used to CLEAR specific Destination bits while preserving the others. A zero mask bit clears the corresponding Destination bit; a one mask bit preserves the corresponding destination bit. 14 CAP 221 2/22/2021

AND, OR, XOR • The OR instruction can be used to SET specific destination bits while preserving the others. A one mask bit sets the corresponding destination bit; a zero mask bit preserves the corresponding destination bit. 15 CAP 221 2/22/2021

AND, OR, XOR • The XOR instruction can be used to INVERT specific Destination bits while preserving the others. A one mask bit inverts the corresponding Destination bit; a zero mask bit preserves the corresponding Destination bit. 16 CAP 221 2/22/2021

AND, OR, XOR / Examples • Clear the sign bit of AL while leaving the other bits un changed. AND AL, 7 Fh ; the mask = 01111111 b • Set the most significant and least significant bits of AL while preserving the other bits. OR AL, 81 h ; the mask = 10000001 b • Change the sign bit of DX. XOR DX, 8000 h 17 CAP 221 2/22/2021

Converting an ASCII Digit to a Number • For any ASCII digits, bit 4 and 5 of its ASCII code are 11; but for the corresponding decimal digit bit 4 and 5 are 00. The remaining bits are similar: 5 d = 00000101, ASCII 5 = 00110101 • If the key ‘ 5’ is pressed, AL gets 35 h, to get 5 in AL, we could do: SUB Or AND 18 AL, 30 h AL, 0 Fh CAP 221 2/22/2021

Changing a letter to its opposite case • The ASCII code of ‘a' to ‘z’ range from 61 h to 7 Ah; the code of ‘A’ to ‘Z’ go from 41 h to 5 Ah. If DL contain the code of a lower case letter, it can be converted to upper case by: SUB DL, 20 h 19 CAP 221 2/22/2021

Changing a letter to its opposite case • For any alphabetic letter, bit 5 of its ASCII code is 1; but for the corresponding uppercase letter bit 5 is 0. The remaining bits are similar: Character code character code A 01000001 a 01100001 B 01000010 b 01100010. . . . . Z 01011010 z 01111010 • To convert lower to upper case we can do this: AND DL, 0 DFh 20 CAP 221 2/22/2021

Clearing a register • A register operand can be cleared to zero using any of the instructions: MOV, SUB, AND, and XOR. The followings are ways to clear any general-purpose register to zero. MOV AX, 0 SUB AX, AX AND AX, 0 XOR AX, AX 21 CAP 221 2/22/2021

Clearing a memory location • A memory operand can be cleared to zero using either the MOV or AND instruction. The followings are ways to clear any memory location to zero. MOV VAR 1, 0 AND VAR 1, 0 22 CAP 221 2/22/2021

Testing a register for Zero • CMP AX, 0 • OR instruction can be used to examine whether or not any general-purpose register is equal to zero. OR AX, AX ZF is affected and if AX contains 0; ZF=1 23 CAP 221 2/22/2021

NOT Instruction • Performs the one’s compliment operation in the destination: NOT destination • No effects on the status flags • Example: complement the bits in AX NOT AX 24 CAP 221 2/22/2021

TEST Instruction • Performs an AND operation but does not change the destination contents: TEST destination, source Effects on Status Flag • ZF, SF, PF reflects the result • CF and OF are cleared. • AF is undefined 25 CAP 221 2/22/2021

TEST Instruction • The TEST Instructions can be used to examine the status of selected bits in the destination operand. • The mask should contain 1’s in the bit positions to be tested and 0’s elsewhere. • The result will have 1’s in the tested bit positions if the destination has 1’s in these positions 26 CAP 221 2/22/2021

TEST Instruction Example • Jump to label BELOW if AL contains even number Solution: • Bit #0 in even numbers is 0 mask = 00000001 b=1 TEST AL, 1 JZ BELOW 27 CAP 221 2/22/2021

Shift Instruction • Shifting: The bits are shifted left or right. bits shifted out are lost. • Rotating: The bits shift out of one end of the data are placed at the other end of the data so nothing is lost. 28 CAP 221 2/22/2021

Shift Instruction • Two possible formats: ; for a single shift or rotat Opcode destination, 1 ; for a shift or rotat of N positions Opcode destination, CL where CL contains N • Destination is an 8 -bit or 16 -bit register or memory location 29 CAP 221 2/22/2021

Shift Left Instruction • To shift 1 bit to the left we use: SHL dest, 1 – 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 30 CAP 221 2/22/2021

Left shift instruction • Shifting multiple times to the left: SHL dest, CL shifts • Effect on flags: ; value in CL = number of SF, PF, ZF reflect the result CF contains the last bit shifted from the destination OF = 1 if the last shift changes the sign bit (if count more than 1 , OF is undefined) 31 CAP 221 2/22/2021

Example • Suppose DH = 8 Ah, CL= 3. What are the contents of DH and of CF after execution of: SHL DH, CL • DH= 10001010, after 3 left shift: • DH= 01010000 =50 h, CF=0 32 CAP 221 2/22/2021

Multiplication by left shift • Each left shift multiplies by 2 the operand for both signed and unsigned interpretations: AL contains 5= 00000101 b. SHL AL, 1 ; AL=00001010 b =10 d SHL AL, 1 ; AL=00010100 b =20 d AX contains FFFFh (-1), CL =3 SHL AX, CL ; AX=FFF 8 h (-8) 33 CAP 221 2/22/2021

SAL instruction • SHL is used to multiply an operand by multiples of 2. • Shift Arithmetic Left SAL is used to emphasize the arithmetic nature of the operation. • SHL and SAL generate the same machine code 34 CAP 221 2/22/2021

overflow • CF and OF accurately indicate unsigned and signed overflow for a single shift. • For a multiple left shift CF, OF only reflect the result of the last shift. BL contains 80 h, CL contains 2 SHL BL, CL ; CF =OF =0, even though both signed and unsigned overflow occur 35 CAP 221 2/22/2021

example • Write some code to multiply the value of AX by 8. Assume that over flow will not occur. • Solution: MOV CL, 3 ; number of shifts to do SAL AX, CL ; multiply by 8 36 CAP 221 2/22/2021

Right shift instruction • To shift to the right use: – SHR dest, 1 – SHR dest, CL ; value of CL = number of shifts. – The effect on the flags is the same as for SHL. 37 CAP 221 2/22/2021

Example • Suppose DH = 8 Ah, CL= 2. What are the contents of DH and of CF after execution of: SHR DH, CL • DH= 10001010, after 2 right shifts: • DH= 0010 =22 h, CF=1 38 CAP 221 2/22/2021

The SAR instruction • The shift arithmetic right operates like SHR, with one difference. The MSB retains its original value. • SAR des, 1 • SAR des, CL • The effect on flags is the same as SHR. 39 CAP 221 2/22/2021

Division by right shift • A right shift might divide the destination by 2, this is correct for even numbers. For odd numbers, a right shift halves it and rounds down to the nearest integer. • Ex: if BL = 00000101 b =5 d • After SHR BL, 1 • BL = 00000010=2 d 40 CAP 221 2/22/2021

Signed and unsigned division • If an unsigned interpretation is being given, SHR should be used. • If a signed interpretation is being given, SAR should be used, because it preserve the sign. 41 CAP 221 2/22/2021

example • Use right shifts to divide the unsigned number 65143 by 4. put the quotient in AX. • Solution: MOV AX, 65143 MOV CL, 2 SHR AX, CL 42 CAP 221 2/22/2021

example • If AL contains -15, give the decimal value of AL after SAR AL, 1 is performed. • Solution: -15 d= 11110001 b After shifting : 11111000 b=-8 d 43 CAP 221 2/22/2021

• Logical shift Summary • Arithmetic shift • Examples: 00010111 (decimal +23) LEFT-SHIFT = 00101110 (decimal +46) 10010111 (decimal − 105) RIGHT-SHIFT = 11001011 (decimal − 53) 44 CAP 221 2/22/2021

Summary • Logical and arithmetic left-shifts are the same. • Logical right-shift is ideal for unsigned binary numbers, while the arithmetic right-shift is ideal for signed 2's complement binary numbers. • Left shift by n is equivalent to multiplying by 2 n • Right shift by n of a value is equivalent to dividing by 2 n and rounding toward negative infinity. 45 CAP 221 2/22/2021

Rotate left • Shifts bits to the left. The MSB is shifted into the rightmost bit. The CF gets the bit shifted out of the MSB. • ROL des, 1 • ROL des, CL 46 CAP 221 2/22/2021

Rotate right • Shifts bits to the right. The Right Most Bit is shifted into the MSB bit. The CF gets the bit shifted out of the RMB. • ROR des, 1 • ROR des, CL • We can use ROL and ROR to inspect the bits in a byte or word, without changing the contents. 47 CAP 221 2/22/2021

example • Use ROL to count the number of 1 bits in BX, without changing BX. Put the answer in AX. • Solution: XOR AX, AX JNC next MOV CX, 16 INC AX top: next: ROL BX, 1 LOOP top 48 CAP 221 2/22/2021

Rotate carry left • Shifts the bits of the destination to the left. • The MSB is shifted into CF, and the previous value of CF is shifted into the rightmost bit. • RCL des, 1 • RCL des, CL 49 CAP 221 2/22/2021

Rotate carry right • Shifts the bits of the destination to the right. • The Right Most Bit is shifted into CF, and the previous value of CF is shifted into the MSB bit. • RCR des, 1 • RCR des, CL 50 CAP 221 2/22/2021

example • Suppose DH = 8 Ah, CF = 1, and CL=3 what are the values of DH and CF after RCR DH, CL Solution: CF DH initial values 1 10001010 after 1 0 11000101 after 2 1 01100010 after 3 0 10110001 51 CAP 221 2/22/2021

Effect of the rotate instructions on the flags • CF = last bit shifted out • OF = 1 if result changes sign on the last rotation. (if count more then 1, OF is undefined) 52 CAP 221 2/22/2021

An Application Reversing a Bit Pattern

Reversing the Bit Pattern • in a word or a byte. • Example : AL contains 11011100 we want to make it 00111011 54 CAP 221 2/22/2021

Solution • SHL from AL to CF and • RCR to move them into the left end of another register… BL 55 1 1 0 1 1 1 0 1 1 CAP 221 2/22/2021

SHL & RCR CF 1 AL 1 1 0 1 1 1 0 0 0 AL 1 0 0 0 0 BL 0 CF 56 CAP 221 2/22/2021

SHL & RCR CF 1 AL 1 1 0 1 1 1 0 0 0 AL 0 0 1 1 1 0 1 1 BL 0 CF 57 CAP 221 2/22/2021

Code MOV CX, 8 ; no. of operation to do REVERSE : SHL AL, 1 ; get a bit into CF RCR BL, 1 ; rotate it into BL LOOP REVERSE ; loop until done MOV AL, BL ; AL gets reverse patterns 58 CAP 221 2/22/2021

Binary & hex I/O • Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert each character to a bit value& collects the bits in a register 59 CAP 221 2/22/2021

Algorithm • Clear BX…. . To hold the binary value • Input a character ……. . ‘ 1’ or ‘ 0’ • WHILE character <> CR then Convert character to binary value Left shift BX Insert value into LSB of BX Input a character END_WHILE 60 CAP 221 2/22/2021

Demonstration for input 110 • Clear BX BX 0000 Input a character ‘ 1’ convert to 1 Left Shift BX BX 0000 Insert value into LSB BX 0000 0001 61 CAP 221 2/22/2021

Demonstration for input 110 Input a character ‘ 1’ convert to 1 Left Shift BX BX 0000 0010 Insert value into LSB BX 0000 0011 62 CAP 221 2/22/2021

Demonstration for input 110 Input a character ‘ 0’ convert to 0 Left Shift BX BX 0000 0110 Insert value into LSB BX 0000 0110 BX contains 110 b 63 CAP 221 2/22/2021

The algorithm assumes • Input characters are either “ 0”, ” 1”or CR • At most 16 bit are input • BX is shifted left to make room and the OR operation is used to insert the new bit into BX 64 CAP 221 2/22/2021

Code XOR MOV INT BX, BX AH, 1 21 H ; clear BX ; input character function ; read a character WHILE_: CMP JE AND SHL OR INT JMP END_WHILE : 65 AL, 0 DH ; CR? END_WHILE ; yes , done AL, 0 FH ; no, convert to binary value BX, 1 ; make room for new value BL, AL ; put value in BX 21 H ; read a character WHILE_ ; loop back CAP 221 2/22/2021

Binary & hex I/O • Binary output: Outputting contents of BX in binary … Shift Operation 66 CAP 221 2/22/2021

Algorithm FOR 16 times DO Rotate left BX /* BX … output CF … MSB */ IF CF = 1 THEN output ‘ 1’ ELSE output ‘ 0’ END_IF END_FOR 67 CAP 221 2/22/2021

Binary output MOV TOP: ROL JC MOV JMP 68 AH, 2 CX, 16 BX, 1 DS 1 DL, ’ 0’ ; OR 30 H DSS DS 1: MOV DL, ’ 1’ ; OR 31 H DSS: INT 21 H LOOP TOP CAP 221 2/22/2021

• • • 69 • . data y db 0 Decimal input mov ah, 1 int 21 h wh_: cmp al, 0 dh je end_ call insert_digit next: int 21 h jmp wh_ • insert_digit proc • mov cl, 3 • and al, 0 fh • mov bl, y • shl y, cl • shl bl, 1 • add y, bl • add y, al • ret • insert_digit endp CAP 221 2/22/2021

Hex Input Digits “ 0” to “ 9” & letters “A” to “F” Followed by a carriage return 70 CAP 221 2/22/2021

Assume • Only upper case letters are used. • The user inputs no more than 4 hex characters. • BX must be shifted 4 times to make room for a hex value. 71 CAP 221 2/22/2021

Algorithm for hex input • Clear BX • Input a hex character • WHILE character <> CR DO – Convert character to binary value – Left shift BX 4 times – Insert value into lower 4 bits of BX – Input a character END_WHILE 72 CAP 221 2/22/2021

Demonstration for input 6 AB • Clear BX BX 0000 Input a character ‘ 6’ convert to 0110 Left Shift BX 4 times BX 0000 Insert value into lower 4 bits of BX BX 0000 0110 73 CAP 221 2/22/2021

Demonstration for input 6 AB Input ‘A’ convert to 1010 Left Shift BX 4 times BX = 0000 0110 0000 Insert value into lower 4 bits of BX BX = 0000 0110 1010 74 CAP 221 2/22/2021

Demonstration for input 6 AB Input ‘B’ convert to 1011 Left Shift BX 4 times BX = 0000 0110 1010 0000 Insert value into lower 4 bits of BX BX = 0000 0110 1011 BX contains 06 AB 75 CAP 221 2/22/2021

Code XOR MOV INT BX, BX CL, 4 AH, 1 21 H ; clear BX CMP JE AL, 0 DH END_WHILE ; CR? ; input character function ; read a character WHILE_: ; convert character to binary digit CMP AL, 39 H ; a digit ? JG LETTER ; input is a digit AND JMP 76 AL, 0 FH SHIFT CAP 221 ; yes , done ; no, a letter ; convert digit to binary value ; go to insert in BX 2/22/2021

Code LETTER: SUB AL, 37 H SHL BX, CL ; convert letter to binary value SHIFT: ; make room for new value ; insert value into BX OR INT JMP END_WHILE: 77 BL, AL 21 H WHILE_ CAP 221 ; put value into low 4 bits of BX ; input a character ; loop until CR 2/22/2021

Hex Output • BX contains 16 bits…. 4 hex digit values • To output the content of BX : – Start from left – Get hold of every digit – Convert it to a hex character – Output it 78 CAP 221 2/22/2021

Algorithm for hex output FOR 4 times DO Move BH to Dl /* BX holds output value*/ shift DL 4 times to the right IF DL < 10 THEN convert to character in ‘ 0’ … ‘ 9’ ELSE convert to character in ‘A’ … ‘F’ END_IF output character rotate BX left 4 times END_FOR 79 CAP 221 2/22/2021

Demonstration ( BX Contains 4 CA 9 h) BX = 4 CA 9 = 0100 1010 1001 move BH to DL DL = 0100 1100 shift DL 4 times to the right DL = 0000 0100 convert to character & output DL = 0011 0100 = 34 h = ‘ 4’ Rotate BX left 4 times BX = 1100 1010 1001 0100 80 CAP 221 2/22/2021

Demonstration ( BX Contains 4 CA 9 h) move BH to DL DL = 1100 1010 shift DL 4 times to the right DL = 0000 1100 convert to character & output DL = 0100 0011 = 43 h = ‘C’ Rotate BX left 4 times BX = 1010 1001 0100 1100 81 CAP 221 2/22/2021

Demonstration ( BX Contains 4 CA 9 h) move BH to DL DL = 1010 1001 shift DL 4 times to the right DL = 0000 1010 convert to character & output DL = 0100 0001 = 41 h = ‘A’ Rotate BX left 4 times BX = 1001 0100 1010 82 CAP 221 2/22/2021

Demonstration ( BX Contains 4 CA 9 h) move BH to DL DL = 1001 0100 shift DL 4 times to the right DL = 0000 1001 convert to character & output DL = 0011 1001 = 39 h = ‘ 9’ Rotate BX left 4 times BX = 0100 1010 1001 83 CAP 221 2/22/2021

- In which instruction msb gets shifted to chf as well as lsb
- Cap 221
- Red shift and blue shift
- Aniline uv spectrum
- Bathochromic shift and hypsochromic shift
- Difference between arithmetic shift and logical shift
- Difference between arithmetic shift and logical shift
- Difference between arithmetic shift and logical shift
- Difference between arithmetic shift and logical shift
- Fischer angle of condyle
- Soft cap vs hard cap
- Cap compas cap vrai
- Rotate and swap instruction in 8051
- Two wheels of different sizes that rotate together
- Paint.net rotate image
- Does the sun rotate
- The moon's rotation
- Pico 8 rotate sprite
- Revolve vs rotate
- Rotate about y axis
- Reflections on a coordinate plane worksheet
- Two coins rotate on a turntable
- Squid log rotate
- Bitwise xor
- Which bird can rotate its neck backwards to a large extent
- Fast does the earth rotate
- Caused by the tilting of the earth’s axis
- Fast does the earth rotate
- Andreu cardo martinez
- Radial tire rotation
- How to rotate coordinates 90 degrees
- Lunar orbital plane
- Rotate 90 degrees counterclockwise
- Rotate image gfg
- First order logic vs propositional logic
- First order logic vs propositional logic
- First order logic vs propositional logic
- Concurrent vs sequential
- Cryptarithmetic problem logic+logic=prolog
- Project management plan example
- Majority circuit
- Combinational logic sequential logic 차이
- Combinational logic sequential logic
- Csce 221 tamu
- Phy 221 msu
- People first timesheet
- Sp 221
- Emmett school bond vote
- Epsc 221
- Ubc cpsc 221
- Csce 221 tamu syllabus
- Cpit 221
- 221 - 206
- Cpit 221
- Redondear a décimos
- Aca 221
- Fin 221
- Cse 221
- Cs 221
- Cpsc 221
- Lagr 221
- Sebuah kapasitor
- Aoi 221 gate
- Edu-221
- Gs-221
- Cse 221
- Ee 221
- 221 ad
- Gs-221
- Bio 221
- Einstein flow cytometry
- 221 bc
- If 221
- 866-221-0269
- Cps 221
- Csce 222
- Ee 221
- 133 221
- A belt of straw and ivy buds
- Ocean bank convocation center
- Counters and shift registers
- Counters and shift registers
- Shift and add multiplication
- Bedside shift report and patient satisfaction