You are on page 1of 18

Shift and Rotate instructions in 8086 :

Any machine (system) works on machine language, which consists of binary


numbers.

In the 8086 microprocessor, we have 16-bit registers to handle our data.

Sometimes, the need to perform some necessary shift and rotate


operations on our data may occur according to the given condition and
requirement.

So, for that purpose, we have various Shift and Rotate instructions present in
the 8086 microprocessor.

The various shift and rotate instructions in 8086 are:

1. SHR : Shift Right


2. SAR : Shift Arithmetic Right
3. SHL : Shift Left
4. SAL : Shift Arithmetic Left
5. ROL : Rotate Left
6. ROR : Rotate Right
7. RCL : Rotate Carry Left
8. RCR : Rotate Carry Right

1) SHR : Shift Right


The SHR instruction is an abbreviation for 'Shift Right'.

This instruction simply shifts the mentioned bits in the register to the right
side one by one by inserting the same number (bits that are being shifted) of
zeroes from the left end.

The rightmost bit that is being shifted is stored in the Carry Flag (CF).

Syntax: SHR Register, Bits to be shifted


Example:
MOV AX, 594FH
MOV CX, 02H
SHR AX, CX
Working:

2) SAR : Shift Arithmetic Right


The SAR instruction stands for 'Shift Arithmetic Right'.

This instruction shifts the mentioned bits in the register to the right side one
by one, but instead of inserting the zeroes from the left end, the MSB is
restored.

The rightmost bit that is being shifted is stored in the Carry Flag (CF).

Syntax: SAR Register, Bits to be shifted


Example: MOV AX, ABCDH
MOV C, 05H
SAR BX, CX

Working:

3) SHL : Shift Left


The SHL instruction is an abbreviation for 'Shift Left'.

This instruction simply shifts the mentioned bits in the register to the left side
one by one by inserting the same number (bits that are being shifted) of
zeroes from the right end.

The leftmost bit that is being shifted is stored in the Carry Flag (CF).
Syntax: SHL Register, Bits to be shifted
Example:
MOV AX, 9EDCH
MOV C, 04H
SHL AX, CX

Working:

4) SAL : Shift Arithmetic Left


The SAL instruction is an abbreviation for 'Shift Arithmetic Left'. This
instruction is the same as SHL.

Syntax: SAL Register, Bits to be shifted


Example:
MOV AX, 7777H
MOV CX, 03H
SAL AX, CX

Working:

5) ROL : Rotate Left


The ROL instruction is an abbreviation for 'Rotate Left'.

This instruction rotates the mentioned bits in the register to the left side one
by one such that leftmost bit that is being rotated is again stored as the
rightmost bit in the register, and it is also stored in the Carry Flag (CF).
Syntax: ROL Register, Bits to be shifted
Example:
MOV AX, 3BADH
MOV CX, 04H
ROL AX, CX

Working:

6) ROR : Rotate Right


The ROR instruction stands for 'Rotate Right'.

This instruction rotates the mentioned bits in the register to the right side one
by one such that rightmost bit that is being rotated is again stored as the MSB
in the register, and it is also stored in the Carry Flag (CF).

Syntax: ROR Register, Bits to be shifted


Example:
MOV AX, 8910H
MOV CX, 05H
ROR AX, CX

Working:
7) RCL : Rotate Carry Left
This instruction rotates the mentioned bits in the register to the left side one
by one such that leftmost bit that is being rotated it is stored in the Carry Flag
(CF), and the bit in the CF moved as the LSB in the register.

Syntax: RCL Register, Bits to be shifted


Example:
MOV BX, 3967H ;ASSUME CF=1
MOV CX, 06H
RCL BX, CX

Working:

8) RCR : Rotate Carry Right


This instruction rotates the mentioned bits in the register to the right side such
that rightmost bit that is being rotated it is stored in the Carry Flag (CF), and
the bit in the CF moved as the MSB in the register.

Syntax: RCR Register, Bits to be shifted


Example: MOV AX, FEDCH
MOV CX, 06H
RCR AX, CX

Working:
Branch Instructions in 8086:
Normally the processor executes the program in a sequential manner,
due to continuous increment of IP (instruction pointer).
Branch instructions are those that jump to another part of the program
without executing in a sequential manner.
Eg: for loop in other programming languages.

goto instruction in c language.

Mainly there are two types of branch instructions in 8086 which are further classified into
two types each.

 Jump instruction.
 Call instruction.

Jump instruction:
This lets the processor jump to a specified location to execute from. From the new
location again code will be executed sequentially. The address changes suddenly when
jump operation is performed.

There are two types of jump operation.

 Intrasegment branching
 Intersegment branching
Intrasegment branching:
This is when jump operation is performed within the same segment( refer
segmentation ). This is also known as the near branch. In intrasegment branching as the
jump is in the same segment, so only IP ( offset address ) would change.

Intersegment branching:
This is when jump operation is performed between segments.

This is also known as far jump. As this jump operation is across the segments , both CS
and IP ( segment address and offset address ) will change.

Jump instructions are of two types:


1) Unconditional Jump instruction
2) Conditional jump instruction.
3)

As shown in the picture, whenever the JMP statement comes, it jumps to the label
given ( here label is next ),

In general the programmer doesn’t know the address of each location , so the
label is mentioned in the assembly code.

This was the example of unconditional jump, where it jumps unconditionally.

Conditional jumps:
Instructions that are executed based on some conditions ae
known as conditional jump instructions.
1. Write an assembly language program to find the largest
number in an array of 8-bit numbers.
2. Write an ALP using 8086 instructions to count the
numbers of zeros in a given 8-bit number.
3. Write an assembly language program to count number
of even and odd numbers in the array of sixteen bit
numbers.
4.

5. Write an 8086 ALP to find the sum of numbers in the


array of 10 elements.
6. Write an assembly language program (ALP) which counts
the number of A’s and a’s in a string of characters.
7. Write a program for finding multibyte addition using
8086 microprocessor.
8. Write a program to print a string ‘MICRO’ using 8086
microprocessor.
9. Write an ALP to add two 16bit numbers with carry.

8086 Assembly language to add 2 32-it numbers. Or


multibyte addition

assume cs:code, ds:data


data segment
num1 dd 4A5B6C7Dh
num2 dd 8E9FABCDh
result dd ?
data ends

code segment
start:
mov ax, data
mov ds, ax
mov dl, 00h
mov ax, offset num1
mov bx, offset num2
add ax, bx
mov word ptr result, ax
mov ax, word ptr num1+2
mov bx, word ptr num2+2
adc ax, bx
mov word ptr result, ax
jnc next
inc dl
next: mov byte ptr result+4, dl
mov ah, 4ch
int 21h
code ends
end start

Find a string in an array

DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$"
SEARCH DB 33H
DATA ENDS

PRINT MACRO MSG


MOV AH, 09H
LEA DX, MSG
INT 21H
INT 3
ENDM

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AL, SEARCH
LEA SI, STRING1
MOV CX, 04H

UP:
MOV BL, [SI]
CMP AL, BL
JZ NEXT
INC SI
DEC CX
JNZ UP
PRINT MSG2
JMP END1

NEXT:
PRINT MSG1

END1:
INT 3
CODE ENDS
END START

Largest number in an array


data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends

code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h

mov bl, 00h


LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jl nxt
mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
int 3
code ends
end start

8086 Assembly Program to Find Smallest


Number from Given Numbers
data segment
STRING1 DB 08h,14h,05h,0Fh,09h
Count db 04h
res db ?
data ends

code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, count

mov bl, 79h


LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jge nxt
mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
int 3
code ends
end start

8086 Assembly Program to Count Number of


0’s and 1’s from a Number

DATA SEGMENT
Num DW 98AFH
Zeros DW ?
Ones DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AX, Num
MOV BX, 00H ; Number of 0s is stored in CX
MOV CX, 10H ; 10H in hexadecimal = 16 in hexdecimal
MOV DX, 00H ; Number of 1s is stored in DX

UP:
ROL AX,1
JC ONE
INC BX
JMP NXT

ONE:
INC DX

NXT:
DEC CX
JNZ UP

MOV Zeros, BX
MOV Ones, DX

INT 3
CODE ENDS
END START

8086 Assembly Program to Count Number of


0’s and 1’s from a String

DATA SEGMENT
STR1 DW "0100110011111100"
Zeros DW ?
Ones DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI, STR1
MOV BX, 00H
MOV DX, 00H
MOV CX, 10H
UP:
MOV AX, [SI]
ROR AX, 1
JNC Zeros
INC BX
JMP Down
Zeros:
INC DX
Down:
INC SI
DEC CX
JNZ UP
MOV Zeros, DX
MOV Ones, BX
INT 3
CODE ENDS
END START

You might also like