Professional Documents
Culture Documents
So, for that purpose, we have various Shift and Rotate instructions present in
the 8086 microprocessor.
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).
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).
Working:
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:
Working:
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:
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).
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.
Working:
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.
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.
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.
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.
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.
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
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$"
SEARCH DB 33H
DATA ENDS
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
code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h
mov res,bl
int 3
code ends
end start
code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, count
mov res,bl
int 3
code ends
end start
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
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