Docsity

Shift and Rotate instructions-Assembly Language Programming-Lab Manual, Exercises of Assembly Language Programming

Toggle sidebar
bg1
2
EXPERIMENT NO 10
Shift and Rotate instructions
1. Objectives
To use different shift and rotate instructions
To use IMUL instruction for multiplication operations
2. Basic Information
In last lab you learned how to manipulate individual bits in an integer, using Boolean operators. In
this lab you will learn how to move bits around in a number, using shift and rotate operations.
Operations such as these are particularly useful when controlling various types of hardware
devices. Assembly language programmers are experts with bits.
2.1. The Shift operations
The shift operations are used to multiply or divide a number by another number that is a power of
2 (i.e., 2n or 2-n). Multiplication by 2 is achieved by a one-bit left shift, while division by 2 is
achieved by a one-bit right shift. The Shift Arithmetic Right (SAR) instruction is used to
manipulate signed numbers. The regular Right Shift (SHR) of a signed numbers effects the sign
bit, which could cause numbers to change their sign. The SAR preserves the sign bit by filling the
vacated bits with the sign of the number. Shift Arithmetic Left (SAL) is identical in operation to
SAR.
2.2. Rotate Operations
The rotate operations are very similar to the shift operations, but the bits are shifted out from one
end of a number and fed back into the other end to fill the vacated bits. They are provided to
facilitate the shift of long numbers (i.e., numbers of more than 16 bits). They are also used to
reposition a certain bit of a number into a desired bit location. The rotate right or left instructions
through the carry flag (RCL and RCR) are similar to the regular rotate instructions (ROL and
ROR), but the carry flag is considered as a part of the number. Hence before the rotate operatin,
the carry flag bit is appended to the number as the least significant bit in the case of RCL, or as
most significant bit in case of RCR.
docsity.com
pf3

Partial preview of the text

Download Shift and Rotate instructions-Assembly Language Programming-Lab Manual and more Exercises Assembly Language Programming in PDF only on Docsity!

EXPERIMENT NO 10

Shift and Rotate instructions

1. Objectives

 To use different shift and rotate instructions  To use IMUL instruction for multiplication operations

2. Basic Information

In last lab you learned how to manipulate individual bits in an integer, using Boolean operators. In this lab you will learn how to move bits around in a number, using shift and rotate operations. Operations such as these are particularly useful when controlling various types of hardware devices. Assembly language programmers are experts with bits.

2.1. The Shift operations

The shift operations are used to multiply or divide a number by another number that is a power of 2 (i.e., 2n or 2-n). Multiplication by 2 is achieved by a one-bit left shift, while division by 2 is achieved by a one-bit right shift. The Shift Arithmetic Right (SAR) instruction is used to manipulate signed numbers. The regular Right Shift (SHR) of a signed numbers effects the sign bit, which could cause numbers to change their sign. The SAR preserves the sign bit by filling the vacated bits with the sign of the number. Shift Arithmetic Left (SAL) is identical in operation to SAR.

2.2. Rotate Operations

The rotate operations are very similar to the shift operations, but the bits are shifted out from one end of a number and fed back into the other end to fill the vacated bits. They are provided to facilitate the shift of long numbers (i.e., numbers of more than 16 bits). They are also used to reposition a certain bit of a number into a desired bit location. The rotate right or left instructions through the carry flag (RCL and RCR) are similar to the regular rotate instructions (ROL and ROR), but the carry flag is considered as a part of the number. Hence before the rotate operatin, the carry flag bit is appended to the number as the least significant bit in the case of RCL, or as most significant bit in case of RCR.

2.3. Multiplication instructions

2.3.1 MUL Instruction

The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, and EAX. The single operand is the multiplier. The following table shows the default multiplicand table shows the default multiplicand and product, depending on the size of the multiplier:

Multiplicand Multiplier Product AL r/m8 AX AX r/m16 DX:AX EAX r/m32 EDX:EAX

The register(s) holding the product are twice the size of multiplicand and multiplier, guaranteeing that overflow will never occur.

2.3.2 IMUL Instruction

The IMUL (signed multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, EAX. It has the same syntax and uses the same operands as the MUL instruction. What is different is that it preserves the sign of the product.