5-8086 Assembly Language Programming
5-8086 Assembly Language Programming
5-8086 Assembly Language Programming
;Comments
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
;
;program data declaration here
;
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
;
;program code here
;
MOV AH, 4CH
INT 21H
CODE ENDS
END START
Sample programs:
In this section some programming examples are given. No algorithm or flow chart is provided. Try out
to draw flow chart and write algorithm for the given programs. For more examples, you can refer the
MSBTE manual for MAP subject. Also, good examples are given in following books:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to add two 32-bit numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DD 12345678H
NUM2 DD 87654321H
RESULT DD ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
LEA BX, NUM1
MOV AX, [BX]
LEA BX, NUM2
MOV DX, [BX]
ADD AX, DX
LEA BX, RESULT
MOV [BX], AX
LEA BX, NUM1
INC BX
INC BX
MOV AX, [BX]
LEA BX, NUM2
INC BX
INC BX
MOV DX, [BX]
ADC AX, DX
LEA BX, RESULT
INC BX
INC BX
MOV [BX], AX
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to subtract two 32-bit numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DD 87654321H
NUM2 DD 12345678H
RESULT DD ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
LEA BX, NUM1
MOV AX, [BX]
LEA BX, NUM2
MOV DX, [BX]
SUB AX, DX
LEA BX, RESULT
MOV [BX], AX
LEA BX, NUM1
INC BX
INC BX
MOV AX, [BX]
LEA BX, NUM2
INC BX
INC BX
MOV DX, [BX]
SBB AX, DX
LEA BX, RESULT
INC BX
INC BX
MOV [BX], AX
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to add data bytes of one array with data bytes of second
;array and store result in third array
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
ARRAY1 DB 01H, 02H, 03H, 04H, 05H, 06H, 07H
ARRAY2 DB 10H, 20H, 30H, 40H, 50H, 60H, 70H
ARRAY3 DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
LEA SI, ARRAY1 ; set 3 pointers for arrays
LEA DI, ARRAY2
LEA BX, ARRAY3
MOV CL, 07H ; count
UP: MOV AL, [SI]
ADD AL, [DI]
MOV [BX], AL
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find area and perimeter of rectangle
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
HEIGHT DB 15H
WIDTH DB 25H
AREA DW ? ; result will be 16-bit
PERIM DW ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AL, HEIGHT ; get HEIGHT in AL
MOV BL, WIDTH ; get WIDTH in BL
MUL BL ; Area = HEIGHT x WIDTH
MOV AREA, AX ; store AREA
;
; perimeter=2(height+width)
MOV AL, HEIGHT
MOV BL, WIDTH
ADD AL, BL ; AL = AL + BL
MOV BL, 02H
MUL BL ; AL = 02H x AL
MOV PERIM, AX ; store perimeter
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to multiply two 16-bit signed numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DW 2001H
NUM2 DW 0004H
RESULT DD ? ; result will be 32-bit
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV DX, 0000H ; clear DX
MOV AX, NUM1 ; get first no. in AX
MOV BX, NUM2 ; get second no. in BX
IMUL BX
LEA DI, RESULT
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to divide 32-bit/16-bit signed numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DD 12345678H ; 32-bit number
NUM2 DW 5678H ; 16-bit number
RESULT DW ? ; result will be 16-bit
REMAIN DW ? ; remainder will be 16-bit
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
LEA SI, NUM1
MOV AX, [SI] ; get lower 16 bits of 1st number in AX
INC SI
INC SI
MOV DX, [SI] ; get higher 16 bits of 1st number in DX
MOV BX, NUM2 ; get second number in BX
IDIV BX ; divide
MOV RESULT, AX ; store result
MOV REMAIN, DX ; store remainder
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to divide 16-bit/16-bit unsigned numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DW 1234H ; 16-bit number
NUM2 DW 0064H ; 16-bit number
RESULT DW ? ; result will be 16-bit
REMAIN DW ? ; remainder will be 16-bit
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AX, NUM1 ; get 1st number in AX
MOV DX, 0000H ; Clear DX
MOV BX, NUM2 ; get second number in BX
DIV BX ; divide
MOV RESULT, AX ; store result
MOV REMAIN, DX ; store remainder
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to divide 8-bit/8-bit unsigned numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DB 34H ; 8-bit number
NUM2 DB 14H ; 8-bit number
RESULT DB ? ; result will be 8-bit
REMAIN DB ? ; remainder will be 8-bit
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AL, NUM1 ; get 1st number in AL
MOV AH, 00H ; Clear AH
MOV BL, NUM2 ; get second number in BL
DIV BL ; divide
MOV RESULT, AL ; store result
MOV REMAIN, AH ; store remainder
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to add two BCD numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DB 06H
NUM2 DB 04H
RESULT DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AL, NUM1
ADD AL, NUM2
DAA
MOV RESULT, AL
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to subtract two BCD numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DB 06H
NUM2 DB 04H
RESULT DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AL, NUM1
SUB AL, NUM2
DAS
MOV RESULT, AL
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to multiply two BCD numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DB 06H
NUM2 DB 04H
RESULT DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AL, 00
MOV BL, NUM2
UP: ADD AL, NUM1
DAA
DEC BL
JNZ UP
MOV RESULT, AL
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to divide two BCD numbers
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DB 24H
NUM2 DB 04H
RESULT DB ?
REMAIN DB ?
TEMP DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV AH, 00H ; To store the result
MOV AL, NUM1
UP: CMP AL, NUM2 ;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find smallest of an array of 8-bit unsigned numbers
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H, 42H, 05H, 63H, 09H
RESULT DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CX, 04H
LEA BX, ARRAY
MOV AH, [BX]
BACK: INC BX
CMP AH, [BX]
JB GO
MOV AH, [BX]
GO: LOOP BACK
MOV RESULT, AH
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find largest of an array of 8-bit unsigned numbers
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H, 42H, 05H, 63H, 09H
RESULT DB ?
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CX, 04H
LEA BX, ARRAY
MOV AH, [BX]
BACK: INC BX
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find positive and negative numbers of an array of 8-bit
;signed numbers
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H,42H,05H,63H,89H,0ACH,5FH,37H,07AH,99H
POS_C DB 00H ; positive number counter
NEG_C DB 00H ; negative number counter
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CX, 0AH ; counter
LEA BX, ARRAY
BACK: MOV AL, [BX]
SHL AL, 1
JC DOWN ; if CF=1, no.is -ve
INC POS_C ; else if CF=0, no. is +ve
JMP SKIP
DOWN: INC NEG_C
SKIP: INC BX
LOOP BACK ; decrement CX by 1 and goto label BACK
; if CX != 0
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find even and odd numbers of an array of 8-bit unsigned
;numbers
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H,42H,05H,63H,09H,0ACH,5FH,37H,07AH,99H
EVEN_C DB 00H ; even number counter
ODD_C DB 00H ; odd number counter
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CX, 0AH ; counter
LEA BX, ARRAY
BACK: MOV AL, [BX]
SHR AL, 1
JC DOWN ; if CF=1, no.is odd
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to arrange numbers in array in ascending order
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H,42H,05H,63H,09H,0ACH,5FH,37H,07AH,99H
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CL, 0AH ; pass counter
OUTER: LEA BX, ARRAY
MOV CH, 09H ; comparison counter
INNER: MOV AL, [BX]
INC BX
CMP AL, [BX] ; compare current and next number
JC DOWN ; if current no. is smaller no exchange
MOV DL, [BX] ; else exchange numbers
MOV [BX], AL
DEC BX
MOV [BX], DL
INC BX
DOWN: DEC CH ; decrement comparison counter
JNZ INNER
DEC CL ; decrement pass counter
JNZ OUTER
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to arrange numbers in array in descending order
;
ASSUME DS:DATA, CS:CODE
DATA SEGMENT
ARRAY DB 52H,42H,05H,63H,09H,0ACH,5FH,37H,07AH,99H
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV CL, 0AH ; pass counter
OUTER: LEA BX, ARRAY
MOV CH, 09H ; comparison counter
INNER: MOV AL, [BX]
INC BX
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to transfer block of data using string instruction
;
ASSUME DS:DATA, ,ES: EXTRA, CS:CODE
DATA SEGMENT
BLOCK1 DB 52H,42H,05H,63H,09H,0ACH,5FH,37H,07AH,99H
BLOCK2 DB 10 DUP(00H)
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX ; initialize datasegment
MOV ES, AX ; initialize extra segment
LEA SI, BLOCK1
LEA DI, BLOCK2
MOV CX, 000AH ; counter
CLD ; set auto increment mode
REP MOVSB
MOV AH, 4CH
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find sum of numbers in an array
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
ARRAY DB 10H, 20H, 30H, 40H, 50H
RESULT DB 0H
CARRY DB 0H
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV CL, 05H ;No. of elements in array
MOV SI, OFFSET ARRAY ;Pointer to ARRAY
UP: MOV AL, [SI] ;Copy current element in AL
ADD RESULT, AL ;Add current element to RESULT
JNC NEXT
INC CARRY ;Store Carry
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find sum of series 1+2+3+ +9+10
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
RESULT DB 0H
CARRY DB 0H
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV CL, 0AH ;No. of elements in series
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to reverse the string
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STRING DB 'Good Morning'
REV DB 12 DUP(0)
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
LEA SI, STRING ;Pointer to STRING
MOV CX, 0CH ;Length of String
LEA DI, REV ;Pointer to REV
ADD DI, 0BH ;Place pointer at the last
;location in REV
UP: MOV AL, [SI] ;Transfer current character of
;STRING to AL
MOV [DI], AL ;Tansfer character in AL to REV
INC SI ;Update pointers
DEC DI
LOOP UP
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to find length string
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STRING DB 'Good Morning$'
LEN DB ?
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
LEA SI, STRING ;Pointer to STRING
MOV CL, 00H ;Counter for length
MOV AL, '$' ;String termination character
NEXT: CMP AL, [SI] ;Check end of is reached
JZ EXIT ;If 'yes', exit
ADD CL, 01H ;If 'no', increment counter
INC SI ;Update pointer
JMP NEXT
EXIT: MOV LEN, CL
MOV AH, 4CH ;Terminate program
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to concatenate two strings
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STR1 DB 'Good$' ;First String
STR2 DB 'Morning$' ;Second String
STR3 DB 11 DUP(0) ;Concatenated string
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
LEA SI, STR1 ;Pointer to STR1
LEA DI, STR3 ;Pointer to STR3
MOV AL, '$' ;String termination character
NEXT1: CMP AL, [SI] ;Check if end of STR1 is reached
JZ DOWN ;If 'yes', start copying second STR2
MOV AH, [SI] ;If 'no', move current character from
MOV [DI], AH ;STR1 to STR3
INC SI ;Update pointers
INC DI
JMP NEXT1
DOWN: LEA SI, STR2 ;Pointer to STR2
NEXT2: CMP AL, [SI] ;Check if end of STR2 is reached
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to convert decimal number to hexadecimal
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
DECI DB 55H
HEX DB ?
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV AL, DECI ;Move decimal no. to AL
AND AL, 0FH ;Separate the lower nibble
MOV DL, AL ;store lower nibble to DL
MOV AL, DECI ;Move decimal no. to AL
AND AL, 0F0H ;Separate upper nibble
MOV CL, 04H ;Move upper nibble to
ROL AL, CL ;lower 4 bit position
MOV DH, 0AH ;
MUL DH ;Multiply shifted nibble by 10
ADD AL, DL ;Add lower nibble to above result
MOV HEX, AL ;Store result
MOV AH, 4CH ;Terminate program
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to count number of zeros in 8 bit number
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM DB 73H
ZEROS DB 00H
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV AL, NUM ;Move NUM to AL
MOV CX, 08H ;Setup a counter with no. of bits
;in NUM
MOV BL, 00H
UP: ROR AL, 1 ;Rotate AL to right
JC DOWN ;If CF=1, the LSB was 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to count number of ones in 8 bit number
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM DB 55H
ONES DB 00H
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV AL, NUM ;Move NUM to AL
MOV CX, 08H ;Setup a counter with no. of bits
;in NUM
MOV BL, 00H
UP: ROR AL, 1 ;Rotate AL to right
JNC DOWN ;If CF=0, the LSB was 0
INC BL ;If CF=1, the LSB was 1
DOWN: LOOP UP
MOV NUM, BL
MOV AH, 4CH ;Terminate program
INT 21H
CODE ENDS
END START
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;
;Program to convert 8 bit hexadecimal number to decimal number
;
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
HEX DB 0FFH
DECI DW ?
DATA ENDS
CODE SEGMENT
START: MOV DX, DATA
MOV DS, DX ;Initialize data segment
MOV AL, HEX ;Move decimal no. to AL
AND AL, 0FH ;Separate the lower nibble
MOV DL, AL ;store lower nibble to DL