0% found this document useful (0 votes)
47 views22 pages

Lecture 12

This document discusses shift and rotate instructions in assembly language. It covers: - Shift instructions (SHL, SAL, SHR, SAR) that move bits left or right in a register or memory location, losing bits that shift out. - Rotate instructions (ROL, ROR) that move bits left or right while maintaining all bits by shifting the bits that fall off one end back to the other end. - Examples of using shift and rotate instructions to perform operations like multiplication, division, and counting set bits. - Algorithms and assembly code for reading and displaying binary and hexadecimal numbers by shifting bits in and out of registers.

Uploaded by

Lord Beerus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views22 pages

Lecture 12

This document discusses shift and rotate instructions in assembly language. It covers: - Shift instructions (SHL, SAL, SHR, SAR) that move bits left or right in a register or memory location, losing bits that shift out. - Rotate instructions (ROL, ROR) that move bits left or right while maintaining all bits by shifting the bits that fall off one end back to the other end. - Examples of using shift and rotate instructions to perform operations like multiplication, division, and counting set bits. - Algorithms and assembly code for reading and displaying binary and hexadecimal numbers by shifting bits in and out of registers.

Uploaded by

Lord Beerus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSC-430

Computer Organization &


Assembly Language
Lecture 12

Prof. Dr. Imtiaz Ali Korejo


Email: imtiaz@[Link]
Shift / Rotate Instructions
➢ Allow to shift bits to the left or to the right in a register or memory location
➢ Bits shifted out are LOST!!
➢ When shifting to the left, all vacated bit positions are replaced with zeros
➢ When shifting to the right, all vacated bit positions are filled with the sign bit
➢ Syntax:
Opcode Destination, 1 ; Single shift /rotate
Opcode Destination, CL ; N shift/rotate bits
Where:
Destination can be 8-bit or 16-bit registers or memory variable. Shifts the
destination field a fixed number of bits N where N is the value in the CL register.
Shift Instructions
 SHL Instruction (Left Shift)
 SAL (Shift Arithmetic Left)
 SHR (Right Shift)
 SAR (Shift Arithmetic Right)
The Shift Instruction
 The SHL(shift left) instruction the bits in the destination to the
left.
Syntax:
SHL Destination, 1
SHL Destination, CL
 Effects on flags:
 SF, PF, ZF reflects the result
 AF is undefined
 CF = last bit shifted out
 OF = 1 if result changes sign on last shift
Examples
1) Shift the contents of register AL to the left by 2 bits
Before: AL: 01 CL:??
MOV CL,2 ;Puts a 3 into register CL
SHL AL,CL ;Shifts register AL 2 bits to the left
After: AL: 04 CL:02
2) Shift the contents of register DH to the left by 3 bits
Before: DH: 8Ah CL:??
MOV CL,3 ;Puts a 3 into register CL
SHL DH,CL ;Shifts register AL 3 bits to the left
After: DH: 50h CL:03
Multiplication by left shift
➢ Consider digit 235, if each digit is shifted left one position and a 0 is
attached at right end, the value will be 2350

➢ Same as Multiplying 235 by 10

➢ Left shift on a binary number means multiplying the number by 2

➢ Example: If AL = 5d, after left shift AL = 10d, after another left shift AL
= 20d
The SAL Instruction

➢ Used when numeric multiplication is intended


➢ Both SHL and SAL instructions generates same machine code.
➢ Example: Multiply AX by 8
MOV CL, 3
SAL AX, CL
➢ Example
AL =0FFh (-1) MOV AL,0FFh
CL = 3 MOV CL,3
AL=F8h (-8) SAL AL,CL
The SHR Instruction
➢ SHR shifts bits in the destination field to the right. All vacated bit
positions on the left are filled with zeros. Used for unsigned division.
➢ A 0 is shifted into MSB and rightmost bit is shifted to CF.
➢ The effect on flag is same as SHL.
➢ Single shift to the right: SHR destination,1 SAR destination,1
➢ Shift of more than 1 bit to the right:
SHR destination,CL SAR destination,CL
where the number of bits to be shifted is placed in the CL register
• Effect in flags: SF, ZF, PF reflect the result
CF = last bit shifted out
OF = 1 if result changes the sign bit on the last shift
Examples
➢ Shift the contents of register AL to the right by 3 bits
• Before: AL: 08 CL:??
MOV CL,3 ;Puts a 3 into register CL
SHR AL,CL ;Shifts register AL 3 bits to the right
• After: AL: 01 CL:03
➢ Example:
➢ DH = 8Ah
➢ CL = 2
➢ After executing instruction: SHR DH, CL:
➢ CF = 1 and DH = 22h
➢ Erase rightmost two bits and add two 0 bits to the left end
➢ If an unsigned interpretation is being given, use SHR.
The SAR Instruction
➢ Operates like SHR, with one difference: the MSB retains its original
value.
➢ If number is even, one right shift is same as divide the number by 2.
➢ If number is odd, one right shift halves it and rounds down to
nearest integer.
➢ Example:
BL = 0000 0101b = 5d
After one right shift:
BL = 0000 0010b = 2d
➢ If an signed interpretation is being given, use SAR. (preserves the
MSB)
Examples
➢ Use right shift to divide unsigned number 65143 by 4. Put quotient in AX.
➢ Solution:
MOV AX, 65143
MOV CL, 2
SHR AX, CL
➢ If AL contains -15, give the decimal value of AL after SAR AL, 1 is
performed.
➢ Solution:
The instruction will divide –15 by 2 and round it down to –8
AL = 1111 0001b
AL = 1111 1000b = – 8
RoL Instruction
➢ ROL (Rotate left)
➢ ROL instructions shift bits to the left but no bits are lost.
➢ On left shifts bits shifted out on the left come back into the register
on the right.
➢ MSB is shifted into the rightmost bit
➢ CF also gets the bit shifted out of the MSB
Syntax: ROL destination, 1 ROL destination, CL
Example
MOV AL,40h ; AL = 0100 0000B
ROL AL,1 ; AL = 1000 0000B, CF = 0
ROL AL,1 ; AL = 0000 0001B , CF = 1
ROL AL,1 ; AL = 0000 0010B, CF = 0
Examples
 Use ROL to count the number of 1 bits in BX, without changing BX.
Put answer in AX
 Solution:
XOR AX,AX
MOV CX, 16
TOP:
ROL BX, 1
JNC NEXT
INC AX
NEXT:
LOOP TOP
ROR Instruction
➢ ROTATE instructions shift bits to the right but no bits are lost.
➢ On right shifts bits shifted out on the right come back into the register on
the left. OR
➢ The rightmost bit is shifted into MSB and also into CF.

Syntax: ROR destination, 1 ROR destination, CL


Example:
MOV AL, 01h ; AL = 0000 0001B
ROR AL,1 ; AL = 1000 0000B , CF = 1
ROR AL, 1 ; AL = 0100 0000B , CF = 0
RCL and RCR Instructions
➢ Shifts the bit of destination to the left.
➢ The RCL(Rotate carry left) instruction shifts each bit to the left, copies the
Carry flag to the LSB, and copies the MSB into the Carry flag

Syntax: RCL destination, 1 RCL destination, CL


Example:
MOV BL, 88h ; CF = BL = 0 1000 1000B
RCL BL,1 ; CF = BL = 1 0001 0000B
RCL BL,1 ; CF = BL = 0 0010 0001B
An application of reversing bit pattern

 AL = 1101 1100, Required = 0011 1011

MOV CX, 8
REVERSE:
SHL AL, 1
RCR BL, 1
LOOP REVERSE
MOV AL, BL
Binary Input
 Algorithm to read a binary number from keyboard and stored it in BX
Clear BX
Input a character
While character <> CR Do
Convert character to binary value
Left shift BX
Insert value into LSB of BX
Input a character
End_While
Binary Input Contd..
 Assembly Language Code for Binary Input:
XOR BX, BX
MOV AH, 1
INT 21h
WHILE_:
CMP AL, 0Dh
JE END_WHILE
AND AL, 0Fh ;convert to binary value
SHL BX, 1
OR BL,AL
INT 21h
JMP WHILE_
END_WHILE:
Binary Output
 Algorithm:
FOR 16 TIMES DO
ROTATE LEFT BX
IF CF = 1
THEN
OUTPUT ‘1’
ELSE
OUTPUT ‘0’
END_IF
END_FOR
Hex input
 Assumptions:
 Only uppercase letters
 Maximum four hex characters
 Algorithm for Hex Input:
CLEAR BX
Input Hex Character
WHILE Character <> CR DO
Convert Character To Binary Value
Left Shift BX Four Times
Insert Value Into Lower 4 Bits Of BX
Input A Character
END_WHILE
Hex Output
 Algorithm:
For 4 Times Do
Move BH to DL
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
THE END.

You might also like