0% found this document useful (0 votes)
77 views6 pages

Practical No.14 To Become Familiar With Rotate and Shift Operations Objective

This document discusses shift, rotate, and loop commands in assembly language. It provides examples of (1) shifting and rotating values in registers using commands like SHL, SHR, ROL, ROR; and (2) using loop commands to repeatedly increment/decrement values or wait for a period of time. Exercises are provided to practice these commands, including finding the greatest common divisor of two numbers using a loop.

Uploaded by

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

Practical No.14 To Become Familiar With Rotate and Shift Operations Objective

This document discusses shift, rotate, and loop commands in assembly language. It provides examples of (1) shifting and rotating values in registers using commands like SHL, SHR, ROL, ROR; and (2) using loop commands to repeatedly increment/decrement values or wait for a period of time. Exercises are provided to practice these commands, including finding the greatest common divisor of two numbers using a loop.

Uploaded by

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

Department of: Subject of:

Computer Systems Engineering Computer Architecture and Assembly


Programming
Mehran University of Engineering Year 2nd Semester 3rd
&Technology
Batch 19CS Duration 03
Hours
Jamshoro

Practical no.14
To become familiar with Rotate and shift operations
Objective:

To get familiar with Rotate and Shift commands in assembly


language. To use loops in complex problems.

Introduction:

Shift and Rotate command:

Shift and Rotate commands are used to convert a number to another form where
some bits are shifted or rotated. Basic difference between shift and rotate is shift
command makes “fall of” bits at the end of register. Where rotate command makes
“Wrap around” at the end of the register. There are both arithmatic (SAL and SAR)
and logical (SHL and SHR) shift instructions. Graphical operation for these
commands are shown below.

MSB LSB

CF Data 0

SHL (Shift Logical Left)

0 Data CF

SHR (Shift Logical Right)

MSB
LSB

CF Data 0

SAL (Shift Arithmatic Left)


Data CF

SAR (Shift Arithmatic Right)

CF Data
ROL (Rotate Left)

Data CF

ROR (Rotate Right)

CF Data

RCL (Rotate Through Carry Left)

Data CF

RCR (Rotate Through Carry

Right) Some simple codes can be given to clarify the idea.

MOV CL,03H ;
MOV AX,02F3H ; In binary 0000 0010 1111 0011
SHR AX,CL ; In binary 0000 0000 0101 1110

In this procedure, SHR commands inserts 0’s from right side. Each time a 0 is
inserted left most bit is vanished from register content.

MOV CL,03H ;
MOV AX,82F3H ; In binary 1000 0010 1111 0011
SAR AX,CL ; In binary 1111 0000 0101 1110

In this procedure, SHR commands inserts MSB content from right side. Each time it
is inserted left most bit is vanished from register content.

MOV CL,03H ;
MOV AX,82F3H ; In binary 1000 0010 1111 0011
ROR AX,CL ; In binary 0111 0000 0101 1110

The whole procedure can be visualized as follows.


1 0 0 0 0 0 1 0 1 1 1 1 0 0 1 1

0 1 1 1 0 0 0 0 0 1 0 1 1 1 1 0

Here rotate by 3 operation is shown. It is clearly seen that every bit is assigned to
a new position that is 3 places away from previous one. Unlike the shift command
no right bit is destroyed. It is placed in the leftmost position.

Exercise part 1:

(a) Program 1:

CODE SEGMENT
ASSUME CS:CODE

MOV CL,02H
MOV
AX,105AH SHL
AX,CL HLT

CODE ENDS
END

Obtain AX register value abd write the previous value and present value in
binary form. What type of operation is this?

(b) Program 2:

CODE SEGMENT
ASSUME CS:CODE

MOV CL,04H
MOV AX,564AH
SAL AX,CL
HLT

CODE ENDS
END
Obtain AX register value and write the previous value and present value in
binary form. What type of operation is this?

(c) Perform for similar values of AX and CL with ROL, ROR. RCL, RCR command.

LOOP in assembly language:

Loop commands are used to perform same operation again and again. This is like
for, while type instructions in ‘C’ or ‘MATLAB’. A common example can be shown as,

MOV CX,0100D
MOV AX,564AH
Lev: DEC AX
Loop LEV
HLT

Here CX acts as a count register. Loop Lev instruction leads instruction to go back
to Lev level until CX is zero. Each time Lev level is executed CX is decreased by 1.
Loop command can be used for waiting purposes. Such as,

MOV CX,0100D
Wt: NOP
Loop Wt
HLT

Here the loop is executed until CX is zero. If 1 loop takes 1ms, the program will wait
for 100ms.

Exercise part 2:

(a) Program 1:

CODE SEGMENT
ASSUME CS:CODE

MOV
AX,1025H
MOV
BX,475AH
MOV CX,50H
Lev: INC AX
DEC BX
LOOP Lev
HLT

CODE ENDS
END
Observe the operation of this code. What happens when the loop is executed again
and again.

(b) Program 2: This code is to find GCD of two numbers.

CODE SEGMENT
ASSUME CS:CODE

MOV AX,5H
MOV BX,3H
Lev: XOR DX,DX
DIV BX
MOV AX,BX
MOV BX,DX
TEST DX,0H
JNZ Lev
HLT
CODE ENDS
END

Here GCD of 5 and 3 are found. You can change the values of AX and BX
and obtain the result for any other values. Find GCD of 08D4H and 235H.
Result:

You might also like