Instruction Set
Instruction Set
Instruction Set
Segment accessed
Data
Extra
Stack
Code
Extra
Extra
Default segment
Stack
Stack
Data
Data
Data
Data
Example
MOV AL, 6+3
MOV AL, 8-2
MOV AL, 4*3
MOV AX, 12/5
MOV AX, 12 MOD 7
MOV AX, 12 AND 4
MOV AX, 12 OR 1
MOV AL, NOT 1
Comment
Copies 9 into AL
Copies 6 into AL
Copies 12 into AL
Copies 2 into AX, reminder is lost
Copies 5 into AX, quotient is lost
Copies 4 into AX (0100)
Copies 13 into AX (1101)
Copies 254 into AL (1111 1110)
INSTRUCTION SET
The instructions of 8086 can be broadly classified under the following headings:
1. Data transfer instructions
2. Data conversion instructions
3. Logical instructions
4. String instructions
5. Arithmetic instructions
6. Branch instructions
7. Processor control instructions
DATA TRANSFER INSTRUCTIONS
In this group large number of instructions is present which perform data transfer of the
following type:
a. Move contents of a register or contents of a memory location, or immediate data to a
register or memory location and move data to or from Accumulator using direct
DKDEPTOFTCE,APSCE
Page1
Note
16-bit register
16-bit pointer
Segment register
Save flags
POP: The POP instruction performs the reverse of PUSH instruction. It removes data
from the stack and places it into the target 16 bit register, segment register or 16 bit
memory location. The POPF instruction (pop flags) removes a 16 bit number from the
stack and places it into the flag register. The POP BX instruction removes the first
byte of data from the stack segment memory location addressed by SP and places it
into BL register. The second byte of data is removed from the stack segment memory
location addressed by SP + 1 and places it into BH register. After both the bytes are
removed from the stack, the SP register is incremented by 2.
POP CS instruction is not valid as if this instruction executes, only a portion of the
address (CS) of the next instruction changes, which makes the POP CS instruction un
predictable and therefore not allowed.
Symbolic
POP reg16
POP mem16
POP seg
POP F
Note
16-bit register
16-bit pointer
Segment register
pop flags
XCHG: The XCHG (exchange) instruction exchanges the contents of a register with
the contents of any register or memory location. This instruction cannot exchange the
DKDEPTOFTCE,APSCE
Page2
Operation
XCHG AL,CL
XCHG CX,BP
XCHG AX,[DI]
XCHG AL,DATA2
Operation
IN AL,P8
IN AX,P8
IN AL,DX
IN AX,DX
OUT P8,AL
OUT P8,AX
OUT DX,AL
OUT DX,AX
DKDEPTOFTCE,APSCE
Page3
INSTRUC
I
CTION SE
ET, PROCEDURES, MACRO
OS
mov opr1, al
m
m ah, 4ch
mov
innt 21h
ennd
TA CONVE
ERSION INS
STRUCTIO
ONS
DAT
This group includ
des the following instrucctions:
v
in AL (XLAT)
(
a. Traanslate the value
b. Looad Effectivee address of a memory loocation into a register (L
LEA)
c. Load DS and another
a
regisster with conntents of 2 memory
m
wordds (LDS)
d. Looad ES and another
a
regisster with conntents of 2 memory
m
wordds (LES)
e. Load AH with LS byte of flag
f registerss (LAHF)
f. Stoore AH into LS
L byte of flag
fl registerss (SAHF)
XL
LAT: The XLAT
X
(transslate) instrucction converrts the conteents of AL register
r
into a
nuumber stored
d in memorry table. Thhis instructioon performs the direct table lookuup
tecchnique used
d to convertt one code too another. First, the conntents of AL
L are added to
t
BX
X to form a memory adddress in the data segmennt. It then coopies the coontents of thiis
adddress to thee AL registeer. The num
mber to be translated iss placed in AL and BX
X
coontains the sttarting offsett of the mem
mory table.
ALP: convert un
npacked BC
CD digits to seven segm
ment code ussing XLAT instruction
i
DKDEPTOFTCE,A
APSCE
Page4
.model small
.data
table db 3Fh, 06h, 5Bh, 4Fh. 66h, 6Dh, 7Dh, 07h, 7Fh, 6Fh
stg_bcd db 09, 03, 05, 01, 10, 07, 02, 04, 06, 08
stg_disp db 10 dup (?)
.code
mov ax, @data
mov ds, ax
mov es, ax
lea bx, table
lea si, stg_bcd
lea di, stg_disp
cld ; DF=0, pointers (SI & DI) automatically increment after execution of string inst
mov cx, length stg_bcd ; cx=10
convert: lodsb ; al=bcd no. from mem loc in DS pointed by SI
cmp al,09
ja invalid
xlat table
stosb; contents of AL (converted 7 segment code) stored in mem loc in ES pointed by DI
DKDEPTOFTCE,APSCE
Page5
ALP: Using XLAT instruction implement the given multiple output logical network.
The input variables are A, B, C, D with D being LSB and outputs are X, Y and Z
with Z being LSB.
X = ABC D + ABC D + ABCD + ABCD
Y = ABC D + ABCD + ABC D
Z = ABCD + ABCD + ABC D + ABCD
A
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
B
0
0
0
0
1
1
1
1
0
0
0
0
1
1
1
1
C
0
0
1
1
0
0
1
1
0
0
1
1
0
0
1
1
D
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
X
0
0
0
0
0
0
0
1
0
1
0
0
1
0
0
1
Y
1
0
0
1
0
0
0
0
0
1
0
0
0
0
0
0
Z
0
0
0
0
0
0
0
1
0
0
0
1
0
1
0
1
.model small
.data
table db 010b, 000b, 000b, 010b, 000b, 000b, 000b, 101b, 000b, 110b, 000b, 001b, 100b,
001b, 000b, 101b
data_in db 09h, 0Ah, 05h, 0Fh, 0Ch, 07, 02, 04, 06, 08
mult_out db 10 dup (?)
.code
mov ax, @data
mov ds, ax
DKDEPTOFTCE,APSCE
Page6
DKDEPTOFTCE,APSCE
Page7
Operation
Loads AX with the address of NUMB
Loads DS and DI with the 32 bit contents of data segment memory
location LIST
Loads ES and BX with the 32 bit contents of data segment memory
location CAT
LAHF and SAHF: These instructions allowed 8085 software to be translated into 8086
software by a translation program. The LAHF instruction transfers the right most 8
bits of the flag register into AH register and the SAHF instruction transfers AH
register into rightmost 8 bits of the flag register. The SAHF instruction is used with
numeric coprocessor to transfer the contents of status register in coprocessor to the
flag register.
ALP: copy contents of flag register into accumulator register using any
arithmetic or logic operations
.model small
.stack
.code
mov ax, @data
mov ds, ax
PUSHF
POP AX
mov ah, 4ch
int 21h
end
LOGICAL INSTRUCTIONS
These can perform AND, OR, EXOR, NOT, TEST, SHIFT, ROTATE etc operations.
Rotate instructions
a. Rotate left contents of memory location or register
b. Rotate right contents of memory location or register
c. Rotate left including carry contents of memory location or register
d. rotate right including carry contents of memory location or register
DKDEPTOFTCE,APSCE
Page8
DKDEPTOFTCE,APSCE
Page9
OR AX, BX
OR [DI], DX
OR CX, 0083H
OR CL, 43H
OR AX, 0083H
OR AL, 83H
XOR AX, BX
XOR DS:[BP], DX
XOR DX, 0083H
XOR CL, 43H
XOR AX, 0083H
XOR AL, 83H
NOT INSTRUCTION
It performs logical inversion of the contents of a register or a memory location. It
replaces the number with its 1s complement.
NOT BX
NOT CL
NOT TEMP
NOT BYTE PTR [BX]
ROTATE INSTRUCTIONS
In this group we have the instructions to
a. Rotate left contents of memory location or register
b. Rotate right contents of memory location or register
c. Rotate left including carry contents of memory location or register
d. rotate right including carry contents of memory location or register
Rotate left contents of a register or a memory location: The mnemonic for this
instruction is ROL. It rotates left the contents of a memory location or a register. The
DKDEPTOFTCE,APSCE
Page10
Register/memory contents
The template for this instruction is 110100VW MOD-000-R/M. There are 7 bits in this
template for identifying the operands. The V bit stands for the count value. This bit will
be 0 for the rotation by 1 bit position. If this bit is 1, it means that CL contains the count
value.
The W bit is 0 to indicate it is a byte operation or it is 1 to indicate a word operation.
Example 1: ROL CX, 1
It rotates left the contents of CX by 1 bit position.
Example 2: ROL BX, CL
It rotates left the contents of BX by a number of bit positions whose value is given in CL.
Example 3: ROL BYTE PTR DS: 0F246H [BP], CL
This instruction rotates left the contents of a memory location whose Effective address is
given as the sum of the 16 bit offset value 0F246H and the contents of BP register. The
physical address is generated using DS as segment register. The number of times the
rotation is performed is given by the contents of CL register. The result is stored in the
same memory location.
Rotate right contents of a register or a memory location: The mnemonic for
this instruction is ROR. It rotates right the contents of a memory location or a register.
The operation can be on a byte or a word data. The rotation can be by one bit position or
more.
DKDEPTOFTCE,APSCE
Page11
The ROR instruction can be used for swapping the hex digits in a byte by using a count
value of 4 in CL, or for swapping the bytes in a word by using a count value of 8 in CL. It
can also be used to test the value of LS bit.
The template for this instruction is 110100VW MOD-001-R/M. There are 7 bits in this
template for identifying the operands. The V bit stands for the count value. This bit will
be 0 for the rotation by 1 bit position. If this bit is 1, it means that CL contains the count
value.
The W bit is 0 to indicate it is a byte operation or it is 1 to indicate a word operation.
Example 1: ROR CX, 1
It rotates right the contents of CX by 1 bit position.
Example 2: ROR BX, CL
It rotates right the contents of BX by a number of bit positions whose value is given in
CL.
Example 3: ROR BYTE PTR DS: 0F246H [BP], CL
This instruction rotates right the contents of a memory location whose Effective address
is given as the sum of the 16 bit offset value 0F246H and the contents of BP register. The
physical address is generated using DS as segment register. The number of times the
rotation is performed is given by the contents of CL register. The result is stored in the
same memory location.
Rotate left including carry on contents of a register or a memory location:
The mnemonic for this instruction is RCL. It rotates left the contents of a memory
location or a register by involving the carry flag in the loop. The operation can be on a
byte or a word data. The rotation can be by one bit position or more.
If the rotation is by 1 bit, it has to be clearly indicated in the instruction. If it is by more
than one bit position, CL must be loaded with the count value to indicate the number of
bit positions by which the rotation has to be made.
After performing rotate left operation by one bit position, the bit moved out from the MS
bit position goes to the carry flag, and in the process moves out the earlier carry bit to the
DKDEPTOFTCE,APSCE
Page12
Register/memory contents
The RCL instruction is useful for testing the value of the MS bit of an operand. It is also
used for moving the carry flag value to the LS bit position of an operand.
The template for this instruction is 110100VW MOD-010-R/M. There are 7 bits in this
template for identifying the operands.
The V bit stands for the count value. This bit will be 0 for the rotation by 1 bit position. If
this bit is 1, it means that CL contains the count value.
The W bit is 0 to indicate it is a byte operation or it is 1 to indicate a word operation.
Example 1: RCL CX, 1
It rotates left with carry, the contents of CX by 1 bit position.
Example 2: RCL BX, CL
It rotates left with carry, the contents of BX by a number of bit positions whose value is
given in CL.
Example 3: RCL BYTE PTR DS: 0F246H [BP], CL
This instruction rotates left with carry, the contents of a memory location whose Effective
address is given as the sum of the 16 bit offset value 0F246H and the contents of BP
register. The physical address is generated using DS as segment register. The number of
times the rotation is performed is given by the contents of CL register. The result is
stored in the same memory location.
Rotate right including carry on contents of a register or a memory location:
The mnemonic for this instruction is RCR. It rotates right the contents of a memory
location or a register by involving the carry flag in the loop. The operation can be on a
byte or a word data. The rotation can be by one bit position or more.
If the rotation is by 1 bit, it has to be clearly indicated in the instruction. If it is by more
than one bit position, CL must be loaded with the count value to indicate the number of
bit positions by which the rotation has to be made.
Register/memory contents
DKDEPTOFTCE,APSCE
Page13
Register/memory contents
DKDEPTOFTCE,APSCE
Page14
Register/memory contents
After performing shift left operation by one bit position, the bit moved out from the MS
bit position goes to the carry flag. In the vacancy created in the LS bit position a 0 is
DKDEPTOFTCE,APSCE
Page15
After performing shift right operation by one bit position, the bit moved out from the LS
bit position goes to the carry flag. In the vacancy created in the MS bit position once
DKDEPTOFTCE,APSCE
Page16
DKDEPTOFTCE,APSCE
Page17
DKDEPTOFTCE,APSCE
Page18
ptr ax= 07 09
al=90, ax=0790
ax=0079
store 79
si to next word
di to next byte
count dec=7
repeat
ax=02 03
al=30,ax=0230
ax=0023
store 23
si=4
di=2
count dec=6
repeat
ax= 05 01
al=10,ax=0510
ax=0051
store 51
si=6
di=3
count dec=5
repeat
this loop repeats till
dx= 0
end
result: 79, 23, 51, 86, 53, 94, 52, 07
DKDEPTOFTCE,APSCE
Page19
Assembly language
LODSB
LODSW
LODS LIST
LODS DATA1
Operation
AL = DS:[SI] i.e. DSX10+SI, SI=SI+1 OR SI-1
AX = DS:[SI] i.e. DSX10+SI, SI=SI+2 OR SI-2
AL = DS:[SI] i.e. DSX10+SI, SI=SI+1 OR SI-1(LIST is 1 byte)
AX = DS:[SI] i.e. DSX10+SI, SI=SI+2 OR SI-2(DATA1 is a
word)
DKDEPTOFTCE,APSCE
Page20
Operation
ES:[DI]=AL i.e. ESX10+DI, DI=DI+1 OR DI-1
ES:[DI]=AX i.e. ESX10+DI, DI=DI+2 OR DI-2
ES:[DI]=AL i.e. ESX10+DI, DI=DI+1 OR DI-1(LIST is 1 byte)
ES:[DI]=AX i.e. ESX10+DI, DI=DI+2 OR DI-2(DATA1 is a
word)
STOS with a REP: The repeat prefix (REP) is added to any string data transfer
instruction, except the LODS instruction. The REP prefix causes the CX register to
decrement by 1 each time the string instruction executes. After CX decrements, the
string instruction repeats. If CX reaches a value 0, the instruction terminates and the
program continues with the next sequential instruction. For e.g. if CX is loaded with
10, and a REP STOSB instruction executes, the microprocessor automatically repeats
the STOSB instruction 10 times. This instruction stores the contents of AL in a block
of memory, as the DI register is automatically incremented or decremented and each
time points to next memory location.
MOVS: This instruction is used to transfer data from one memory location to another.
The MOVS instruction transfers a byte or a word from data segment location
addressed by SI to the extra segment location addressed by DI. The pointers SI and
Assembly language
MOVSB
MOVSW
MOVS N1,N2
MOVS D1,D2
Operation
ES:[DI]=DS:[SI], DI=DI+1 ; SI+1
ES:[DI]= DS:[SI], DI=DI+2 ; SI+2
ES:[DI]= DS:[SI], DI=DI+1 ; SI+1, (N1,N2 are bytes)
ES:[DI]= DS:[SI], DI=DI+2 ; SI+2; (D1,D2 are words)
DKDEPTOFTCE,APSCE
Page21
DKDEPTOFTCE,APSCE
Page22
DKDEPTOFTCE,APSCE
Page23
DKDEPTOFTCE,APSCE
Page24
DKDEPTOFTCE,APSCE
Page25
DKDEPTOFTCE,APSCE
Page26
DKDEPTOFTCE,APSCE
Page27
loop1:
done:
cx=10
al=24h ascii code of $
di pointing to ascii_strg
DF=0
if CX not 0 and ZF=0, repeat;
if CX=0 or ZF=1 come out
if ZF=1, replace $ with
after completion of loop, display
the string in ascii_strg
end
ALP: compare two strings stored in strg1 and strg2 and display if they are same or
not.
.model small
.data
strg1 db be peaceful, $
.model small
.data
strg1 db be peaceful, $
DKDEPTOFTCE,APSCE
Page28
.code
.code
next:
exit:
disp:
next:
exit:
disp:
end
end
ALP: transfer a given source string to destination string using string instructions
.model small
.stack
.data
x db aruna,$
y db 5 dup(?),$
.model small
.stack
.data
x db aruna,$
y db 5 dup(?),$
.code
.code
back:
mov ax,@data
mov ds,ax
lea si,x
lea di,y
mov cx,0005h
mov al,[si]
mov [di], al
loop back
mov ah,09h
lea dx,y
int 21h
mov ax,@data
mov ds,ax
mov es,ax
lea si,x
lea di,y
mov cx,0005h
cld
rep movsb
mov ah,09h
lea dx,y
int 21h
DKDEPTOFTCE,APSCE
Page29
mov ah,4ch
int 21h
end
ALP: reverse a string stored in memory location X and display the result
.model small
.data
count equ 05h
x db aruna,$
res db 5 dup (?),$
.model small
.data
count equ 05h
x db aruna,$
res db 5 dup (?),$
.code
.code
back:
back:
.model small
.data
count equ 05h
x db aruna,$
res db 5 dup (?),$
msg1 db palindrome,$
msg2 db not palindrome,$
.code
mov ax, @data
DKDEPTOFTCE,APSCE
Page30
mov ds, ax
mov es, ax
lea si, x
lea di, res
mov cx, count
add di, cx
dec di
back: cld
lodsb
std
stosb
loop back
cld
lea si, x
lea di, res
mov cx, count
repe cmpsb
jnz fail
lea dx, msg1
jmp disp
fail: lea dx, msg2
disp: mov ah, 09h
int 21h
mov ah, 4ch
int 21h
end
ALP: move a block of data between two overlapping areas. Source location address
is stored in memory location src_addr and destination location address is stored in
memory location dst_addr. The number of data is stored in memory location labeled
by N (using string instructions)
DKDEPTOFTCE,APSCE
Page31
.model small
.data
src_addr dw
dst_addr dw
src db
dst db
.code
mov ax, @data
mov ds, ax
.model small
.data
src_addr dw
dst_addr dw
src db
dst db
.code
mov ax, @data
mov ds, ax
mov es, ax
next:
next1:
cmp si, di
ja next1
add si, cx
dec si
add di, cx
dec di
mov al,[si]
mov [di], al
dec si
dec di
loop next
jmp exit
mov al,[si]
mov [di], al
next:
cld
cmp si, di
ja next
std
add si, cx
dec si
add di, cx
dec di
movsb
loop next
mov ah, 4ch
int 21h
DKDEPTOFTCE,APSCE
Page32
end
end
ALP: scan 80 characters of a string which ends with a blank. If string consists of
only blank characters, a branch is taken to NOT_FOUND location; otherwise
beginning with the 1st non blank character, a substring of LINE of up to 31
characters is moved to SYMBOL until a blank is found or 31 characters have been
moved.
.model small
.data
line db
abcdefghijklmnopqrstuvwxyzabcse
,$
symbol db 31 dup(?), $
msg db not_found, $
.code
mov ax, @data
mov ds, ax
mov es, ax
lea di, line
mov cx, 80
mov al, 20h; ascii code for blank
cld
next:
scas line
loope next
je not_found
mov si, di
dec si
lea di, symbol
mov cx, 31
jmp short same
fill:
stos symbol; mem loc=al=char
same:
lods line ; al=char
cmp al, 20h
loopne fill
jmp exit
not_found: lea dx, msg
mov ah, 9
int 21h
DKDEPTOFTCE,APSCE
Page33
end
ALP: Searches an array table consisting of 20 byte strings until the 1st 8 bytes of an
element are matched by the string beginning at symbol. The offset and segment
address of table and symbol are in tablept and symbolpt.
.model small
.data
tablept dw . ; offset & segment address of table
symbolpt dw
; offset & segment address of symbol
table_size dw . ; no. of entries in table
table db .,$,.,$,.
symbol db .,$
msg1 db match found,$
msg2 db match not found,$
.code
mov ax, @data
mov ds, ax
les di, tablept
mov dx, di
lds si, symbolpt
mov bx, table_size
cld
loop1:
mov cx, 8
repe cmpsb
je found
add dx, 20
mov di, dx
lea si, symbol
dec bx
jne loop1
not_found: lea dx, msg2
jmp disp
found:
disp:
.exit
end
DKDEPTOFTCE,APSCE
Page34
DKDEPTOFTCE,APSCE
Page35
DKDEPTOFTCE,APSCE
Page36
DKDEPTOFTCE,APSCE
Page37
DKDEPTOFTCE,APSCE
Page38
BP DS: F356
0100
1230
0100
EDD0
DKDEPTOFTCE,APSCE
Page39
CX
2360
DCA0
COMPARE INSTRUCTIONS
In this group there are instructions to
a. compare the contents of 2 registers
b. compare contents of a register and a memory location
c. compare immediate data with the contents of a register or a memory location
Compare the contents of 2 registers or a register and a memory location: A compare
instruction compares the two operands, and affects the status flags values depending on
the result of comparison. The compare instruction actually computes the value of
operand_1 minus operand_2. The original values of the operands are not changed. The
result is stored in a register which is not accessible to the programmer. Based on the
result, the flags are affected.
Template for this instruction is 001110DW MOD-REG-R/M. The mnemonic for this
instruction is CMP.
Example 1: CMP AX, BX
This instruction compares the contents of AX and BX registers. After the compare
operation, the status flags are affected depending on the result.
BX
FLAGS
AX
Before: 122F 70F7
After: 122F 70F7 Z=0,S=1,P=0,C=1,A=0,O=0
In the above example, the result of comparison is 122FH 70F7H = A138H. The result is
non zero, negative, number of 1s in the result are odd, contents of AX are less than BX
and a borrow is needed to perform subtraction, no borrow is required during subtraction
of LS hex digits. The overflow flag is reset because the subtraction of 2 positive numbers
never results in an overflow.
Example 2: CMP DS: 0F246H [BP], DX
This instruction compares the contents of a memory location whose effective address is
given as the sum of the 16 bit offset value F246H and the contents of BP register with
DX register contents. The physical address is generated using DS as segment register.
FLAGS
BP DS:F346 DX
DKDEPTOFTCE,APSCE
Page40
0100
0100
122F
122F
70F7
70F7 Z=0,S=1,P=0,C=1,A=0,O=0
Compare immediate data with the contents of a register or a memory location: The
template for this instruction is 100000SW MOD-111-R/M. The S bit stands for sign
extension. When W=0 (byte), the S bit is also 0. If W = 1 (word), the S bit must be a 1
only if the immediate data is <= 7FH. In such a case, the assembler will generate only 1
byte code for the immediate data. However, during execution, the 1 byte data will be sign
extended to make it a 2 byte operand. If the immediate data is >7FH, the S bit must be 0,
and the assembler generates a 2 byte code for the immediate data.
Example 1: CMP CX, 83H
It is a word operation (W = 1) with immediate data > 7FH, S bit is 0. After the compare
operation the status flags are affected depending on the result.
Example 2: CMP CL, 83H
It is a byte operation, and the S bit is 0. After the compare operation the status flags are
affected depending on the result.
Example 3: CMP CX, 43H
It is a word operation (W = 1) with immediate data <= 7FH, S bit is 1. After the compare
operation the status flags are affected depending on the result.
Compare Accumulator contents with immediate data: template for this instruction is
0011110W. There are only 2 instructions in this category. The assembler generates 1 byte
or 2 byte code for the immediate data depending on whether it is a byte or a word
operation. The accumulator contents are compared with the immediate data and the status
flags are affected depending on the result.
Example 1: CMP AX, 83H
It is a word operation. After the compare operation the status flags are affected depending
on the result.
Example 2: CMP AL, 83H
It is a byte operation. After the compare operation the status flags are affected depending
on the result.
DATA SIZE CONVERSION INSTRUCTIONS
In this category there are instructions to convert the byte in AL to sign extended word in
AX, and convert the word in AX to sign extended double word in DX & AX. These
instructions do not affect the flags.
CONVERT BYTE TO WORD
To convert a byte into a word, without changing its value sign extension is done. For a
positive byte valued number (MS bit=0), sign extension is achieved by making the MS
byte of the word as 00H and LS byte of the word as the byte to be converted. For a
negative byte valued number (MS bit=1), sign extension is achieved by making the MS
byte of the word as FFH and LS byte of the word as the byte to be converted. Only AL
DKDEPTOFTCE,APSCE
Page41
AH
34H
00H
AL
02H
02h
Thus after CWB instruction we get 0002H in AX, which is the sign extended value of
02H in AL.
Example 2
AH
AL
Before: 34H 92H
After :
FFH 92H
Thus after CWB instruction we get FF92H IN AX, which is the sign extended value of
92H in AL.
CONVERT WORD TO DOUBLE WORD
To convert a word into a double word, without changing its value sign extension is done.
For a positive word valued number (MS bit=0), sign extension is achieved by making the
MS word of the double word as 0000H and LS word of the double word as the word to be
converted. For a negative word valued number (MS bit=1), sign extension is achieved by
making the MS word of the double word as FFFFH and LS word of the double word as
the word to be converted. Only AX contents can be converted to its equivalent double
word using CBD instruction.
CBD stands for Convert Word to Double Word. DX will have the MS word of the
converted result. Opcode for this instruction is 99H.
Example 1:
Before:
After :
DX
1234G
0000H
AX
5678H
5678H
Thus after CWD instruction we get 00005678H in register pair DX-AX, which is the sign
extended value of 5678H in AX.
Example 2
DX
AX
Before:
1234H
9678H
After :
FFFFH 9678H
Thus after CWD instruction we get FFFF9678H in register pair DX-AX, which is the
sign extended value of 5678H in AX.
DKDEPTOFTCE,APSCE
Page42
CL
60
60
AL
A0
00
AH
45
3C
CX
9180
9180
AX
0030
4800
DX
4567
0001B
Thus the unsigned product of 0030H & 9180H is obtained in DX-AX as 001B4800H
DKDEPTOFTCE,APSCE
Page43
AH
45
DC
Thus the signed product of 60H (+60H) & A0H (=-60H) is obtained in AX as DC00H (=2400H)
Example 3: IMUL CX
Before:
After:
CX
9180
9180
AX
0030
4800
DX
4567
FFEB
Thus the signed product of 0030H (=+30H) & 9180H (=-6E80H) is obtained in DX-AX
as FFEB4800H (=-0014B800H).
DKDEPTOFTCE,APSCE
Page44
DKDEPTOFTCE,APSCE
Page45
BP
0100
0100
DS:F346
0030
0030
AX
9192
0308
DX
0000
0012
Thus after the unsigned division of 00009192H by 0030H, the quotient is obtained in AX
as 0308H and the remainder in DX as 0012H.
Example 2: DIV CL
CL AL AH
Before:
60
A0
45
After:
60
B9
40
Thus after the unsigned division of 45A0H by 60H, the quotient is obtained in AL as
B9H and the remainder in AH as 40H.
Example 3: DIV CX
CX
AX
DX
Before: 0030 9192
0000
After:
0030 0308
0012
Thus after the unsigned division of 00009192H by 0030H, the quotient is obtained in AX
as 0308H and the remainder in DX as 0012H.
PERFORM SIGNED DIVISION: IDIV is the mnemonic of the instruction to perform
signed division. Template for this instruction is 1111011W MOD-111-R/M.
For byte valued divisor, the dividend is implied to be in AX, and the divisor is specified
to be in a byte register or a byte location in the memory. The 8 bit quotient will be stored
in AL and the 8 bit remainder in AH. If the divisor is 0, or if the correct quotient is more
positive than 7FH, or more negative than 80H, the 8086 branches to an interrupt service
subroutine, to take appropriate action to deal with this error. It could be an error message
display on the terminal.
For word valued divisor, the dividend is implied to be in DX-AX, and the divisor is
specified to be in a word register or a word location in the memory. The 16 bit quotient
will be stored in AX and the 16 bit remainder in DX. If the divisor is 0, or if the correct
quotient is more positive than 7FFFH, or more negative than 8000H, the 8086 branches
DKDEPTOFTCE,APSCE
Page46
BP
0100
0100
DS:F346
FFB0
FFB0
AX
0FE0
0300
DX
FFFF
FFE0
Thus after the signed division of FFFF0FE0H (=-0000F020H) by FFB0H (-0050H), the
quotient is obtained in AX as 0300H and the remainder in DX as FFE0H (-0020H).
Example 2: IDIV CL
CL AL AH
Before:
30
EE
E7
After:
30
80
EE
Thus after the signed division of E7EEH (=-1812H) by 30H, the quotient is obtained in
AL as 80H (=-80H) and the remainder in AH as EEH (-12H).
Example 3: IDIV CX
CX
AX
DX
Before: FFB0 0FE0
FFFF
After:
FFB0 0300
FFE0
Thus after the signed division of FFFF0FE0H (=-0000F020H) by FFB0H (-0050H), the
quotient is obtained in AX as 0300H and the remainder in DX as FFE0H (-0020H).
DKDEPTOFTCE,APSCE
Page47
DKDEPTOFTCE,APSCE
Page48
.exit
end
DKDEPTOFTCE,APSCE
Page49
12|34
00010010 0 0110100
46|12
01000110 00010010
-------------------------- ---------------------------------------66|22
11001100 00100010
=-3378
01100110
-----------------------------------------01100110 00100010
As CF=1, result is ve, find 10s complement.
.model small
.data
bcd1 db 34, 18
bcd2 db 89, 27
bcd3 dw ?
.code
.startup
mov al, bcd1
sub al, bcd2
das
mov bcd3, al
mov al, bcd1+1
sbb al, bcd2+1
das
mov bcd3+1, al
AL
34
22
22
22
12
CC
66
66
CF
---0
0
0
0
1
1
1
AF
---0
*
*
*
1
*
*
.exit
End
ALP: unpacked BCD addition & subtraction (ascii codes) of 2 digit 10s
complement numbers. (DX)= up1+up2-up3
.model small
.data
up1 db 03, 05
up2 db 02, 08
up3 db 04, 03
.code
.startup
mov al, up1
add al, up2
aaa
mov dl, al
DKDEPTOFTCE,APSCE
Page50
mcand (20)
mcand+2 (216)
mplier (20)
mplier+2 (216)
--------------------------------------------------232
216
20
264
DKDEPTOFTCE,APSCE
Page51
prod+4
prod+2 prod
dx-------------ax
(mcand)(mplier)
dx------------ax
(mcand+2)(mplier)
dx------------ax
(mcand)(mplier+2)
dx-----------ax
(mcand+2)(mplier+2)
-----------------------------------------------.model small
.data
mcand dw 1234h, 2345h
; (mcand=23451234)
mplier dw 3245h, 2098h
; (mplier=20983245)
prod dw 4 dup (00)
.code
.startup
mov ax, mcand
mul mplier
mov prod, ax
add prod+2, dx
mov ax, mcand+2
mul mplier
add prod+2, ax
adc prod+4, dx
mov ax, mcand
mul mplier+2
add prod+2, ax
adc prod+4, dx
mov ax, mcand+2
mul mplier+2
add prod+4, ax
adc prod+6, dx
.exit
end
.model small
.data
a db 4, 3 ; 34
b db 6, 5 ; 56
c db 4 dup (?) ; 1904
c0 db 3 dup (?)
c1 db 3 dup (?)
.code
.startup
mov al, a ; al=4
mul b
; ax= 4 X 6= 0018h
aam
; ax= 02 04 (unpacked BCD)
DKDEPTOFTCE,APSCE
Page52
DKDEPTOFTCE,APSCE
Page53
r1216+d
-------------------------------r0
b/a
r2232+c216 /a
r1216+d /a
Quotient
q2
q1
q0
remainder
r2
r1
r0
.model small
.data
x dw 1234h
y dw 2099h
z dw 3457h
v dw 1098h
res dw 2 dup (00)
.code
.startup
mov ax,x
imul y; dx-ax
mov res, ax
add res+2, dx
mov ax, z
cwd; dx-ax
add res, ax
adc res+2, dx
sub res, 540
sbb res+2, 0
mov ax, v
cwd; dx-ax
sub ax, res
sbb dx, res+2
idiv x; quo=ax, rem=dx
mov res, ax
mov res+2, dx
.exit
end
DKDEPTOFTCE,APSCE
Page54
The mnemonic for the instruction is JMP. The execution of this instruction always results
in a branch. The branch could be within the same code segment, in which case it is called
a NEAR jump or an INTRASEGMENT jump.
NEAR jump instruction can provide the destination address using a label in a relative
mode, with 1 or 2 bytes of signed displacement, or the destination address is provided
indirectly in a register or a memory location. A NEAR jump instruction using 1 byte
displacement is termed as a SHORT jump.
Thus the 3 types of NEAR jumps are:
(i) NEAR jump with 1 byte displacement (i.e. SHORT jump).
(ii) NEAR jump with 2 byte signed displacement.
DKDEPTOFTCE,APSCE
Page55
DKDEPTOFTCE,APSCE
Page56
If a backward jump instruction is present at the end of the segment, say at memory
location FFEEH, then the next instruction will be at a distance of 2 bytes, at the location
DKDEPTOFTCE,APSCE
Page57
JMP [BX]: jumps to current code segment location addressed by contents of data
segment memory location addressed by BX. (20 bit address of memory location in data
segment= DS X 10 + BX, contents of this location is copied into IP.
4. JMP- Inter-segment Direct Jump or FAR Jump: As it is desired to jump or branch
to a different segment, it is necessary to provide the segment base and the offset values of
the destination in the instruction. Thus it is a 5 byte instruction, where the first byte
provides the Opcode and the next 2 bytes provide the offset and the last 2 bytes provide
the segment base value of the destination. Opcode for this instruction is EAH. In
assembly language program, the programmer just provides the label of the location where
jump is desired and the assembler will be told that it is a FAR jump, using the FAR
pointer as JMP FAR PTR LOC.
Opcode
Offset (low)
Offset (high)
Segment (low) Segment (high)
EA
00
00
00
10
DKDEPTOFTCE,APSCE
Page58
8086 provides a very convenient way to implement loops in a program. These loop
instructions can result in a branch both conditionally and unconditionally. Without such
loop instructions, it requires more code and more execution time to achieve the same
effect using other instructions. These iteration instructions are described below.
1. LOOP - Decrement CX and branch if CX is not equal to zero: The mnemonic for the
instruction is LOOP and it is a 2 byte instruction, where the first byte provides the
Opcode and the second byte provides the displacement in 2's complement notation.
However, in assembly language programming, the programmer provides the label of the
location where jump is desired. The assembler then generates the displacement in 2's
complement notation. Thus the destination must be within a distance of -128 to +127
bytes from the beginning of next instruction after the LOOP instruction. If the label is
defined at a distance outside this range, an error message will be flashed by the assembler
during assembly process.
Although theoretically, it is possible to branch forward using a LOOP instruction, it is
invariably used for a backward branch only. The execution of this instruction does not
affect any of the flags.
The execution of 'LOOP BACK' instruction will result in the following.
a. CX is decremented by 1.
b. If CX content is not 0, a branch to BACK takes place.
c. If CX content is 0, execution of next instruction after LOOP takes place.
In a LOOP instruction CX is always used as an implied counter. Thus LOOP BACK is
equivalent to the following pair of instructions:
DEC CX
JNZ BACK
2. LOOPZ or LOOPE - Decrement CX and branch if CX is not equal to zero and
Z flag = 1: The mnemonic for this instruction is LOOPZ and it stands for 'Loop while Z
flag = 1'. It is a 2 byte instruction, where the first provides the Opcode and the second
byte provides the displacement in 2's complement notation.
DKDEPTOFTCE,APSCE
Page59
DKDEPTOFTCE,APSCE
Page60
.model small
.data
augend db 12, 23, 24, 15, 19, 78, 69, 56
addend db 52, 65, 85, 96, 35, 65, 20, 80
sum db 8 dup (?)
.code
.startup
mov cx, 8
DKDEPTOFTCE,APSCE
Page61
Described below are the instructions which perform a branch based on the value of a
single status flag. The status flags used in these instructions are Z, S, O, C, and P. All
these are 2 byte instructions, where the first byte provides the opcode and the second byte
provides the displacement in 2s complement notation.
The 8 bit displacement indicates the number of bytes by which the jump takes place. The
range for an 8 bit 2s complement number is from -128 (80H) to +127 (7FH). If
it is a positive number, the jump is to a forward location. If it is a negative number, the
jump is to a backward location. Thus the memory location specified by the label in jump
DKDEPTOFTCE,APSCE
Page62
DKDEPTOFTCE,APSCE
Page63
JG, JL, JGE, JLE etc. are used when signed numbers are compared.
JA, JB, JAE, JBE etc. are used when unsigned numbers are compared.
i.e. the terms greater than, less than refer to signed numbers and the terms above or below
refer to unsigned numbers.
1. JBE/ JNA (jump if below or equal/ jump if not above):
Used after CMP instruction to check if operand-1 > or < operand-2. This instruction
results in a branch based on values of CF and ZF.
If CF=1 or ZF=1 (i.e. op1 < op2 or op1 = op2) this instruction execution results in a
jump to the label specified in the instruction.
If CF=0 & ZF=0, execution continues with the next instruction after JBE.
2. JNBE/ JA (jump if not below or equal/ jump if above):
If CF=0 & ZF=0 (i.e. op1 > op2) this instruction execution results in a jump to the
label specified in the instruction.
If CF=1 or ZF=1, execution continues with the next instruction after JBE.
3. JLE/ JNG (jump if less or equal/ jump if not greater):
If SF=1 & OF=0, op1 < op2, op1-op2 = negative result and the result is correct as OF=0.
Therefore, op1 < op2, JLE instruction results in a branch.
DKDEPTOFTCE,APSCE
Page64
DKDEPTOFTCE,APSCE
Page65
INSTRUCTIONS
(OR
MACHINE
CONTROL
These instructions provide control of flags, sample the TEST pin, provide external
synchronization etc.
1. INSTRUCTIONS TO SET/RESET FLAGS
a) Set/Clear/Complement carry flag (STC/CLC/CMC): The carry flag propagates the
carry or borrow in multiple word addition and subtraction. The most common task for the
carry flag is to indicate an error upon return from a procedure. Suppose that a procedure
reads data from a disk memory file. This operation can be successful or an error such as
file not found can occur. On return from procedure, if C = 1, an error has occurred; if C =
0, no error occurred. Most of the DOS and BIOS procedures use the carry flag to indicate
error conditions.
STC (Set carry), CLC (Clear carry), CMC (Complement carry) control the contents of the
carry flag.
DKDEPTOFTCE,APSCE
Page66
DKDEPTOFTCE,APSCE
Page67
DKDEPTOFTCE,APSCE
Page68
It is a 3 byte instruction, where the first byte provides the Opcode and the next two bytes
provide the 16 bit displacement in 2's complement notation. The Opcode for this
instruction is E8H. The 16 bit displacement indicates the number of bytes by which the
jump takes place. The range for a 16 bit 2's complement number is from -32768 (8000H)
to +32767 (7F00H). If it is a positive number, the jump is to a forward location. If it is a
negative number the jump is to a backward location. Thus the memory location specified
by the label in the CALL instruction should be generally in the range of -32768 to
+32767 bytes from the beginning of the next instruction after CALL.
The execution of a NEAR direct CALL results in the following
DKDEPTOFTCE,APSCE
Page69
The call address is given as the contents of a word register or word location memory.
This form of call is used to select one of the several procedures based on a computed
value.
The execution of a NEAR Indirect CALL results in the following
a. The offset value of the next instruction after CALL, which is present in the instruction
pointer register (IP), is saved above the top of the stack.
b. IP value is replaced by the contents of word register, or of a word location in memory,
which is provided in the instruction. Thus the new IP value is the offset address of the
procedure, and hence branch to the procedure takes place.
CALL - Inter-segment Direct Call or FAR direct Call
DKDEPTOFTCE,APSCE
Page70
The CALL address is given as the contents of a double word in memory. The execution
of a FAR Direct CALL results in the following
The Segment value of the next instruction after CALL, which is present in the Code
Segment register (CS), is saved above the top of the stack.
The offset value of the next instruction after CALL, which is present in the instruction
pointer register (IP), is next saved above the top of the stack.
IP value is replaced by the 16 bit offset value provided in the word location whose
address is given in the instruction. Thus the new IP value is the offset address of the
procedure.
CS value is replaced by the 16 bit segment base value provided in the next word location.
Thus the new CS value is the Segment base address of the procedure, and hence branch
to the procedure takes place.
RETURN INSTRUCTION
The CALL instruction pushes the address of the instruction following the CALL on the
stack. The address saved above the top of the stack is generally termed as RETURN
ADDRESS.
The return address is the contents of the IP, in case of a NEAR CALL, whether of
DIRECT or INDIRECT type. The return address is the contents of IP and CS, in case of a
FAR CALL, whether direct or indirect type.
The RET instruction, which is at the end of the procedure, pops out from top of the stack,
the return address, to IP only or IP and CS, so that a branch back to the calling program at
the next instruction after CALL is achieved.
If the calling program is in the same code segment as the called procedure, then a NEAR
return or an intra segment return instruction is used to achieve the objective.
Execution of NEAR return instruction results in popping out the one word return address
to IP. In addition to popping the return address, it can also optionally remove a specified
number of bytes from the stack. This is achieved by adding a 16 bit immediate value
provided in the RET instruction, to the contents of SP register. Thus the 2 types of NEAR
return instructions are
a. NEAR return or intra segment return
b. NEAR return or intra segment return with a 16 bit immediate value added to the SP
contents.
DKDEPTOFTCE,APSCE
Page71
It is a 1 byte instruction and it Opcode is C3H. The offset value of the next instruction
after CALL, which was earlier saved by the NEAR CALL on the stack, is now popped
from top of the stack to the IP register. And so the branch back to the calling program
takes place.
RET - Intra segment Return with Immediate value added to SP register
It is a 3 byte instruction, where the first byte provides the Opcode, and the next 2 bytes
provide the immediate value to be added to SP. An example for this instruction is
RET 12H. Opcode for this instruction is C2H. The execution of this instruction results
in the following.
1. The offset value of the next instruction after call, which was earlier saved by the
NEAR CALL instruction on the stack is now popped from the top of the stack to the IP
Register. And so the branch back to the calling program takes place
2. The 16 bit immediate value provided in the instruction is added to the SP register
contents. In other words, the immediate value provided in the RET Instruction indicates
the number of bytes that are removed from the top of the stack after popping out the
return address.
RET - Inter Segment or FAR RETURN
It is a 1 byte instruction. The Opcode for this instruction is CBH. The execution of a
FAR RET instruction results in the following
The segment base and offset value of the next instruction after call, which was earlier
saved by FAR CALL instruction on the stack, is now popped from the top of the stack to
IP & CS registers and so the branch back to the calling program takes place.
DKDEPTOFTCE,APSCE
Page72
It is a 3 byte instruction, where the first byte provides the Opcode, and the next 2 bytes
provide the immediate value to be added to SP. An example for this instruction is
RET 12H. Opcode for this instruction is CAH. The execution of this instruction results
in the following.
1. The segment base and offset value of the next instruction after call, which was earlier
saved by FAR CALL instruction on the stack, is now popped from the top of the stack to
IP & CS registers and so the branch back to the calling program takes place.
2. The 16 bit immediate value provided in the instruction is added to the SP register
contents. In other words, the immediate value provided in the RET Instruction indicates
the number of bytes that are removed from the top of the stack after popping out the
return address.
MACROS
A Macro is a group of instructions that perform one task, just as a procedure performs
one task. The difference is that a procedure is accessed via a CALL instruction, while a
macro and all the instructions defined in the macro is inserted in the program at the point
of usage.
The name of the macro and any parameters associated with it are typed, and the
assembler then inserts them into the program.
Macro sequences execute faster than procedure because there are no CALL and
RET instructions to execute. The instructions of the macro are placed in the program by
the assembler at the point they are invoked.
The MACRO and ENDM directives delineate or separate a macro sequence. The
st
1 statement of macro is the MACRO instruction, which contains the name of the macro
and any parameters (formal parameters) associated with it
Eg: TRANSFER MACRO A,B
TRANSFER=name of macro
A,B=formal parameters
The last statement of a macro is the ENDM instruction, before which a label should never
be written.
The eg of macro shown below moves the word sized contents of memory location
B into word sized memory location A.
TRANSFER MACRO A,B
PUSH AX
MOV AX,B
MOV A,AX
POP AX
ENDM
DKDEPTOFTCE,APSCE
Page73
Macros contain local variables. A local variable is one that appears in the
MACRO, but is not available outside the MACRO. LOCAL directive is used to define
local variables. LOCAL directive must always immediately follow the MACRO
directive, without any intervening space or comments. In the example below, jump
address (label) is defined as local variable, otherwise, assembler flags it as an error.
READ MACRO A
LOCAL READ1
READ1: MOV AH, 6
MOV DL, OFFH
INT 21H
JE READ1
MOV A, AL
ENDM
READ BX
* ??0000: MOV AH, 6
*MOV DL, OFFH
*INT 21H
*JE ??0000
*MOV BX, AL
This example reads a character from the keyboard and stores it into the byte sized
memory location indicated as a parameter with the MACRO. ??0000 is assembler
generated label.
PARAMETER PASSING TECHNIQUES
The various parameter passing techniques are using registers, using memory locations,
using pointers passed in registers, using stack.
1. PARAMETER PASSING USING REGISTERS
The data, to be passed as parameters is stored in the registers and these registers are
accessed in the procedure to process the data.
DKDEPTOFTCE,APSCE
Page74
DKDEPTOFTCE,APSCE
Page75
DKDEPTOFTCE,APSCE
Page76
MACRO
Accessed using macro name by the
assembler during conversion from
assembly language to machine code
Macros do not use the concept of stack
PUBLIC and EXTRN Directives: The directive PUBLIC is used to declare labels of
code, data, or entire segments are available to other program modules. EXTRN (external)
informs the assembler that the names, procedures and labels declared after this directive
DKDEPTOFTCE,APSCE
Page77
DKDEPTOFTCE,APSCE
Page78
In computer system, the data will not be in correct form. The system converts the data
from one form to another. Binary data are removed from a register or memory and
converted to ASCII for the video display. ASCII data are converted to binary as they are
typed on the keyboard.
BINARY TO ASCII
2 METHODS
.model small
.code
.startup
mov al, 4ah
call disp
.exit
disp proc near
mov ah, 0
aam
add ah, 20h
cmp ah, 20h
je disp1
add ah, 10h
disp1: mov dl, ah
mov ah, 6
push ax
int 21h
pop ax
mov dl, al
add dl, 30h
int 21h
ret
disp endp
DKDEPTOFTCE,APSCE
Page79
1.
2.
3.
4.
.model small
DKDEPTOFTCE,APSCE
Page80
0-9
A-F
a-f
ASCII
CODE
30-39
41H-46H
61H-66H
.model small
.stack
DKDEPTOFTCE,APSCE
Page81
call readh
mov temp, ax
mov ah,4ch
int 21h
readh proc near
mov cx, 4
mov si. cx
mov bx, 0
readh1: mov ah, 1
int 21h
call conv
shl bx, cl
add bl, al
dec si
jnz readh1
mov ax, bx
ret
readh endp
.model small
DKDEPTOFTCE,APSCE
Page82
end
The date is read from the system by using INT 21H function call number 2AH. This
leaves the day of the week in AL, the year in CX, the day of the month in DL, and
the month in DH.
The date is set by using INT 21h and function no. 2bh in AH register with the day of
the week in AL, the year in CX, the day of the month in DL, and the month in DH.
The time is read from the system DOS, using INT 21H function call number 2CH.
This returns with hours in CH, minutes in CL, Seconds in DH, hundredths of
second in DL.
The date is set by using INT 21h and function no. 2dh in AH register with hours in
CH, minutes in CL, Seconds in DH, hundredths of second in DL.
DKDEPTOFTCE,APSCE
Page83
.model small
.data
.code
disp macro char
push ax
push dx
mov dl, char
mov ah,2
int 21h
pop dx
pop ax
endm
mov ax, @data
mov ds, ax
call times
mov ah, 4ch
int 21h
; The time is available from DOS, using INT 21H function call number 2CH.
; This returns with hours in CH and minutes in CL. Also available are seconds ; in
DH and hundredths of second in DL.
mov bh, a
cmp ch, 12
jb times1
; set a for am
; if below 12:00 noon
mov bh, p
sub ch, 12
; set p for pm
; adjust to 12 hrs
times1: or ch, ch
jne times2
mov ch, 12
DKDEPTOFTCE,APSCE
Page84
; convert minutes
; display tens
pop ax
disp al
disp
disp bh
disp .
disp m
disp .
; display units
; display space
; display a or p
; display .
; display m
; display .
ret
times endp
end
PROGRAM TO READ SYSTEM DATE
.model small
.data
.code
disp macro char
push ax
push dx
mov dl, char
mov ah,2
int 21h
pop dx
DKDEPTOFTCE,APSCE
Page85
;
; DL,
; get month
; display tens
; display units
disp .
dates3: disp 2
DKDEPTOFTCE,APSCE
Page86
; scale to 00-99
; convert to bcd
; convert to ascii
; display tens
; display units
The keyboard of a personal computer is read via a DOS function call, which uses
INT 21H. Data read from the key board are either in ASCII coded form or in extended
ASCII coded form. Each function key has 4 sets of codes selected by the function key
alone, shift-function key combination, the alternate-function key combination and the
control-function combination.
There are three ways to read the keyboard.
The first method reads a key and echoes (or displays) the key on the video screen. The
second method tests to see if the key is pressed & if it is, it reads the key else returns
without any key. The third method allows an entire character string or line to be read
from the keyboard.
DKDEPTOFTCE,APSCE
Page87
DOS function number 06H reads a key without an echo to the screen. It also allows
extended ASCII characters and does not respond to control-c key combination. The value
of key typed will be stored in AL register. This function uses AH for function number
06H and DL =0FFH to indicate that the function call (INT 21H) will read the keyboard
without an echo. It returns from the INT 21H, even if no key is typed.
If a character is read, the zero flag (ZF) indicates whether a character was typed. A zero
condition (ZF=1) indicates that no key was typed, and a non zero condition (ZF=0)
indicates that AL contains the ASCII code of the key typed or a 00H. If AL=00H, the
function must again be invoked to read an external ASCII character from the keyboard.
keys proc far
mov ah, 06h
mov dl, 0ffh
int 21h
je keys
or al, al
jnz keys1
int 21h
stc
keys1: ret
keys endp
; if no key is typed
; test for 00h, clear carry
; get extended ascii character
; indicate extended ascii character
Function call number 0AH reads an entire line of information or data up to 255
characters, from the keyboard. It continues to acquire the keyboard data or read the
keyboard (displaying data as typed) until either the specified number of characters are
typed or until the enter key is typed .This function requires that AH=OAH & DS:DX
address the keyboard buffer (a memory area where ACII data are stored) i.e DX contains
the offset address of the buffer. The first byte of the buffer area must contain maximum
number of keyboard characters read by this function (maximum number can be 255). The
DKDEPTOFTCE,APSCE
Page88
Video data are displayed in a number of different ways with DOS function calls. DOS
function numbers 02H or 06H can be used to display one character at a time of function
or 09H can be used to display an entire string of characters.
1. Displaying one ASCII character
If AH=02H and DL=ASCII character to be displayed, then the function called INT 21H
normally displays the data in DL on the video screen.
Similarly if AH=06H and DL = ASCII character to be displayed, then this function
displays the ASCII character on the video screen.
.model small
.code
disp macro a
mov ah, 06h
mov dl, a
int 21h
endm
.startup
disp 0dh
*mov ah, 06h
*mov dl, 0dh
DKDEPTOFTCE,APSCE
Page89
.exit
end
2. Displaying a character string
A character string is a series of ASCII coded character that end with a $ when used with
DOS function calls number 09H. This function requires that DS:DX address the character
string to be displayed, before executing the INT 21H instruction. i.e. DX contains the
offset address of the string to be displayed.
.model small
.data
mes db 13, 10,this is a test line ,$
.code
.startup
mov ah, 09h
mov dx, offset mes
int 21h
.exit
end
Program that displays ab followed by carriage return and line feed combination
using macro
.model small
.code
disp macro var
mov dl, var
mov ah, 6
int 21h
endm
.startup
disp a
; display a
; move b to al
DKDEPTOFTCE,APSCE
Page90
; display contents of al
.model small
.data
D db 37h
B db ?
.code
.startup
mov bl, D
and bl, 0f0h
mov cl, 4
shr bl, cl
mov al, 0ah
mul bl
mov dl, D
and dl, 0fh
add al, dl
.model small
.data
D db 37h
B db 0h
.code
.startup
L1: cmp D, 09h
jle L2
sub D, 10h
add B, 0ah
jmp L1
L2: mov al, D
add B, al
.exit
end
mov B, al
.exit
end
DKDEPTOFTCE,APSCE
Page91
.model small
.data
D db ?, ?
B db 0ffh
.code
.startup
mov al, B
mov ah, 0h
mov bl, 100 ; =64h
div bl ; Q=al=02h, R=ah=37h
mov D, al
mov bl, 10 ; =0ah
mov al, ah
mov ah, 0h
div bl ; Q=al=05h, R=ah= 05h
mov cl, 4
shl al, cl
add al, ah
mov D+1, al
.exit
end
.model small
.data
D db 0h
B db 0ffh
.code
.startup
L1: cmp B, 09h
jle L2
sub B, 0ah
add D, 10h
daa
jmp L1
L2: mov al, B
add D, al
.exit
end
DKDEPTOFTCE,APSCE
Page92
.model small
.data
bcd db 59h
ascii db ?, ?
.code
.startup
mov ah, bcd
and ah, 0f0h
mov cl, 4
shr ah, cl
mov al, bcd
and al, 0fh
add ax, 3030h
mov ascii, ax
.exit
end
ALP: convert 2 ASCII characters to packed BCD
.model small
ascii db 5, 9
bcd db ?
.code
.startup
mov ah, ascii
mov al, ascii+1
sub ax, 3030h
mov cl, 4
shl al, cl
shr ax, cl
mov bcd, al
.exit
end
DKDEPTOFTCE,APSCE
Page93
.model small
.data
n1 dw 1122h
n2 dw 2233h
N equ 2
avg dw ?, ?
.code
.startup
mov dx, 0
mov bx, N
mov ax, n1
add ax, n2
jnc next
inc dx
next: div bx
mov avg, ax
mov avg+2, dx
.exit
end
ALP: sort a given set of 16 bit unsigned integers into ascending order using
insertion sort algorithm
.model small
.stack
.data
a dw 0004h, 0003h, 0001h, 0002h
L dw ($-a)/2
.code
mov ax,@data
; initialize the ax register
mov ds,ax
;initialize ax value to ds register
mov cx,02h
;initialize 2 to cx register
ol:mov dx, cx
dec dx
mov si, dx
add si, si
mov ax, a[si]
inl:cmp a[si-2], ax
jbe next
mov di, a[si-2]
DKDEPTOFTCE,APSCE
Page94
ALP: sort a given set of 8bit unsigned integers into ascending order using the
bubble sort algorithm.
.model small
.stack
.data
a db 0dh,0ch,0ah,0bh,0eh
L dw $-a
;calculate the size of numbers
.code
mov ax,@data
;initialize the data segment
mov ds,ax
mov bx,L
;the number of data byte is initialized in bx register
dec bx
;load bx with (n-1)
nxtpass:mov cx,bx
;save the count in cx register
mov si,0
;initialize the pointer
nxtcomp:mov al,a[si]
;load the data in to al pointed by si
inc si
;increment the point
cmp al,a[si]
;is the content of al less than that of si pointed data?
jb next
;yes, go to next
xchg al,a[si]
;no, exchange two data in memory
mov a[si-1],al ;repeat till the end of the memory
next:loop nxtcomp
;has all the comparison over in this pass?
;no, go to nxtcomp to continue
dec bx
;has all the passes completed?
jnz nxtpass
;no, go to nxtpass
mov ah,4ch
;terminate the program
int 21h
end
ALP: sort a given set of 8bit unsigned integers into ascending order using the
selection sort algorithm.
.model small
DKDEPTOFTCE,APSCE
Page95
.model small
.data
cr equ 13
Lf equ 10
a db 55h, 33h, 44h, 66h, 22h
L dw $-a
srckey db 35h
asc1 equ (srckey/10h) + 0
asc2 equ (srckey mod 10h) + 0
msg1 db element ,asc1,asc2,found at position:
res db ?, cr, Lf, $
msg2 db element ,asc1,asc2, not found, cr, Lf, $
.code
DKDEPTOFTCE,APSCE
Page96
.model small
.stack
.data
msg1 db 'search fails $'
msg2 db 'search successful position is:'
result db ?,$
array dw 00ffh,01feh,23fdh,45fch,0abfbh,0bbfah,0cdf0h,0eeeeh
L dw ($-array)/2
key dw 0eeeeh
.code
mov ax,@data
;initialize the data segment
mov ds,ax
mov bx, 1
mov dx, L
mov cx, key
again:cmp bx, dx
ja fail
mov ax, bx
add ax, dx
DKDEPTOFTCE,APSCE
Page97
.model small
.data
count equ 05h
largest db 01h dup(?)
list db 52h, 23h, 26h, 45h, 15h
.code
mov ax, @data
mov ds, ax
lea si, list
mov cl, count
mov al, [si]
dec cl
again:cmp al, [si+1]
jnc next
DKDEPTOFTCE,APSCE
Page98
ALP: multiply two 3 X 3 matrices of signed 8 bit integers. Assume each of the
elements of the product matrix can be stored in 1 byte locations.
Algorithm:
for i=1 to 3
for j= 1 to 3
c[i,j]= 0
for k= 1 to 3
c[i, j]= c[i, j] + a[i, k] * b[k, j]
end
end
end
.model small
.data
ar1 db 1, 2, -3
ar2 db 4, 5, 6
ar3 db 2, -1, 3
br1 db 2, 4, -4
br2 db 3, -2, 5
br3 db 1, 5, 2
c db 9 dup (?)
L2 db ?
L1 db ?
.code
.startup
mov bp, 0
mov L2, 3
lea si, ar1
loop1: lea di, br1
DKDEPTOFTCE,APSCE
Page99
Algorithm:
if r= 0 or r= n then nCr=1
if r= 1 or r= n-1 then nCr=n
in other cases, nCr=(n-1)Cr +(n-1)C(r-1)
.model small
.data
n db 4
r db 2
ncr db ?
.code
.startup
DKDEPTOFTCE,APSCE
Page100
Algorithm:
if n= 0, then f[n]=0
if n= 1, then f[n]=1
In other cases, f[n]=f[n-1] + f[n-2]
DKDEPTOFTCE,APSCE
Page101
Algorithm:
if n= 0, then f[n]=0
if n= 1, then f[n]=1
In other cases, f[n]=f[n-1] + f[n-2]
DKDEPTOFTCE,APSCE
Page102
DKDEPTOFTCE,APSCE
Page103
DKDEPTOFTCE,APSCE
Page104