Friday, April 17, 2009

8086 programs

; Program to Add, Subtract, Divide and multiply to unsigned numbers

Data1 Segment
Addition DB 02H,82H
Subtraction DB 02H,02H
Multiplication DB 03H,03H
Division DB 09H,03H
ResultA DB ?
ResultS DB ?
ResultM DB 2 DUP (?)
ResultD DB 2 DUP (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start : Mov AX, Seg data1
Mov DS, AX

;Addition
Mov SI, Offset Addition
Mov AL, [SI]
Mov AH, [SI+01]
Add AL, AH
Mov [ResultA], AL

;Subtraction
Mov SI, Offset Subtraction
Mov AL, [SI]
Mov AH, [SI+01]
Sub Al, AH
Mov [ResultS],AL

;Multiplication
Mov SI, Offset Multiplication
Mov AL, [SI]
Mov AH, [SI+01]
IMul AL
Mov [ResultM],Al
Mov [ResultM+1], AH

;Division
Mov SI, Offset Division
Mov AL, [SI]
Mov AH, 00H
Mov BL, [SI+01]
IDiv BL
Mov [ResultD], AL
Mov [ResultD+1], AH

Mov AH, 4CH
INT 21H
Code1 Ends
End Start



;Program to convert BCD to BINARY

Data1 Segment
HEX DB (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AL, 15H
Mov BL, AL
AND BL, 0FH
Mov CL, 04
SHR AL, CL
Mov BH, 0AH
MUL BH
ADD AL, BL
Mov SI, Offset HEX
Mov [SI], AL

Mov AH, 4CH
INT 21H
Code1 Ends
End Start



; Program to convert Binary to BCD

Data1 Segment
DEC1 DB 3 DUP (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov SI, Offset DEC1
Mov AL, 0FFH
Mov AH, 00H
Mov BL, 64H
DIV BL
Mov [SI], AL
Mov AL, AH
Mov AH, 00
Mov BL, 0AH
DIV BL
INC SI
Mov [SI], AL
INC SI
Mov [SI], AH

Mov AH, 4CH
INT 21H
Code1 Ends
End Start

OUTPUT:

0C5E:0000 02. 05. 05.


;Program to find that the given string is Palindrome or not..
Data1 segment
CR EQU 36
Len DW (?)
B1 DB 'MADAM','$'
B2 DB 'Given String is not Palindrome','$'
B3 DB 'Given String is Palindrome','$'
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg data1
Mov DS, AX

; To find length of string(caps)
Mov SI, Offset B1
Mov BX, 00

STRLEN: Mov AL, [SI]
Cmp AL, CR
JE Skip
INC SI
INC BX
JMP STRLEN

Skip: Mov Len, BX
Mov CX, BX
SHR CX, 0001
Mov SI, Offset B1
Add SI, Len
Mov DI, SI
Dec DI
Mov SI, Offset B1

Again: Mov AL, [SI]
Mov BL, [DI]
Cmp AL, BL
JNZ GO
INC SI
Dec DI
Loop Again
JMP LAST

GO: Mov DX, Offset B2
Mov AH, 09
INT 21H

ROT: Mov AH, 4CH
INT 21H

LAST: Mov DX, Offset B3
Mov AH, 09
INT 21H
JMP ROT

Code1 Ends
END Start


OUTPUT:
Given String is Palindrome
; Program to arrange the data in decending order.

Data1 segment
B1 DB 01, 02, 03, 04, 05, 06, 07, 08
Len DB 08
Data1 Ends

Code1 segment
Assume CS: Code1, DS: Data1

Start : Mov AX, Seg data1
Mov DS, AX
Mov SI, Offset len
Mov BL, [SI]
Mov CL, BL
Mov SI, Offset B1

Back : DEC CL

Again: Mov AL, [SI]
INC SI
CMP AL, [SI]
JNC Noexc
Mov DL, [SI]
Mov [SI], AL
Dec SI
Mov [SI], DL
INC SI

Noexc: Dec CL
JNZ Again
Mov SI, Offset B1
Mov CL, BL
Dec BL
JNZ Back

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:


0C5F:0000 08. 07. 06. 05. 04. 03. 02. 01.
0C5F:0008 08.
; Program to arrange the data in ascending order.

Data1 segment
B1 DB 08, 07, 06, 05, 04, 03, 02, 01
Len DB 08
Data1 Ends

Code1 segment
Assume CS: Code1, DS: Data1

Start : Mov AX, Seg data1
Mov DS, AX
Mov SI, Offset len
Mov BL, [SI]
Mov CL, BL
Mov SI, Offset B1

Back : DEC CL

Again: Mov AL, [SI]
INC SI
CMP AL, [SI]
JC Noexc
Mov DL, [SI]
Mov [SI], AL
Dec SI
Mov [SI], DL
INC SI

Noexc: Dec CL
JNZ Again
Mov SI, Offset B1
Mov CL, BL
Dec BL
JNZ Back

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:

0c5f:0000
0C5F:0000 01. 02. 03. 04. 05. 06. 07. 08.
0C5F:0008 08.
; Program to find GCD of Two numbers

Data1 Segment
B1 DB 05, 17H
B2 DB (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start : Mov Ax, Seg data1
Mov DS, AX
Mov SI, Offset B1
Mov AL, [SI+01]
Mov BL, [SI]

Back : Mov AH, 00
DIV BL
CMP AH, 00
JZ next
Mov AL, BL
Mov BL, AH
Jmp back

Next : Mov B2, BL
Mov AH, 4CH

INT 21H
Code1 Ends
End Start


OUTPUT:

0C5F:0000
0C5F:0000 05. 17. 01.
; Program to compute LCM

Data1 segment
B1 DB 5, 11h
B2 DB (?)
Data1 Ends

Code1 SEgment
Assume CS:code1, DS: Data1

Start : Mov AX, Seg data1
Mov DS,AX
Mov SI, Offset B1
Mov AL, [SI+01]
Mov BL,[SI]

Back : Mov AH, 00
Div BL
CMP AH,00
JZ Go
Mov AL, BL
Mov BL, AH
JMP Back

Go : Mov AL, [SI]
Mul Byte ptr[SI+01]
Div BL
Mov B2, AL

Mov AH, 4CH
INT 21H
Code1 Ends
End start


OUTPUT:

0C5F:0000
0C5F:0000 05. 11. 55. 00.
;Program to generate 1st N numbers of Fibonacci series.

Data1 Segment
RB DB 8 DUP (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov SI, Offset RB
Mov BYTE PTR [SI], 01
INC SI
Mov BYTE PTR [SI], 01
Mov AL, 01
Mov BL, 01
Mov CX, 0006

Back: ADD AL, BL
INC SI
Mov [SI], AL
Mov DL, AL
Mov AL, BL
Mov BL, DL
Loop Back

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:
0C5C:0000 01. 01. 02. 03. 05. 08. 0D. 15.
;program to determine printer status.

Data1 Segment
LF EQU 0AH
CR EQU 0DH
PMSG DB 'Program is printed',CR,LF
PMSGLEN DW $-PMSG
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AH, 02
Mov DX, 0001
AND AH, 20H
JZ Ahead
JMP Exit

Ahead: Mov CX, PMSGLEN
Mov SI, Offset PMSG
Mov AH, 05H

Again: Mov DL, [SI]
INT 21H
INC SI
Loop Again

Exit: Mov AH, 4CH
INT 21H
Code1 Ends
End Start
;Program to rename file.

Data1 Segment
OLD DB 'Naveen1.ASM',0
NEW DB 'Roopa1.ASM',0
SUCMSG DB 'Naveen1.ASM RENAMED, AS Roopa1.ASM','$'
FAILMSG DB 'ERROR1 TATA1.ASM IS NOT RENAMED','$'
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov ES, AX
LEA DX, OLD
LEA DI, NEW
Mov AH, 56H
INT 21H
JC ERROR
LEA DX, SUCMSG
JMP DISPLAY1

ERROR: LEA DX, FAILMSG

Display1: Mov AH, 09H
INT 21H

Exit: Mov AH, 4CH
INT 21H

Code1 Ends
End Start

OUTPUT:
-g
Naveen1.ASM RENAMED, AS Roopa1.ASM
;Program to detrmine user memory size.

Data1 Segment
CR EQU 13
LF EQU 10
MSG DB 'Memory Size in KB='
ASCRES DB 4 DUP (?),'HEX',CR,LF,'$'
RES DW ?
HEXCODE DB '0123456789ABCDEF'
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

HEX_ASC PROC
Mov DL, 10H
Mov AH, 0
Mov BX, 0
DIV DL
Mov BL, AL
Mov DH, HEXCODE [BX]
Mov BL, AH
Mov DL, HEXCODE [BX]
RET

HEX_ASC ENDP
Start: Mov AX, Seg Data1
Mov DS, AX
INT 12H
Mov RES, AX
Mov AL, BYTE PTR RES
CALL HEX_ASC
Mov ASCRES+2, DH
Mov ASCRES+3, DL
Mov AL, BYTE PTR RES+1
CALL HEX_ASC
Mov ASCRES, DL
Mov DX, Offset MSG
Mov AH, 09
INT 21H

Mov AH, 4Ch
INT 21H
Code1 Ends
End Start

OUTPUT:
Memory Size in KB=2 80HEX

; Program to add two multi byte numbers

Data1 Segment
D1 DB 02h,05H,06H,03H
D2 DB 85H,67H,83H,47H
Result DB 5 dup(?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov SI, Offset D1
Mov DI, Offset D2
Mov CX, 0004
Mov BX, Offset result
CLC ;Clear carry

Back: Mov AL, [SI]
ADC AL, [DI]
Mov [BX], AL
Inc SI
Inc DI
Inc BX
Loop Back
Jnc GO
Mov Byte ptr[BX], 01

Again: Mov AH, 4ch
INT 21H

Go: Mov Byte ptr[BX], 00
Jmp again
Code1 Ends
End start


OUTPUT:

0C5F:0000 02. 05. 06. 03. 85. 67. 83. 47.
0C5F:0008 87. 6C. 89. 4A.
;Program to display ASCII code and character


Data1 Segment
X DB ?
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AH, 01
INT 21H
Mov SI, Offset X
Mov [SI], AL
CALL DISP1
Mov AL, [SI]
AND AL, 0F0H
Mov CL, 04H
ROR AL, CL
ADD AL, 30H
Mov DL, AL
Mov AH, 02H
INT 21H
Mov AL, [SI]
AND AL, 0FH
CMP AL, 0AH
JC SUB1
ADD AL, 07H

Sub1: ADD AL, 30H
Mov AH, 02H
Mov DL, AL
INT 21H
Mov AH, 4CH
INT 21H

DISP1 PROC NEAR
Mov AH, 02H
Mov DL, 20H
INT 21H
Mov AH, 02H
Mov DL, 3DH
INT 21H
Mov AH, 02H
Mov DL, 20H
INT 21H
RET
DISP1 Endp

Code1 Ends
End Start

OUTPUT:

-g
2 = 32
-g
a = 61
-g
e = 65
-g
1 = 31


;Program to display a string on console using BIOS call

Data1 Segment
MSG DB 'HELLO WORLD USING BIOS','$'
LEN EQU 22
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov CX, LEN
Mov SI, Offset MSG

Next: Mov AH, 0EH
Mov AL, [SI]
INT 10H
INC SI
Loop Next

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:
HELLO WORLD USING BIOS








;Program to compute factorial of a number recursively.

Data1 Segment
RB DW (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AX, 0005H
CALL FACT

Mov AH, 4CH
INT 21H

FACT PROC
CMP AX, 01H
JE Exit
PUSH AX
DEC AX
CALL FACT
POP AX
MUL RB
Mov RB, AX
RET

Exit: Mov RB, 01
RET
FACT ENDS

Code1 Ends
End Start


OUTPUT:
0C5C:0000 78. 00.
;Program to read character from keyboard and echo it.

Data1 Segment
MSG DB 'Enter Character from the keyboard(# to End)','$'
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AH, 09
Mov DX, Offset MSG
INT 21H

Nextchar: Mov AH, 08H
INT 21H
Mov AH, 02H
Mov DL, AL
INT 21H
CMP AL,'#'
JNE Nextchar
Mov AH, 4CH
Mov AL, 00H

INT 21H
Code1 Ends
End Start

OUTPUT:
Enter Character from the keyboard(# to End)Naveen#


;Program to multiply two 16 bit binary number.

Data1 Segment
Result DD (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AX, 00FFH
Mov BX, 00FEH
MUL BX
Mov SI, Offset Result
Mov WORD PTR [SI], AX
Mov WORD PTR [SI+02], DX

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:
0C54:0000 02. FD. 00.
;Program to count number of 1's and 0's in a given 16 bit data.

Data1 Segment
R1 DB (?)
R0 DB (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov AX, 0FFFFH
Mov CX, 0010H
Mov BL, 00

Rep1: ROR AX, 01
JNC Go
INC BL

GO: Loop Rep1
Mov R1, BL
Mov DH, BL
SUB DH, BL
Mov R0, DH

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:
0C5C:0000 10. 00.
;Program to addition of 2 square matrices.

Data1 Segment
MA1 DB 01,02,03
MA2 DB 04,05,06
MA3 DB 07,08,09
MB1 DB 01,02,03
MB2 DB 04,05,06
MB3 DB 07,08,09
MR1 DB ?,?,?
MR2 DB ?,?,?
MR3 DB ?,?,?
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov SI, Offset MA1
Mov DI, Offset MB1
Mov BX, Offset MR1
Mov CX, 0009

Back: Mov AL, [SI]
ADD AL, [DI]
Mov [BX], AL
INC AL
INC BX
INC SI
INC DI
Loop Back

Mov AH, 4CH
INT 21H

Code1 Ends
End Start



OUTPUT:

0C54:0000 01. 02. 03. 04. 05. 06. 07. 08.
0C54:0008 09. 01. 02. 03. 04. 05. 06. 07.
0C54:0010 08. 09. 02. 04. 06. 08. 0A. 0C.
0C54:0018 0E. 10. 12. 00.
;Program to addition of 2 square matrices.

Data1 Segment
MA1 DB 01,02,03
MA2 DB 04,05,06
MA3 DB 07,08,09
MB1 DB 01,02,03
MB2 DB 04,05,06
MB3 DB 07,08,09
MR1 DB ?,?,?
MR2 DB ?,?,?
MR3 DB ?,?,?
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov SI, Offset MA1
Mov DI, Offset MB1
Mov BX, Offset MR1
Mov CX, 0009

Back: Mov AL, [SI]
ADD AL, [DI]
Mov [BX], AL
INC AL
INC BX
INC SI
INC DI
Loop Back

Mov AH, 4CH
INT 21H

Code1 Ends
End Start



OUTPUT:

0C54:0000 01. 02. 03. 04. 05. 06. 07. 08.
0C54:0008 09. 01. 02. 03. 04. 05. 06. 07.
0C54:0010 08. 09. 02. 04. 06. 08. 0A. 0C.
0C54:0018 0E. 10. 12. 00.
;Program to search an number in an array

Data1 segment
B1 DB 01,02,03,04,05,06,07,08,09,10,11
B2 DB 'NUMBER IS PRESENT','$'
B3 DB 'NUMBER IS NOT PRESENT','$'
Data1 Ends

Code1 Segment
Assume CS:Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov CX, 00AH
Mov SI, Offset B1
Mov AL, 05

Again: CMP AL, [SI]
JZ NO_OP
INC SI
Loop Again
Mov AH, 09
Mov DX, Offset B3
INT 21H

Last: Mov AH, 4CH
INT 21H

NO_OP: Mov AH, 09
Mov DX, Offset B2
INT 21H
JMP Last

Code1 Ends
End Start



OUTPUT:

NUMBER IS PRESENT
; Program to generate Multiplication Table.

Data1 Segment
B1 DB 10 DUP (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg Data1
Mov DS, AX
Mov CX, 000AH
Mov SI, Offset B1
Mov DL, 02
Mov AX, 0000
Mov BL, 01

Next: Mov AL, DL
MUL BL
Mov [SI], AL
INC BL
INC SI
Loop Next

Mov AH, 4CH
INT 21H
Code1 Ends
End Start


OUTPUT:

0C5E:0000 02. 04. 06. 08. 0A. 0C. 0E. 10.
0C5E:0008 12. 14. 00. ;Program to convert Binary to BCD.

Data1 segment
DEC1 DB 3 DUP (?)
Data1 Ends

Code1 Segment
Assume CS: Code1, DS: Data1

Start: Mov AX, Seg data1
Mov DS, AX
Mov SI, Offset DEC1
Mov AL, 0FFH
Mov AH, 00H
Mov BL, 64H
DIV BL
Mov [SI], AL
Mov AL, AH
Mov AH, 00
Mov BL, 0AH
DIV BL
INC SI
Mov [SI], AH

Mov AH, 4CH
INT 21H
Code1 Ends
End Start



OUTPUT:
0C5E:0000 02. 05. 05. 00.

No comments: