Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views40 pages

Assembly_Lecture_07

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 40

Lecture # 07

Logic, Shift, and


Rotate Instructions
Engr. Fiaz Khan

Engr. Muhammad Fiaz 1


Overview
• Instructions in assembly language allow us to change the bit pattern in a byte or word.
• Manipulating bits is often not possible in high-level languages except for C, making assembly
language important.
• Section 7.1 introduces logic instructions like AND, OR, XOR, and NOT, which clear, set, and
examine bits in a register or variable. They're used for tasks like converting lowercase letters to
uppercase and determining if a number is even or odd.
• Section 7.2 covers shift instructions, allowing bits to be shifted left or right in registers or memory
locations. Left shifts double a number, while right shifts halve it, providing a way to multiply and
divide by powers of 2.
• Section 7.3 discusses rotate instructions, similar to shifts but with shifted-out bits re-entering at
the other end. These are useful for examining and changing bits or groups of bits.
• Section 7.4 demonstrates using logic, shift, and rotate instructions for binary and hexadecimal
input/output, enabling the solving of a wide range of problems involving number manipulation.

Engr. Muhammad Fiaz 2


Logic Instructions
• Logic instructions, such as AND, OR, XOR, and NOT, are essential in
assembly language.
• These instructions allow us to manipulate individual bits in a
computer.
• In binary, 0 represents false and 1 represents true.
• The truth tables for AND, OR, XOR, and NOT operations show how
they behave with different inputs.
• When these logic operations are applied to 8- or 16-bit operands,
they are performed on each bit position separately.

Engr. Muhammad Fiaz 3


Engr. Muhammad Fiaz 4
Truth Tables for AND, OR, XOR, and NOT
(0 = false, 1 = true)

Engr. Muhammad Fiaz 5


AND, OR, and XOR Instructions
• AND, OR, and XOR instructions in assembly language perform specific logic
operations.
• The formats for these instructions are:
• AND destination, source
• OR destination, source
• XOR destination, source
• The result of the operation is stored in the destination, which must be a
register or memory location.
• The source can be a constant, register, or memory location, but memory-
to-memory operations are not permitted.

Engr. Muhammad Fiaz 6


Effect on flags:
• SF, ZF, PF reflect the result
• AF is undefined
• CF, OF= 0

Engr. Muhammad Fiaz 7


Mask
• Instructions like AND, OR, and XOR can selectively modify bits in the
destination.
• To achieve this, we create a source pattern called a mask.
• The mask is designed so that when the instruction is executed, it alters
the destination bits as desired.
• Properties of AND, OR, and XOR operations help in choosing the mask
bits:
• AND clears specific destination bits while preserving others.
• OR sets specific destination bits while preserving others.
• XOR complements specific destination bits while preserving others.

Engr. Muhammad Fiaz 8


Engr. Muhammad Fiaz 9
Engr. Muhammad Fiaz 10
Engr. Muhammad Fiaz 11
Converting an ASCII Digit to a Number
• When a program reads a character from the keyboard, AL gets its ASCII
code.
• For digit characters, such as '0' to '9', AL receives codes like 30h to 39h
instead of the actual digit.
• To convert these ASCII digits to their decimal values, we can use the
SUB or AND instruction.
• Using the AND instruction to clear the high nibble of AL effectively
converts ASCII digits to decimal values.
• This method works because the ASCII codes for '0' to '9' have a
consistent pattern.
• Emphasizing the use of AND over SUB makes the program more
readable by indicating that we're modifying the bit pattern of AL.
• The reverse problem of converting a stored decimal digit back to its
ASCII code is left for practice.

Engr. Muhammad Fiaz 12


Converting a Lowercase Letter to Upper Case
• ASCII codes for lowercase letters 'a' to 'z' range from 61h to 7Ah, and for
uppercase letters 'A' to 'Z' from 41h to 5Ah.
• To convert a lowercase letter to uppercase, traditionally, we could
subtract 20h from its ASCII code.
• However, comparing the binary codes of corresponding lowercase and
uppercase letters reveals that we only need to clear bit 5 to perform the
conversion.
• This can be achieved using the AND instruction with the mask
11011111b or 0DFh.
• So, to convert a lowercase character in DL to uppercase, execute AND DL,
0DFh.
• The reverse problem of converting from uppercase to lowercase is left as
an exercise.

Engr. Muhammad Fiaz 13


Clearing a Register
• There are multiple ways to clear a register, such as AX.
• Traditionally, we can clear AX by either
• MOV AX, 0 or
• SUB AX, AX.
• Alternatively, we can use XOR AX, AX, exploiting the property
that 1 XOR 1 equals 0 and 0 XOR 0 equals 0.
• The latter two methods (SUB AX, AX and XOR AX, AX)
are more efficient in terms of machine code, requiring only
two bytes compared to three bytes for MOV AX, 0.
• However, when clearing a memory location, the first method
(MOV AX, 0) must be used due to the prohibition on
memory-to-memory operations.

Engr. Muhammad Fiaz 14


Testing a Register for Zero
• Executing an instruction like OR CX, CX might seem
unnecessary because it doesn't change the contents of
CX.
• However, it affects the zero flag (ZF) and sign flag (SF).
• Specifically, if CX contains 0, then ZF becomes 1.
• Therefore, OR CX, CX can be used as an alternative to
CMP CX, 0 to test if the content of a register is zero or
to check the sign of the contents.

Engr. Muhammad Fiaz 15


Not Instruction
• The NOT instruction performs the one's complement
operation on the destination.
• The format for using NOT is: NOT destination.
• This operation flips all the bits in the destination
operand.
• There is no effect on the status flags (such as zero flag,
carry flag, etc.) when NOT instruction is executed.

Engr. Muhammad Fiaz 16


Test Instruction
• The TEST instruction performs an AND operation of the
destination with the source but doesn't change the destination
contents.
• Its purpose is to set the status flags based on the result.
• The format for TEST instruction is: TEST destination, source.
• The flags affected by TEST are: SF, ZF, and PF reflect the result, AF
is undefined, CF and OF are set to 0.
• TEST can be used to examine individual bits in an operand using a
mask containing 1's in the bit positions to be tested and 0's
elsewhere.
• If the tested bit positions in the destination have 1's, the result
will have 1's in those positions; otherwise, it will have 0's.
• If the destination has 0's in all tested positions, ZF will be set to 1.

Engr. Muhammad Fiaz 17


Engr. Muhammad Fiaz 18
Shift Instructions
• Shift and rotate instructions move the bits in the destination operand left
or right by one or more positions.
• In shift instructions, the bits shifted out are lost, while in rotate
instructions, the shifted-out bits are put back into the other end.
• There are two formats for these instructions: one for a single shift/rotate
and another for shifting/rotating by a specified number of positions.
• The destination can be an 8- or 16-bit register or memory location, and in
some cases, an 8-bit constant can also be used.
• These instructions are useful for multiplying and dividing by powers of 2,
and for binary and hexadecimal input/output in programs.

Engr. Muhammad Fiaz 19


The SHL Instruction
• The SHL (shift left) instruction moves the bits in the destination operand to
the left.
• For a single shift, the format is SHL destination, 1. This shifts all bits to the
left, with a 0 shifted into the rightmost bit position.
• If the shift count N is different from 1, the instruction takes the form SHL
destination, CL, where CL contains N. Multiple shifts are then performed
based on the value in CL.
• After the shift operation, the value of CL remains unchanged.
• Flags affected by SHL are SF, PF, and ZF reflecting the result. AF is
undefined, CF holds the last bit shifted out, and OF is set to 1 if the result
changes sign on the last shift.

Engr. Muhammad Fiaz 20


Engr. Muhammad Fiaz 21
Explanation of Example 7.7
• Initially, DH contains the hexadecimal value 8Ah, which in binary is
10001010.
• CL contains the value 3.
• The instruction SHL DH, CL is executed, which means DH will be shifted to
the left by CL (3) positions.
• After 3 left shifts, the three leftmost bits will be shifted out. CF will contain
the value of the last bit shifted out, which is 0.
• The new contents of DH can be obtained by erasing the leftmost three bits
and adding three zero bits to the right end.
• Therefore, DH will contain the binary value 01010000b, which is equivalent
to the hexadecimal value 50h.

Engr. Muhammad Fiaz 22


SHL - SAL
• Left shifting a decimal number multiplies it by a power of 10, adding a zero
to the right end.
• Similarly, left shifting a binary number multiplies it by 2, shifting all bits one
position to the left.
• For example, left shifting the binary number 5 (00000101b) by one position
yields 10 (00001010b), doubling its value.
• The SAL (Shift Arithmetic Left) instruction performs left shifts and can be
used for multiplying an operand by multiples of 2.
• Although SAL emphasizes the arithmetic nature of the operation, the SHL
(Shift Left) instruction is commonly used interchangeably and generates
the same machine code.
• Negative numbers can also be multiplied using left shifts. For instance, left
shifting -1 (FFFFh) produces -8 (FFF8h).

Engr. Muhammad Fiaz 23


Overflow
• Left shifts can cause overflow when treated as multiplication operations.
• For a single left shift, the Carry Flag (CF) and Overflow Flag (OF) accurately
indicate unsigned and signed overflow, respectively.
• However, when multiple left shifts are performed, the overflow flags may
not provide reliable indications.
• This is because multiple shifts are executed as a series of single shifts, and
CF and OF only reflect the result of the last shift.
• For example, if BL contains 80h, CL contains 2, and we execute SHL BL, CL,
then both CF and OF will be 0, even though both signed and unsigned
overflow occur.

Engr. Muhammad Fiaz 24


Engr. Muhammad Fiaz 25
The SHR (shift right) Instruction
• The SHR (shift right) instruction performs right shifts on the
destination operand.
• For a single right shift, the format is SHR destination, 1.
• In a single shift, a 0 is shifted into the most significant bit position,
and the rightmost bit is shifted into the Carry Flag (CF).
• If the shift count N is different from 1, the instruction takes the form
SHR destination, CL, where CL contains N. Multiple right shifts are
then performed based on the value in CL.
• The effect on the flags (SF, ZF, PF, AF, CF, OF) is the same as for SHL
(Shift Left).

Engr. Muhammad Fiaz 26


Engr. Muhammad Fiaz 27
The SAR (Shift Arithmetic Right) Instruction
• The SAR (Shift Arithmetic Right) instruction is similar to SHR (Shift Right), but with one
difference: the most significant bit (MSB) retains its original value.
• The syntax for SAR is SAR destination, 1 for a single shift, and SAR destination, CL for
multiple shifts.
• The effect on flags (SF, ZF, PF, AF, CF, OF) is the same as for SHR.
• Division by right shift can be achieved using SAR. For even numbers, a right shift divides
the value by 2. For odd numbers, it also divides by 2 but rounds down to the nearest
integer.
• For example, if BL contains 00000101b (5 in decimal), then after a right shift, BL will
contain 00000010b (2 in decimal).
• When performing division by right shifts, a distinction must be made between signed
and unsigned numbers.
• For unsigned interpretation, SHR should be used. For signed interpretation, SAR must be
used because it preserves the sign.

Engr. Muhammad Fiaz 28


Engr. Muhammad Fiaz 29
Engr. Muhammad Fiaz 30
Engr. Muhammad Fiaz 31
The ROL (Rotate Left) instruction
• The ROL (Rotate Left) instruction shifts bits to
the left, with the most significant bit (MSB)
shifted into the rightmost bit. The Carry Flag
(CF) also receives the bit shifted out of the
MSB.
• ROL creates a circular arrangement of bits,
where the least significant bit follows the
MSB in the circle.
• Syntax: ROL destination, 1 for a single
rotation and ROL destination, CL for multiple
rotations.

Engr. Muhammad Fiaz 32


The ROR (Rotate Right) instruction
• The ROR (Rotate Right) instruction works
similarly to ROL, but shifts bits to the right. The
rightmost bit is shifted into the MSB and into
the CF.
• ROR also creates a circular arrangement of bits,
with the MSB following the least significant bit.
• Syntax: ROR destination, 1 for a single rotation
and ROR destination, CL for multiple rotations.
• In both ROL and ROR instructions, CF reflects
the bit that is rotated out, allowing inspection of
bits in a byte or word without changing their
contents.

Engr. Muhammad Fiaz 33


Engr. Muhammad Fiaz 34
The RCL (Rotate through Carry Left) instruction
• The RCL (Rotate through Carry Left) instruction
shifts the bits of the destination to the left.
• Similar to ROL (Rotate Left), the MSB is shifted
into the Carry Flag (CF), and the previous value
of CF is shifted into the rightmost bit.
• RCL operates like ROL, but with CF being
included in the rotation of bits.
• Syntax: RCL destination, 1 for a single rotation
and RCL destination, CL for multiple rotations.

Engr. Muhammad Fiaz 35


The RCL (Rotate through Carry Left) instruction
• The RCR (Rotate through Carry Right)
instruction rotates the bits of the destination
to the right.
• Similar to RCL (Rotate through Carry Left),
the Carry Flag (CF) is involved in the rotation.
• RCR operates like RCL but shifts bits to the
right.
• Syntax: RCR destination, 1 for a single
rotation and RCR destination, CL for multiple
rotations.

Engr. Muhammad Fiaz 36


Engr. Muhammad Fiaz 37
An Application: Reversing a Bit Pattern
• Reversing a bit pattern in a byte or word involves changing its order,
such as turning 11011100 into 00111011.
• An approach to reverse the bit pattern is by using shift and rotate
instructions.
• By shifting the bits of the original byte or word leftward with SHL and
then rotating them into another register with RCR, the reversed bit
pattern can be obtained.
• This process is repeated for each bit position, typically eight times for a
byte.
• Finally, the reversed bit pattern in the secondary register can be copied
back into the original register.
• The loop is used to repeat the shifting and rotating process until all bits
have been reversed.

Engr. Muhammad Fiaz 38


Binary and Hex I/O
• Shift and rotate instructions are useful in binary and hexadecimal input/output (I/O).
• For binary input, we often read a binary number from the keyboard, where each character
represents a binary digit ('0' or '1').
• To collect these bits into a register, we can use an algorithm:
• Clear the register (e.g., BX) where the binary value will be stored.
• Input a character ('0' or '1').
• Repeat the following steps until the entered character is a carriage return (CR):
• Convert the character to its binary value.
• Left shift the register (BX).
• Insert the binary value into the least significant bit (lsb) of the register (BX).
• Input the next character.
• End the process when the carriage return is encountered.

Engr. Muhammad Fiaz 39


ENGR. MUHAMMAD FIAZ
LECTURER COMPUTER SCIENCE

TELF/EF SET / ACEPT CERTIFIED

MICROSOFT ACADEMY TRAINER

ORACLE CERTIFIED PROFESSIONAL

GOOGLE CERTIFIED PROFESSIONAL

THANK YOU! Lahore, Pakistan

+92-320-7617-093

muhammad.fiaz@cs.uol.edu.pk
Do you have any questions?
https://www.linkedin/in/fiazofficials

Engr. Muhammad Fiaz 40

You might also like