0% found this document useful (0 votes)
137 views

Logic Shift and Rotate Instructions

The document discusses various logic, shift, and rotate instructions in assembly language. It explains how AND, OR, and XOR instructions can be used to selectively modify bits in a destination using a mask. It also covers left and right shift instructions for multiplication and division, as well as rotate instructions and rotate through carry instructions. Examples are provided to demonstrate how to use these instructions to clear, set, or complement bits, reverse a bit pattern, count set bits, and manipulate values.

Uploaded by

Md Mahfuz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Logic Shift and Rotate Instructions

The document discusses various logic, shift, and rotate instructions in assembly language. It explains how AND, OR, and XOR instructions can be used to selectively modify bits in a destination using a mask. It also covers left and right shift instructions for multiplication and division, as well as rotate instructions and rotate through carry instructions. Examples are provided to demonstrate how to use these instructions to clear, set, or complement bits, reverse a bit pattern, count set bits, and manipulate values.

Uploaded by

Md Mahfuz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Logic, Shift, and

Rotate Instructions
By
Sakifa Aktar
Lecturer
Intro
• Instructions that can be used to change the bit pattern in a byte or
word
• The ability to manipulate bits is generally absent in high-level
languages (except C), and is an important reason for programming in
assembly language
• We can change individual bits in computer by using logic operations
• The binary values of 0 and 1 are treated as false and true, respectively
Ex: 7.1
Truth Tables for AND, OR, XOR, and NOT
AND, OR, and XOR Instructions
• One use of AND, OR and XOR is to selectively modify the bits in the
destination
• To do this, we construct a source bit pattern known as :mask. The
mask bits are chosen so that the corresponding destination bits are
modified in the desired manner when the instruction is executed.
• To choose the mask bits, we make use of the following properties of
AND, OR, and XOR:
• Clear:
• AND instruction can be used to clear specific destination bits while preserving
the others
• A 0 mask bit clears the corresponding destination bit; a 1 mask bit preserves
the corresponding destination bit
• Set:
• OR instruction can be used to set specific destination bits while preserving the
others
• A 1 mask bit sets the corresponding destination bit; a 0 mask bit preserves the
corresponding destination bit
• Complement:
• XOR instruction can be used to complement specific destination bits while
preserving the others
• A 1 mask bit complements the corresponding destination bit; a 0 mask bit
preserves the corresponding destination bit
Clear(And)
• 0=clear
• 1=preserve
Set(or)
0=preserve
1=sets
Complement
0=preserve
1=complement
• Ex: 7.2: Clear the sign bit of AL while leaving the other bits unchanged
• Solution: Use the AND instruction with 01111111b = 7Fh as the mask.
Thus:
AND AL,7Fh
• Ex: 7.3: Set the most significant and least significant bits of AL while
preserving the other bits
• Solution: Use the OR instruction with 10000001b =81h as the mask.
Thus:
OR AL,81h
• Ex: 7.4: Change the sign bit of DX
• Solution: Use the XOR instruction with a mask of 8000h. Thus:
XOR DX,8000h
Converting an Lower Case Letter to Upper
Case (Lab Work)
Clearing a Register
• We already know two ways to clear a register:
• MOV AX,0
• SUB AX,AX
• Using the fact that 1 XOR 1 = 0 and 0 XOR 0 = 0, a third way is:
• XOR AX,AX
• Because of the prohibition on memory-to-memory operations this way is only
applicable for register to register operation
• Can you think of another way to clear a register?
NOT Instruction
• The NOT instruction performs the one's complement operation on the
destination
• There is no effect on the status flags
• Ex: 7.5: Complement the bits In AX
• NOT AX
TEST Instruction
• The TEST Instruction performs an AND operation of the destination
with the source but does not change the destination contents
• The purpose of the TEST instruction is to set the status flags
• The TEST instruction can be used to examine individual bits in an
operand
• The mask should contain 1's In the bit positions to be tested and 0's
elsewhere:
• Because b AND 1 = b, b AND 0 = 0, will have 1's in the tested bit positions if
and only if the destination has 1’s in these positions; it will have 0's elsewhere
• If destination has O's in all the tested position, the result will be 0 and so ZF =
1.
Ex: 7.6
• Jump to label BELOW If AL contains an even number.
• Solution: Even numbers have a 0 in bit 0(LSB). Thus, the mask is
000000001b:
TEST AL, 1 ;is AL even?
JZ BELOW ;yes, go to BELOW

JZ= jump if equal to zero.


1010=10=EVEN
0111=07=ODD
Shift Instructions
• The shift instructions shift the bits in the destination operand by one or
more positions either to the left or right
• The bits shifted out are lost
• For a single shift or rotate, the form is :
• Opcode destination,1
• For a shift or rotate of N positions, the form is:
• Opcode destination, CL
• Where CL contains N.
• In both cases, destination is an 8- or 16-bit register or memory location
Left Shift Instructions
• The SHL Instruction: The SHL (shift left) instruction shifts the bits in
the destination to the left:
• SHL destination,1
• SHL destination, CL
• A 0 is shifted into the rightmost bit position and the msb is shirted
into CF
Ex: 7.7
• Prob: Suppose DH contains 8Ah and CL contains 3. What are the values of DH
and of CF after the instruction SHL DH,CL is executed?
• Solution: The binary value of DH Is 10001010. After 3 left shifts, CF
will contain 0. The binary value of DH Is 01010000. After 3 left shifts, CF will
contain 0

• 8Ah=10001010b
• CF= 1, 00010100 shift 1
• CF=0, 00101000 shift 2
• CF=0, 01010000 shift 3
Multiplication by Left Shift
• A left shift on a binary number multiplies it by 2
• 2*destination = shift left operation
• For example, suppose that AL contains 5 = 00000101b.
• A left shift gives 00001010b =10, thus doubling Its value
• Another left shift yields 00010100= 20d, so it is doubled again
• To emphasize the arithmetic nature of the operation the opcode SAL
(shift arithmetic left) is often used in instances where numeric
multiplication is intended
• Both instructions (SHL, SAL) generate the same machine code
• Negative numbers can also be multiplied by powers of 2 by left shift
Overflow in Multiplication by Shifts
• The overflow flags are not reliable indicators for a multiple left shift
• A multiple shift is really a series of single shifts, and CF only reflect the
result of the last shift
• For example, if BL contains 80h, CL contains 2 and we execute SHL
BL,CL, then CF =OF =0 even though both signed and unsigned
overflow occur.
Ex 7.8
• Write some code to multiply the value of AX by 8. Assume that
overflow will not occur.
• Solution: To multiply by 8, we need to do three left shifts.
MOV CL, 3 ;number of shifts to do
SAL AX,CL ;multiply by 8=2^3
00001000=08h * 8
Right Shift Instructions
• The instruction SHR
(shift right) performs
right shifts on the
destination operand:
• SHL destination,1
• SHL destination, CL
• A 0 Is shifted Into the
msb position, and the
rightmost bit Is shifted into CF
Ex: 7.9
• Suppose DH contains 8Ah and CL contains 2. What are the values of
DH and CF after the Instruction SHR DH,CL is executed?
• Solution: The value of DH in binary Is 10001010. After two right shifts,
CF = 1. The new value of DH Is obtained by erasing the rightmost two
bits and adding two 0 bits to the left end, thus DH = 00100010b = 22h.
The SAR Instruction: Division by Right Shift
• The SAR Instruction (shift arithmetic right) operates like SHR, with one
difference: the msb retains Its original value
• Because a left shift doubles the destination's value, it's reasonable to
guess that a right shift might divide it by 2
• This Is correct for even numbers
• For odd numbers, a right shift halves It and rounds down to the
nearest integer
• For example, if BL contains 00000101b = 5, then after a right shift. BL
will contain 00000010 = 2 {floor}
Signed and Unsigned Division
• In doing division by
right shifts, we need to
make a distinction
between signed and
unsigned numbers
• If an unsigned
interpretation is being
given, SHR should be used Fig: SAR

• For a signed interpretation, SAR must be used, because it preserves


the sign
• Ex 7.10: Use right shifts to divide the unsigned number 65143 by 4. Put
the quotient in AX
• Solution: To divide by 4, two right shifts are needed. Since the dividend
is unsigned, we use SHR. The code is:
MOV AX,65143 ; AX has number
MOV CL,2 ;CL has number of right shifts
SHR AX,CL ;divide by 4
• If AL contains -15, give the decimal value of AL after SAR AL, 1 is
performed
• Solution: Execution of SAR AL,1 divides the number by 2 and rounds
down. Dividing -15 by 2 yields -7.5, and after rounding down we get -8.
In terms of the binary contents:
• we have -15“’ 11110001. After shifting, we have 11111000b= -8.
• -15=11110001
• 11111000 CF=1, -8
Rotate Instructions
• Rotate Left: The instruction ROL (rotate only left) shifts bits to the left
• The msb is shifted into the rightmost bit
• The CF also gets the bit shifted out of the msb
• Rotate Right: The instruction ROR (rotate only right) works just like
ROL, except that the bits are rotated to the right.
• The rightmost bit is shifted into the msb, and also into the CF
Fig: ROL

Fig: ROR
Ex: 7.12
• Use ROL to count the number of 1 bits In BX, without changing BX.
Put the answer In AX.
Rotate Carry Left (RCL)
• The Instruction RCL (Rotate through Carry Left) shifts the bits of the
destination to the left
• The msb is shifted Into the CF, and the previous value of CF ls shifted
Into the rightmost bit
• In other words, RCL works like
• Just like ROL, except that CF is part of the circle of bits being rotated
• The instruction RCR (Rotate through Carry Right) works just like RCL,
except that the bits are rotated to the light
Fig: RCL

Fig: RCR
Ex: 7.13
• Suppose DH contains 8Ah, CF= l, and CL contains 3. What are the
values of DH and CF after the instruction RCR DH,CL is executed?
An Application: Reversing a Bit Pattern
• For example, if AL contains 11011100, we want to make it 00111011
• How to write any bit pattern in reverse order with shift operation.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy