Practical No.14 To Become Familiar With Rotate and Shift Operations Objective
Practical No.14 To Become Familiar With Rotate and Shift Operations Objective
Practical no.14
To become familiar with Rotate and shift operations
Objective:
Introduction:
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
0 Data CF
MSB
LSB
CF Data 0
CF Data
ROL (Rotate Left)
Data CF
CF Data
Data CF
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
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 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.
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: