8086 programs

79
Girisha G K, Asst Professor 1: PROGRAMS INVOLVING DATA TRANSFER INSTRUCTIONS ; 1:- PROGRAM 01a. ; FILE NAME: blktrawl.asm ; Program is tested and found OK ; //PROGRAM TO TRANSFER A BLOCK OF DATA FROM ONE MEMORY ; LOCATION TO ANOTHER MEMORY LOCATION WITH OVERLAP\\ .model small .stack .data memloc1 db 02h, 04h, 06h, 08h, 0ah memloc2 db 01h, 03h, 05h, 07h, 09h .code mov ax, @data ;// Intialize mov ds, ax ;data segment\\ mov cx, 05h ;intialize block counter lea si, memloc1 ;load the offset address of source memory location into SI lea di, memloc2 ;load the offset address of destination mem location into DI back: mov al, [si+4] ;load the element pointed by SI+4 to AL mov [di+2], al ;transfer the content of AL to mem location pointed by DI+2 dec si ;decrement SI to point to next element of the block dec di ;decrement Destination pointer loop back ;if counter is not zero continue the transfer mov ah, 4ch ;// exit to int 21h ; DOS\\ end ;end the program Dept of ECE, NMIT, Bangalore. 1

Upload: brijesh-m-nayak

Post on 22-Jan-2016

99 views

Category:

Documents


10 download

DESCRIPTION

8086 program set

TRANSCRIPT

Page 1: 8086 Programs

Girisha G K, Asst Professor

– 1: PROGRAMS INVOLVING DATA TRANSFER INSTRUCTIONS

; 1:- PROGRAM 01a.

; FILE NAME: blktrawl.asm

; Program is tested and found OK

; //PROGRAM TO TRANSFER A BLOCK OF DATA FROM ONE MEMORY ; LOCATION TO ANOTHER MEMORY LOCATION WITH OVERLAP\\

.model small

.stack

.datamemloc1 db 02h, 04h, 06h, 08h, 0ahmemloc2 db 01h, 03h, 05h, 07h, 09h

.codemov ax, @data ;// Intializemov ds, ax ;data segment\\mov cx, 05h ;intialize block counterlea si, memloc1 ;load the offset address of source memory location into SIlea di, memloc2 ;load the offset address of destination mem location into DI

back: mov al, [si+4] ;load the element pointed by SI+4 to ALmov [di+2], al ;transfer the content of AL to mem location pointed by DI+2dec si ;decrement SI to point to next element of the blockdec di ;decrement Destination pointerloop back ;if counter is not zero continue the transfermov ah, 4ch ;// exit toint 21h ; DOS\\

end ;end the program

EXAMPLE RESULT:Before execution:

[????]: 02h, 04h, 06h, 08h, 0ah, 01h, 03h, 05h, 07h, 09hAfter execution:

[????]: 02h, 04h, 06h, 02h, 04h, 06h, 08h, 0ah, 07h, 09h

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.1

Page 2: 8086 Programs

Girisha G K, Asst Professor

; 1:- PROGRAM 01b.

; FILE NAME: blktrawo.asm

; Program is tested and found OK

; //PROGRAM TO TRANSFER A BLOCK OF DATA FROM ONE MEMORY ; LOCATION TO ANOTHER MEMORY LOCATION WITHOUT OVERLAP\\

.model small

.stack

.datamemloc1 db 02h, 04h, 06h, 08h, 0ahmemloc2 db 5 dup (0)

.codemov ax, @data ;// intializemov ds, ax ;data segment\\mov cx, 05h ;intialize block counterlea si, memloc1 ;load the offset address of src mem loc into SIlea di, memloc2 ;load the offset address of dest mem loc into DI

back: mov al, [si] ;load the element pointed by SI to ALmov [di], al ;transfer the content of AL to mem loc pointed by

DIinc si ;increment SI to point to next element of the blockinc di ;increment Destination pointerloop back ;if counter is not zero continue the transfermov ah, 4ch ;// exit toint 21h ; DOS\\

end ;end the program

EXAMPLE RESULT:Before execution:

Memloc1: [????]: 02h, 04h, 06h, 08h, 0ahMemloc2: [????]: 00h, 00h, 00h, 00h, 00h

After execution:Memloc1: [????]: 02h, 04h, 06h, 08h, 0ahMemloc2: [????]: 02h, 04h, 06h, 08h, 0ah

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.2

Page 3: 8086 Programs

Girisha G K, Asst Professor

; 1:- PROGRAM 02

; FILE NAME: blkint.asm

; Program is tested and found OK

; //PROGRAM TO INTERCHANGE A BLOCK OF DATA OF TWO MEMORY ; LOCATIONS\\

.model small

.stack

.datamemloc1 db 02h, 04h, 06h, 08h, 10hmemloc2 db 12h, 14h, 16h, 18h, 20h

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov cx, 05h ;intialize the counterlea si, memloc1 ;effective address of mem loc1 in silea di, memloc2 ;effective address of mem loc1 in dimov es, ax

back: mov bl, [si] ;transfer the contents of si into blxchg bl, [di] ;exchange the contents of di and blmov [si], bl ;load the contents of bl into siinc si ;increment siinc di ;increment diloop back ;continue the transfer till count = 0mov ah, 4ch ;// Terminateint 21h ; the program\\

end

EXAMPLE RESULT:Before execution:

Memloc1: [????]: 02h, 04h, 06h, 08h, 10hMemloc2: [????]:12h, 14h, 16h, 18h, 20h

After execution:Memloc1: [????]:12h, 14h, 16h, 18h, 20hMemloc2: [????]: 02h, 04h, 06h, 08h, 10h

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.3

Page 4: 8086 Programs

Girisha G K, Asst Professor

-2: PROGRAMS INVOLVING ARITHMETIC AND LOGICAL OPERATIONS

; 2:- PROGRAM 01a

; FILE NAME: addbyte.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO ADD TWO 8-BIT DATA\\

.model small

.stack

.datanum1 db 33hnum2 db 44hresult dw (0)

.codemov ax, @data ;//Intialize themov ds, ax ; data segment\\mov al, num1 ;get first number in aladd al, num2 ;add al(first number) with 2nd numbermov result, al ; store the sum in result locmov ah, 4ch ;// terminate int 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.4

Page 5: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 01b

; FILE NAME: addword.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO ADD TWO 16-BIT NUMBERS\\

.model small

.stack

.datanum dw 1133h, 2244hresult dw 2 dup(0)carry db (0)

.codemov ax, @data ;//Intialize themov ds, ax ; data segment\\mov ax, num ;get first num word in axclc ;CF = 0add ax, num+2 ;add first num word with 2nd word mov result, ax ;save the sum in resultadc carry, 00h ;save the carry if anymov ah, 4ch ;// terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.5

Page 6: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 01c

; FILE NAME: adddword.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO ADD TWO 32-BIT (MULTI-PRECISION) ; NUMBERS\\

.model small

.stack

.datanum1 dd 01234567h ;// two 32-bit numbersnum2 dd 89abcdefh ; to be added\\sum dd (0)carry db (0)

.codemov ax, @data ;//Intialize themov ds, ax ; data segment\\mov ax, word ptr num1 ;ax = LSBs of 1st nummov bx, word ptr num2 ;bx = LSBs of 2nd numclcadd ax, bx ;ax = ax + bxmov bx, word ptr [num1+2] ;bx = MSBs of 1st nummov cx, word ptr [num2+2] ;cx = MSBs of 2nd numadc bx, cx ;bx = bx + cx + CFmov word ptr [sum+2], bx ;// store the resultmov word ptr sum, ax ; in the memory\\adc carry, 00h ;save carry if anymov ah, 4ch ;// Terminate int 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.6

Page 7: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 02a

; FILE NAME: subbyte.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO SUBTRACT TWO 8-BIT NUMBERS\\

.model small

.stack

.datanum1 db 77h ;//the two numbersnum2 db 88h ; to be added\\result db (0)borrow db (0)

.codemov ax, @data ;//Intialize the mov ds, ax ; data segment\\clc ;CF = 0mov al, num1 ;1st num in alsub al, num2 ;subtract 2nd num from 1st nummov result, al ;store the resultsbb borrow,00h ;save borrow if anymov ah, 4ch ;// terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.7

Page 8: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 02b

; FILE NAME: subword.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO SUBTRACT TWO 16-BIT NUMBERS\\

.model small

.stack

.datanum dw 5555h, 4444h ;two words for subtractionresult dw 2 dup(0)borrow db (0)

.codemov ax, @data ;//Intialize mov ds, ax ; the data segment\\clcmov ax, num ;minuend in axsub ax, num+2 ;subtract subtrahend from minuendmov result, ax ;store the result in memorysbb borrow, 00h ;save the borrow if anymov ah, 4ch ;//Terminate int 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.8

Page 9: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 02c

; FILE NAME: subdword.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO SUBTRACT TWO 32-BIT NUMBERS\\

.model small

.stack

.datanum1 dd 89abcdefh ;minuendnum2 dd 01234567h ;subtrahendresult dd (0)borrow db(0)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov ax, word ptr num1 ;LSBs of minuendmov bx, word ptr num2 ;LSBs of subtrahendclc ;CF = 0sub ax, bx ;ax = ax - bxmov bx, word ptr num1+2 ;MSBs of minuendmov cx, word ptr num2+2 ;MSBs of subtrahendsbb bx, cx ;bx = bx - cxmov word ptr result+2, bx ;// store the mov word ptr result, ax ; result in memory locations\\sbb borrow, 00h ;save borrow if anymov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.9

Page 10: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 03a

; FILE NAME: mulsign.asm

; Program is tested and found OK

; //ASSEMBLY LEVEL PROGRAM TO MULTIPLY SIGNED HEXADECIMAL ; NUMBERS\\

.model small

.stack

.datanum db -19h, -05h ;two unsigned nos for multiplicationresult db ?

.codemov ax, @data ;//Intialize mov ds, ax ; the data segment\\mov al, num ;get the first nomov bl, num+1 ;get the second noimul bl ;multiply two signed nosmov result, al ;store the result in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.10

Page 11: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 03b

; FILE NAME: mulusign.asm

; Program is tested and found OK

; //ASSEMBLY LEVEL PROGRAM TO MULTIPLY UNSIGNED HEXADECIMAL ; NUMBERS\\

.model small

.stack

.datanum db -19h, -05h ;two unsigned nos for multiplicationresult db ?

.codemov ax, @data ;// Intialize mov ds, ax ; the data segment\\mov al, num ;get the first nomov bl, num+1 ;get the second nomul bl ;multiply two unsigned nosmov result, al ;store the result in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.11

Page 12: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 04a

; FILE NAME: divsign.asm

; Program is tested and found OK

; //ASSEMBLY LEVEL PROGRAM TO DIVIDE TWO UNSIGNED HEXADECIMAL ; NUMBERS\\

.model small

.stack

.datadividend dw -19h ;dividenddivisor db -05h ;divisorresult db ?

.codemov ax, @data ;//Intialize mov ds, ax ; the data segment\\mov ax, dividend ;get the first nomov bl, divisor ;get the second noidiv bl ;divide 1st num by 2nd nummov result, al ;store the result in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.12

Page 13: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 04b

; FILE NAME: divusign.asm

; Program is tested and found OK

; //ASSEMBLY LEVEL PROGRAM TO DIVIDE TWO UNSIGNED HEXADECIMAL ; NUMBERS\\

.model small

.stack

.datadividend dw 19h ;dividenddivisor db 05h ;divisorresult db ?

.codemov ax, @data ;//Intialize mov ds, ax ; the data segment\\

mov ax, dividend ;get the first no mov bl, divisor ;get the second no

div bl ;divide 1st num by 2nd nummov result, al ;store the result in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.13

Page 14: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 05a

; FILE NAME: bcdtobin.asm

; Program is tested and found OK

; //PROGRAM TO CONVERT A BCD NUMBER TO BINARY NUMBER (HEX ; NUMBER)\\

.model small

.stack

.datanum db 67hresult db (0)

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\

mov al, num ;get the num to be convertedmov cl, 04 ;intialize rotation countand al, 0fh ;mask higher numbermov bl, al ;save ax in bxmov al, num ;restore axand al, 0f0h ;mask lower nibblercr al, cl ;rotate ax, cl timesmov dl, 0ah ;dl = 0ahmul dl ;al * 10 add al, bl ;al = 60h + 07h, --> 43hmov result, al ;store the resultmov ah, 4ch ;// Terminate int 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.14

Page 15: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 05b

; FILE NAME:- bintobcd.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO CONVERT A BINARY (HEX) NUMBER TO BCD ; NUMBER\\

.model small

.stack

.datanum dw 0abch ;number to be converted

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov ax, num ;ax = nummov bx, 0ah ;bx = 10hmov cx, 00h ;cx = 00h ; clear counter

repeat: mov dx, 00h ;dx = 00hdiv bx ;ax = ax/bxpush dx ;save remainderinc cx ;increment countercmp ax, 00h ;test if quotient = 0jnz repeat ;if not divide again

skip: pop dx ;get remainderadd dl, 30h ;convert to ASCIImov ah, 06h ;display digitint 21hloop skipmov ah, 4ch ;//Terminateint 21h ; the program\\end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.15

Page 16: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 06

; FILE NAME: squcube.asm

; Program is tested and found OK

; //PROGRAM TO FIND THE SQUARE AND CUBE OF A GIVEN NUMBER\\

.model small

.stack

.datanum db 04hsqu db (0)cube db (0)

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov al, num ;al = 05hmul al ;al = al * almov squ, al ;squ = al = 10hmul num ;al = al(squ) * nummov cube, al ;cube = al = 40hmov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.16

Page 17: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 07

; FILE NAME: lcm.asm

; Program is tested and found OK

; ASSEMBLY LEVEL PROGRAM TO FIND THE LCM OF TWO NUMBERS

Wrong program.model small.stack.data

num1 db 05h, 03h ;// two numbers lcm dw (0)

.codemov ax, @data ;// Intialize mov ds, ax ; the data segment\\mov ax, 00hmov dx, 00hmov bx, 00hmov al, num1 ;store num1 into almov bl, num1+1 ;store num1+1 into bl

back: push axdiv bx ;divide ax by bxcmp dx, 00h ;compare if remainder = 0je exit ;if yes, ax is LCM otherwise notpop ax ;retrieve axadd al, num1 ;add ax with num1mov dx, 00 ;clear dxjmp back ;jump to back

exit: pop lcm ;get the result (LCM)mov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.17

Page 18: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 08

; FILE NAME:- gcd.asm

; Program is tested and found OK

; //PROGRAM TO FIND THE GCD OF TWO 16-BIT NUMBERS\\

.model small

.stack

.datanum1 dw 15 ;two numbersnum2 dw 20gcd dw ?

.codemov ax, @data ;//Intialize mov ds, ax ; the data segment\\

mov bx, num1 ;get num1 in axmov cx, num2 ;get num2 in bxcmp bx, cx ;compare for the biggest nojb divide ;//if cx is big then proceedxchg bx, cx ; else exchange the nos\\

divide: mov dx, 00hmov ax, cxdiv bx ;//dividend/divisorcmp dx, 00h ;//if rem = 0, theje over ; divisor is the GCD\\mov cx, bx ;//else divisor = dividendmov bx, dx ; divisor = remainderjmp divide

over: mov gcd, bx ;store the GCD in mem locmov ah, 4ch ;//Terminateint 21h ; the main program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.18

Page 19: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 09

; FILE NAME:- factorl.asm

; Program is tested and found OK

; //PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER\\

.model small

.stack

.datanum dw 05hmsg db 'factorial of 0 is 1 $'factorl dw (?)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov bx, num ;get the numbercmp bx, 00h ;//factorial of zeroje disp ; is one\\call factorial ;find factorial calling subroutinemov factorl, ax ;store factorial in mem locjmp exit

disp: mov ah, 09h ;//string display lea dx, msg ; interrupt\\int 21h

exit: mov ah, 4ch ;//Terminateint 21h ; the program\\

; //SUBROUTINE TO FIND FACTORIAL OF A GIVEN NUMBER\\factorial proc near ;start of the procedure

cmp bx, 01h ;//factorial of jbe return ; one is one\\push bx ;save bx in stackdec bx ;decrement bxcall factorial ;call the subroutine recursivelypop bx ;get back the value for bxmul bx ;multiply the factors of a given numret ;return to the main prgm

factorial endp ;end of the procedure

return: mov ax, 01h ;// return theret ; factorial for one\\

endOBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.19

Page 20: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 10a

; FILE NAME: aaa.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM USING AAA INSTRUCTION AFTER ADDITION\\

.model small

.stack

.datanum dw 38h, 37hresult dw ?

.codemov ax, @data ;// Intialize mov ds, ax ; the data segment\\mov ax, num ;ax = 38hmov bx, num+2 ;bx = 37hadd ax, bx ;ax = ax + bx = 6fhaaa ;ah = 01h, al = 05hadd ax, 3030h ;convert to ascii valuemov result, ax ;ax = 3135hmov ah, 4ch ;// terminate the int 21h ; program\\

end

RESULT:Input: num = 0038h, num+2 = 0037h

Output: result = 35, result+1 = 31

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.20

Page 21: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 10b

; FILE NAME: aas.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM USING AAS INSTRUCTION AFTER ADDITION\\

.model small

.stack

.datanum dw 39h, 33hresult dw ?

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov ax, num ;ax = 39hmov bx, num+2 ;bx = 33hsub ax, bx ;ax = ax - bx = 06haas ;ah = 00h, al = 06hadd ax, 3030h ;convert to ascii valuemov result, ax ;ax = 3036hmov ah, 4ch ;// terminate the int 21h ;program\\

end

RESULT:Input: num = 0039h, num+2 = 0033h

Output: result = 00, result+1 = 36

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.21

Page 22: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 10c

; FILE NAME: aam.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM USING AAM INSTRUCTION AFTER ADDITION\\

.model small

.stack

.datanum db 06h, 08hresult dw ?

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov al, num ;al = 06hmov bl, num+1 ;bl = 08hmul bl ;ax = al * bl = 30haam ;ah = 04h, al = 08hadd ax, 3030h ;convert to ascii valuemov result, ax ;ax = 3438hmov ah, 4ch ;// terminate the int 21h ;program\\

end

RESULT:Input: num = 06h, num + 1 = 08h

Output: result = 38, result+1 = 34

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.22

Page 23: 8086 Programs

Girisha G K, Asst Professor

; 2:- PROGRAM 10d

; FILE NAME: aad.asm

; Program is tested and found OK

; //Assembly program using AAM instruction after addition\\

.model small

.stack

.datanum1 dw 0205hnum2 db 07hresult dw ?

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov ax, num1 ;ax = 0205hmov bh, num2 ;bh = 07haad ;ax = 19h --> 25 decimaldiv bh ;ax = 0403mov result, axmov ah, 4ch ;// terminate the int 21h ;program\\

end

RESULT:Input: num1 = 0205h, num2 = 07h

Output: result = 03, result+1 = 04

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.23

Page 24: 8086 Programs

Girisha G K, Asst Professor

-3: PROGRAMS INVOLVING BIT MANIPULATION INSTRUCTIONS

; 3:- PROGRAM 01

; FILE NAME: posorneg.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND THE GIVEN NUMBER IS POSITIVE OR ; NEGATIVE\\

.model small

.stack

.datamessage1 db 'Number is Positive $'message2 db 'Number is Negative $'num db 0c0h

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov al, num ;get the number in al to be checkedand al, 80h ;retain only the MSBjnz negnum ;if ZF = 1, the number is negativemov ah, 09h ;//else the number is positivemov dx, offset message1 ; and display the messageint 21h ; on console\\jmp exit

negnum:mov ah, 09h ;//display the negative numbermov dx, offset message2 ; messageint 21h ; on console\\

exit: mov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.24

Page 25: 8086 Programs

Girisha G K, Asst Professor

; 3:- PROGRAM 02

; FILE NAME:oddorevn.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND THE GIVEN NUMBER IS ODD OR EVEN\\

.model small

.stack

.datamessage1 db 'Number is even $'message2 db 'Number is odd $'num db 40h

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\mov al, num ;al = numror al,01h ;check for the LSBjnc even ;if CF = 0 --> even else odd numbermov dx, offset message2 ;display message for oddjmp odd

even: mov dx, offset message1 ;display message for evenodd: mov ah, 09h

int 21hmov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.25

Page 26: 8086 Programs

Girisha G K, Asst Professor

; 3:- PROGRAM 03

; FILE NAME: onszro.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND THE NUMBER OF ONES AND ZEROS IN A ; GIVEN NUMBER\\

.model small

.stack

.datanum db 40hcnt_one db (0)cnt_zero db (0)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov al, num ;al = 40hmov cx, 08h ;cx = count (no of bits in al)

repeat: rol al, 01h ;rotate each bit rightjnc next ;if CF = 0, count as no of zerosinc cnt_one ;else count as no of onesjmp skip

next: inc cnt_zero ;no of zerosskip: loop repeat ;repeat until all the bits are checked

mov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.26

Page 27: 8086 Programs

Girisha G K, Asst Professor

; 3:- PROGRAM 04

; FILE NAME: 2outof5.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO CHECK WHETHER A BYTE BELONGS TO 2 OUT ; OF 5 CODE\\

;** The code is valid if the number of ones in the least 5 bits are 2**

.model small

.stack

.data data1 db 80h

cnt_one db (0)message1 db 'valid 2 out of 5 code $'message2 db 'invalid 2 out of 5 code $'

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov al, data1 ;al = data1mov cx, 05h ;intialize the counter

back: ror al, 01h ;rotate right al bit by bitjnc next ;if CF = 0 jump to nextinc cnt_one ;if CF = 1, count no of ones

next: loop back ;repeat to count no of ones in the 1st 5-bitscmp cnt_one, 02h ;compare no of ones with 02hjnz skip ;if no of ones is 2 then it is 2 out of 5 codemov ah, 09h ;//display the messagelea dx, message1 ; as valid 2 out of 5 code\\int 21hjmp end1

skip: lea dx, message2 ;//if no of ones is not 2 then mov ah, 09h ; display the message asint 21h ; invalid 2 out of 5 code\\

end1: mov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.27

Page 28: 8086 Programs

Girisha G K, Asst Professor

; 3:- PROGRAM 05a

; FILE NAME: bitpali.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND WHETHER A GIVEN BYTE IS A BITWISE ; PALINDROME OR NOT\\

.model small

.stack

.datanum db 18hmsg1 db 'number is a bitwise palindrome $'msg2 db 'number is not a bitwise palindrome $'

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov al, num ;get the number in almov bl, al ;save a copy in blmov cx, 08h ;intialze the counter to 8

back: ror al, 01h ;rotate al to right oncejnc skip ;if no carry jump to skiprol bl, 01h ;else rotate bl left oncejc go_on ;if carry jump to go_onjmp term ;else jump to term

skip: rol bl, 01h ;rotate bl left oncejc term ;if carrry jump to term

go_on: loop back ;else cx = cx-1 & if cx != 0, jump backmov ah, 09h ;// display msg1lea dx, msg1 ; if given number is int 21h ; a bitwise palindrome\\jmp last ;jump to last

term: mov ah, 09h ;//display msg2lea dx, msg2 ; if given number isint 21h ; not a bitwise palindrome\\

last: mov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.28

Page 29: 8086 Programs

Girisha G K, Asst Professor

; 3:- PROGRAM 05b

; FILE NAME: nibpali.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND WHETHER A GIVEN BYTE/WORD IS A ; NIBBLE WISE PALINDROME OR NOT\\

.model small

.stack

.datanum db 45hmsg1 db 'number is a nibble wise palindrome $'msg2 db 'number is not a nibble wise palindrome $'

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov al, num ;al = numand al, 0fh ;mask the higher nibblemov bl, al ;save al in blmov al, num ;again al = numand al, 0f0h ;mask the lower nibblemov cl, 04h ;intialize counter as 04rol al, cl ;//compare both lower cmp al, bl ; and higher nibbles\\jz succ ;if both the nibbles are same jump to succmov dx, offset msg2 ;//else display the numbermov ah, 09h ; is not a palindrome\\int 21hjmp last

succ: lea dx, msg1 ;//display the message asmov ah, 09h ; given no is a palindrome\\int 21h

last: mov ah, 4ch ;//terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.29

Page 30: 8086 Programs

Girisha G K, Asst Professor

-4: PROGRAMS INVOLVING BRANCH/LOOP INSTRUCTIONS

; 4:- PROGRAM 01a

; FILE NAME: addndata.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO ADD ‘N’ NUMBER OF DATA IN AN ; ARRAY\\

.model small

.stack

.dataarray db 02h, 04h, 06h, 08h, 10h, 12h, 14h, 16h, 18h, 20hary_cnt equ 0ahsum dw (0)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov cx, ary_cnt ;intialize the counterxor di, di ;clear di registerlea bx, array ;bx = effective address of array

back: mov al, [bx+di] ;al = first element of arraymov ah, 00h ;clear higher byte of axadd sum, ax ;//add sum and array inc di ; elements one by one\\loop back ;add all the elements of the arraymov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.30

Page 31: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 01b

; FILE NAME: avgndata.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO FIND AVERAGE OF ‘N’ NUMBER OF ; DATA IN AN ARRAY\\

.model small

.stack

.dataarray db 02h, 04h, 06h, 08h, 10h, 12h, 14h, 16h, 18h, 20hary_cnt equ 0ahsum dw (0)avg db (0)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov cx, ary_cnt ;intialize the counterxor di, di ;clear diclc ;CF = 0lea bx, array ;bx = effective address of array

back: mov al, [bx+di] ;al = first element of arraymov ah, 00h ;ah = 00hadc sum, ax ;// add all the elementsinc di ; of the arrayloop back ; and the result will be in sum\\mov ax, sum ;ax = summov cl, ary_cnt ;cl = no of elements in arraydiv cl ;al = ax/clmov avg, al ;avg = almov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.31

Page 32: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 02a

; FILE NAME: lrgstnum.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND THE LARGEST NUMBER IN THE ARRAY\\

.model small

.stack

.dataarray db 10h, 06h, 04h, 08h, 12hary_cnt equ 05hlrg_num db (0)

.codemov ax, @data ;// Intializemov ds, ax ; the data segment\\xor di, di ; di = 0mov cx, ary_cnt ;cx = no of elements in the arraylea bx, array ;bx = effective address of arraymov al, lrg_num ;assume lrgst no at present is 0

back: cmp al, [bx+di] ;compare lrge no to the first elementjnc skip ;// if CF= 0, the element is smallermov dl, [bx+di] ; else move large no intomov al, dl ; al using 3rd register\\

skip: inc diloop back ;repeat for all the elements of the arraymov lrg_num, al ;store largest no in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.32

Page 33: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 02b

; FILE NAME: smlstnum.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO FIND THE SMALLEST NUMBER IN THE ARRAY\\

.model small

.stack

.dataarray db 02, 07, 06, 10, 05ary_cnt equ 05hsml_num db (0)

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\xor di, di ; di = 0mov cx, ary_cnt ;cx = no of elements in arraylea bx, array ;bx = effective address of arraymov al, [bx+di] ;al <-- first element of arraydec cxinc di

back: cmp al, [bx+di] ;compare 1st and 2nd element of arrayjc skip ;//if al = small, skip mov dl, [bx+di] ; else take the small nomov al, dl ; into al and then proceed

skip: inc di ; to check the next element\\loop backmov sml_num, al ;store small number in mem locmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.33

Page 34: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 03a

; FILE NAME: bubsorta.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN DATA IN ASCENDING ; ORDER IN AN ARRAY USING BUBBLE SORT.\\

.model small

.stack

.dataarray db 24h, 56h, 48h, 35hary_cnt equ 04

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov bx, ary_cnt-1 ;no of passes

nxtchk: mov cx, bx ;no of checksmov si, 00h ; si = 0

nxtpas: mov al, array[si] ;al = 1st element inc sicmp al, array[si] ;compare 1st and 2nd elementjbe nxtemt ;//if below jump toxchg al, array[si] ; nxtemt otherwise mov array[si-1], al ; exchange the elements\\

nxtemt: loop nxtpas ;proceed with next passdec bx ;dx = dx - 1jnz nxtchk ;continue with next checkmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.34

Page 35: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 03b

; FILE NAME: bubsortd.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN DATA IN ; DESCENDING ORDER IN AN ARRAY USING BUBBLE SORT.\\

.model small

.stack

.dataarray db 24h, 56h, 48h, 35hary_cnt equ 04

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov bx, ary_cnt-1 ;no of passes

nxtchk: mov cx, bx ;no of checksmov si, 00h ; si = 0

nxtpas: mov al, array[si] ;al = 1st element inc sicmp al, array[si] ;compare 1st and 2nd elementjge nxtemt ;//if greater jump toxchg al, array[si] ; nxtemt otherwise mov array[si-1], al ; exchange the elements\\

nxtemt: loop nxtpas ;proceed with next passdec bx ;dx = dx - 1jnz nxtchk ;continue with next checkmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.35

Page 36: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 04a

; FILE NAME: inssorta.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN ARRAY IN ; ASCENDING ORDER USING INSERTON SORT\\

.model small

.stack

.dataarray dw 14h, 5h, 24h, 32hcount dw ($-array)/2

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov cx, 2 ;cx = 2

loop1: mov dx, cx ;dx = cxdec dx ;dx = dx - 1mov si, dx ;si = dxadd si, si ;si = si + simov ax, array[si] ;//compare the first

loop2: cmp array[si-2], ax ; and 2nd element\\jbe loop3 ;if below leave as it ismov di, array[si-2] ;//else exchange the elementsmov array[si], didec si ;//decrement sidec si ; two times\\dec dxjnz loop2

loop3: mov array[si], axinc cxcmp cx, countjbe loop1mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.36

Page 37: 8086 Programs

Girisha G K, Asst Professor

; 4:- PROGRAM 04b

; FILE NAME: inssortd.asm

; Program is tested and found OK

; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN ARRAY IN ; DESCENDING ORDER USING INSERTON SORT\\

.model small

.stack

.dataarray dw 14h, 5h, 24h, 32hcount dw ($-array)/2

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov cx, 2 ;cx = 2

loop1: mov dx, cx ;dx = cxdec dx ;dx = dx - 1mov si, dx ;si = dxadd si, si ;si = si + simov ax, array[si] ;//compare the first

loop2: cmp array[si-2], ax ; and 2nd element\\jae loop3 ;if above leave as it ismov di, array[si-2] ;//else exchange the elementsmov array[si], didec si ;//decrement sidec si ; two times\\dec dxjnz loop2

loop3: mov array[si], axinc cxcmp cx, countjbe loop1mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.37

Page 38: 8086 Programs

Girisha G K, Asst Professor

-5: PROGRAMS ON STRING MANIPULATION INSTRUCTIONS

; 5:- PROGRAM 01

; FILE NAME: strngtra.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO TRASFER A STRING FROM ONE LOCATION TO ; ANOTHER MEMORY LOCATION\\

.model small

.stack

.datasrc db 'ELECTRONICS $'dst db ?str_cnt equ 0bh

.codemov ax, @data ;//Intializemov ds, ax ; the data andmov es, ax ; extra segments\\mov cx, str_cnt ;intialize the counter for no of charsmov si, offset src ;si = offset address of source stringmov di, offset dst ;di = offset address of destination memorycld ;DF = 0; auto increment of si & direp movsb ;trnsfr the contents pointed by si to mem pointed by

dimov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.38

Page 39: 8086 Programs

Girisha G K, Asst Professor

; 5:- PROGRAM 02

; FILE NAME: strngrev.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO REVERSE A GIVEN STRING\\

.model small

.stack

.data string db 'COMMUNICATION $' str_cnt equ 0ch

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov cx, str_cnt ;Intialize the array countermov si, offset string ;si = pointer to beginning of stringmov di, si ;//di = pointer toadd di, str_cnt ; end of the string\\

repeat: cmp di, si ;compare si & dijbe exit ;if di <= si, exitmov ah, [si] ;// otherwisexchg ah, [di] ; exchange the contentsmov [si], ah ; of si & di\\dec di ;decrement diinc si ;increment sijmp repeat ;jump to repeat

exit: lea dx, string ;// display themov ah, 09h ; reversed string\\int 21hmov ah, 4ch ;// Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.39

Page 40: 8086 Programs

Girisha G K, Asst Professor

; 5:- PROGRAM 03

; FILE NAME: chrsrch.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO SEARCH A CHARACTER IN A GIVEN STRING\\

.model small

.stack

.datastring db 'ELECTRONICS $'str_cnt equ 0bhmsg1 db 'character found $'msg2 db 'character not found $'loc db ?

.codemov ax, @data ;//Intializemov ds, ax ; data andmov es, ax ; extra segment\\mov cx, str_cnt ;intialize string counterlea di, string ; di = offset of stringmov al, 'T' ;al = character to be searched

back: cmp al, byte ptr[di] ;compare char with first element in the stringje found ;if found jump to foundinc di ;//else proceed with loop back ; comparing all chars in string\\lea dx, msg2 ;// if not found displaymov ah, 09h ; msg2 using DOS services\\int 21hjmp last

found: mov ah, 09h ;//if found displaylea dx, msg1 ; msg1 using DOS services\\int 21hmov dx, str_cnt ;// store the offsetsub dx, cx ; location of the desiredmov loc, dx ; char in a string\\

last: mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.40

Page 41: 8086 Programs

Girisha G K, Asst Professor

; 5:- PROGRAM 04

; FILE NAME: strpali.asm

; Program is tested and found OK

; //ASSEMBLY PROGRAM TO CHECK WHETHER A GIVEN STRING IS A ; PALINDROME OR NOT\\

.model small

.stack

.datastring db 'MALAYALAM $'str_cnt equ 09hmsg1 db 'String is not a palindrome $'msg2 db 'string is a palindrome $'

.codemov ax, @data ;//Intializemov ds, ax ; the datamov es, ax ; and extra segment\\mov cx, str_cnt ;intialize the counterdec cx ;decrement counterlea si, src ;si = offset of source stringlea di, src ;source offset in di alsoadd si, cx ;si point to last location

back: mov ah, [si] ;last char in ahmov al, [di] ;first char in alcmp ah, al ;//if they are notjne last ; equal go to last\\dec si ;//if they are equal inc di ; decrement si and increment di\\cmp si, di ;//compare si & di if they are equal jbe disp ; display msg2\\jmp back ;else continue to compare for nxt chars

disp: lea dx, msg2 ;display msg2mov ah, 09hint 21hjmp exit

last: lea dx, msg1 ;display msg1mov ah, 09hint 21h

exit: mov ah, 4ch ;//Terminateint 21h ; the program\\

endOBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.41

Page 42: 8086 Programs

Girisha G K, Asst Professor

-6: PROGRAMS INVOLVING DOS SOFTWARE INTERRUPTS (INT 21H)

; 6:- PROGRAM 01

; FILE NAME: readkb.asm

; Program is tested and found OK

; // PROGRAM TO READ A CHARACTER FROM THE KEYBOARD\\

.model small

.stack

.data

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov ah, 01h ;//DOS function to get aint 21h ; character from keyboard\\mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.42

Page 43: 8086 Programs

Girisha G K, Asst Professor

; 6:- PROGRAM 02

; FILE NAME: readkbf.asm

; Program is tested and found OK

; // PROGRAM TO READ A CHARACTER FROM THE KEYBOARD WITH ; BUFFERED INPUT\\

.model small

.stack

.databuff db 30 ;max length passed to the function

db 0 ;actual length returned by function db 30 dup(0) ;space for storing returned string from function.code

mov ax, @data ;//Intializemov ds, ax ; the data segment\\mov ah, 0ah ;//DOS function tolea dx, buff ; read string from the keyboard\\int 21hmov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.43

Page 44: 8086 Programs

Girisha G K, Asst Professor

; 06:- PROGRAM 03

; FILE NAME: dispchr.asm

; Program is tested and found OK

; //PROGRAM TO DISPLAY A CHARACTER ON CONSOLE\\

.model small

.stack

.data

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\mov ah, 02h ;//call DOS servicemov dl, 'E' ; to display the char 'E'int 21h ; on console\\mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.44

Page 45: 8086 Programs

Girisha G K, Asst Professor

; 06:- PROGRAM 04

; FILE NAME: dispstr.asm

; Program is tested and found OK

; //PROGRAM TO DISPLAY A STRING ON CONSOLE\\

.model small

.stack

.datastr db 'THURST TO LEARN$'

.codemov ax, @data ;//Intializemov ds, ax ; the data segment\\lea dx, str ;dx = offset address of strmov ah, 09h ;//call DOS service to int 21h ; display the string\\mov ah, 4ch ;//Terminateint 21h ; the program\\

end

OBSERVED RESULT:

Dept of ECE, NMIT, Bangalore.45

Page 46: 8086 Programs

Girisha G K, Asst Professor

; 07:- PROGRAM 01

; FILE NAME: pcikey.asm

; Program is tested and found OK

; //PROGRAM TO DEMONSTRATE INTERFACING OF MATRIX KEYBOARD\\

Schematic Diagram of key board:

Dept of ECE, NMIT, Bangalore.46

Page 47: 8086 Programs

Girisha G K, Asst Professor

.MODEL SMALL ;Specify the model for the executable. Must for every program.

.STACK 5000H

.DATA ; Any data declarations here.Message1 DB 'DEMONSTRATION PROGRAM FOR KEYBOARD

INTERFACE',13,10,'$' Message2 DB 'This program will display the key you pressed on the

Interface.',13,10,'$' Message3 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$' Message4 DB 13,'The Key You Pressed is : ','$' Keys DB '0 1 2 3 4 5 6 7 8 9 . + - X / % ACCECK= MCMRM M+','$' Show DB '01','$ '

CR EQU 0c003H PA EQU 0c000H PB EQU 0c001H PC EQU 0c002H

.CODE ;Start your coding here.

MOV AX,@DATA ; Initialize all segment registers as needed here. MOV DS, AX

MOV AH, 9h ; Display the message line1. MOV DX, OFFSET Message1 INT 21h MOV AH, 9h ; Display the message line2. MOV DX, OFFSET Message2 INT 21h MOV AH, 9h ; Display the message line3. MOV DX, OFFSET Message3 INT 21h

MOV AX, 92h ; Initialize Port A - Input, CU & CL - Output MOV DX, CR OUT DX, AX ; Write to Control Register.

GETKEY: MOV BH, 1h ; Scan Lines MOV BL, 00h ; Initialize a counter. It contains the no of the Key

SCANLINES: MOV AL, BH MOV DX, PC ; Send Line Number to Port CL OUT DX, AL

MOV DX, PA ; Read from Port A IN AL, DX

Dept of ECE, NMIT, Bangalore.47

Page 48: 8086 Programs

Girisha G K, Asst Professor

MOV CH, AL ; CH has the value indicating the key pressed MOV AL,00HCHECK: ; Initialize the counter ; Now repeatedly check which key was selected. MOV CL, CH AND CL, 01h ; CH is shifted to right CMP CL, 01h JZ DISPLAY ; If Equal Come out INC BL SHR CH, 01h ; CH = CH >> 1 INC AL CMP AL, 08h ; All bits are compared JNZ CHECK ; Go back for next scan line SHL BH, 01h ; Move to next scan line CMP BH, 10h JNZ SCANLINES ; Repeat the SCAN Lines Loop (4 times) JMP LOOPOUT

DISPLAY: ; Display the selected key MOV AH, 9h ; Display the message line3. MOV DX, OFFSET Message4 INT 21h

MOV AX, 0000h MOV AL, BL MOV BL, 02h MUL BL MOV BX, AX MOV DI, OFFSET Show MOV AL, Keys[BX] MOV Show[0h], AL MOV AL, Keys[BX + 1h] MOV Show[1h], AL MOV AH, 9h ; Display the character pressed. MOV DX, OFFSET Show INT 21h MOV CX, 0FFFFh DELAY: MOV AX, 0FFhDELAY1: DEC AX JNZ DELAY1 LOOP DELAY

LOOPOUT: MOV AH, 01h INT 16h ; Get the Key JZ GETKEY ; Check for the key

Dept of ECE, NMIT, Bangalore.48

Page 49: 8086 Programs

Girisha G K, Asst Professor

MOV AH, 4ch ; Exit the program safely. INT 21hEND ; This is the end of your program.

Dept of ECE, NMIT, Bangalore.49

Page 50: 8086 Programs

Girisha G K, Asst Professor

; 07:- PROGRAM 02

; FILE NAME: pcisev.asm

; Program is tested and found OK

; //PROGRAM TO DEMONSTRATE INTERFACING OF SEVEN SEGMENT DISPLAY\\

Schematic Diagram of Seven Segment Display:

Dept of ECE, NMIT, Bangalore.50

Page 51: 8086 Programs

Girisha G K, Asst Professor

.MODEL SMALL ;Specify the model for the executable. Must for every program.

.STACK 5000H

.DATA ;Any data declarations here. Message1 DB 'DEMONSTRATION PROGRAM FOR SEVEN SEGMENT DISPLAY',13,10,'$' Message2 DB 'Check the Message < ELECTRO SYSTEMS > on Seven Segment Display',13,10,'$' Message3 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$'

DisplayData DB 0ffh,0ffh,0ffh,0ffh,0c6h,086h,0c7h,086h,0bfh,0c0h,0deh,087h,0bfh,092h,091h,092h,092h,0c8h,086h,087h CR EQU 0c003H PA EQU 0c000H PB EQU 0c001H PC EQU 0c002H

.CODE ;Start your coding here.

MOV AX,@DATA ;Initialize all segemnt regesters as needed here. MOV DS,AX

MOV AH,9h ;Display the message line1. MOV DX, OFFSET Message1 INT 21h MOV AH,9h ;Display the message line2. MOV DX, OFFSET Message2 INT 21h MOV AH,9h ;Display the message line3. MOV DX, OFFSET Message3 INT 21h

; Send the control word

MOV AL,80h MOV DX,CR OUT DX,AL

GETKEY: MOV BX,0000h

LOOP1: ; Display the charecters 4 at a tine. MOV AL,BL AND AL,03h CMP AL,00H JNZ NO_DELAY MOV CX,0FFFFhDELAY : MOV AX,0FFFh

Dept of ECE, NMIT, Bangalore.51

Page 52: 8086 Programs

Girisha G K, Asst Professor

DELAY1: DEC AX JNZ DELAY1 LOOP DELAYNO_DELAY:

MOV CL,00h MOV CH,DisplayData[BX]LOOP2: MOV AH,01h INT 16h ;Get the Key JNZ END_PROGRAM

;MOV CL,01 MOV AH,CH AND AH,80h CMP AH,00h JNZ CONTROL MOV DX,PB MOV AL,00h OUT DX,AL JMP END_CONTROLCONTROL: MOV DX,PB MOV AL,01h OUT DX,AL END_CONTROL:

MOV DX,PC MOV AL,01h OUT DX,AL MOV DX,PC MOV AL,00h OUT DX,AL SHL CH,1 INC CL CMP CL,08h

JNZ LOOP2 ;LOOP2 Repeats from here.

INC BX CMP BX,20 JNZ LOOP1 ;LOOP1 Repeats from here.

MOV AH,01h INT 16h ;Get the Key JZ GETKEY

END_PROGRAM:

Dept of ECE, NMIT, Bangalore.52

Page 53: 8086 Programs

Girisha G K, Asst Professor

MOV AH,4ch ; Exit the program safely. INT 21hEND ;This is the end of your program.

Dept of ECE, NMIT, Bangalore.53

Page 54: 8086 Programs

Girisha G K, Asst Professor

; 07:- PROGRAM 03

; FILE NAME: pcilogic.asm

; Program is tested and found OK

; //PROGRAM TO DEMONSTRATE INTERFACING OF LOGICAL CONTROLLER INTERFACE\\

Schematic diagram of a Logic Controller:

Dept of ECE, NMIT, Bangalore.54

Page 55: 8086 Programs

Girisha G K, Asst Professor

.MODEL TINY ;Specify the model for the executable. Must for every program.

.DATA ;Any data declarations here.

;; NOTE:;; The follwing data declerations are common for every program.;; The values of CR, PA, PB and PC will vary from PC to PC.;; The user need to modify the values of CR, PA, PB, PC;; for Assembling the program;;

CR EQU 0c003H PA EQU 0c000H PB EQU 0c001H PC EQU 0c002H

Message1 DB 'DEMONSTRATION PROGRAM FOR LOGIC CONTROLLER',13,10,'$' Message2 DB 'This program will read input in Port B. and outputs',13,10,'$' Message3 DB 'it's compliment in Port A.',13,10,13,10,'$' Message4 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$'.STACK.CODE ; Start your coding here. MOV AX,@DATA ; Initialize all segemnt regesters as needed here. MOV DS, AX

MOV AH, 9h ; Display the message line1. MOV DX, OFFSET Message1 INT 21h MOV AH, 9h ; Display the message line2. MOV DX, OFFSET Message2 INT 21h MOV AH, 9h ; Display the message line3. MOV DX, OFFSET Message3 INT 21h

MOV AH, 9h ; Display the message line4. MOV DX, OFFSET Message4 INT 21h

MOV AL, 82H ; Initialize A - Output B-Input Ports MOV DX, CR OUT DX, AL ; Write to Control Register.

GETKEY: MOV DX, PB ; Get Data from Register B IN AL, DX

Dept of ECE, NMIT, Bangalore.55

Page 56: 8086 Programs

Girisha G K, Asst Professor

NOT AL ; Compliment it.

MOV DX, PA ; Put Data to Register A OUT DX, AL

MOV AH, 01h INT 16h ;Get the Key JZ GETKEY

MOV AH, 4ch ; Exit the program safely. INT 21hEND ;This is the end of your program.

Dept of ECE, NMIT, Bangalore.56

Page 57: 8086 Programs

Girisha G K, Asst Professor

; 07:- PROGRAM 04

; FILE NAME: pcistep.asm

; Program is tested and found OK

; //PROGRAM TO DEMONSTRATE INTERFACING OF STEPPER MOTOR INTERFACE\\

Schematic Diagram of a Stepper Motor Interface:

Dept of ECE, NMIT, Bangalore.57

Page 58: 8086 Programs

Girisha G K, Asst Professor

.MODEL TINY ;Specify the model for the executable. Must for every program.

.STACK ;100h

.DATA ;Any data declarations here.;; NOTE:;; The follwing data declerations are common for every program.;; The values of CR, PA, PB and PC will vary from PC to PC.;; The user need to modify the values of CR, PA, PB, PC;; for Assembling the program

CR EQU 0c403H PA EQU 0c400H PB EQU 0c401H PC EQU 0c402H

Message1 DB 'DEMONSTRATION PROGRAM FOR STEPPER MOTOR',13,10,'$' Message2 DB 'User following Keys for operating.',13,10,9,9,'C - Clockwise.',13,10,9,9,'A - Anti-Clockwise.','$' Message3 DB 13,10,'This program is running...',13,10,'Press ESC key to EXIT.',13,10,'$'

Speed DW 0FFH

.CODE ;Start your coding here.

CALL CLRSCR ;Clear Screen

MOV AX,@DATA ;Initialize all segemnt regesters as needed here. MOV DS,AX

MOV AH, 9h ; Display the message line1. MOV DX, OFFSET Message1 INT 21h MOV AH, 9h ; Display the message line2. MOV DX, OFFSET Message2 INT 21h MOV AH, 9h ; Display the message line3. MOV DX, OFFSET Message3 INT 21h

MOV DX, CR MOV AL, 80h OUT DX, AL

GETKEY: MOV BL, AL MOV AH, 01h INT 16h ; Get the Key ZF = 1 (No Key), ZF = 0 (Key)

Dept of ECE, NMIT, Bangalore.58

Page 59: 8086 Programs

Girisha G K, Asst Professor

MOV AL, BL JZ COMPARE0 ; If No key pressed, ; Key was pressed, then get the key MOV AH, 00h INT 16h

MOV BL, AL SUB BL, 1BH ; 27 -Esc Key JZ EXIT_PROGRAM

COMPARE0: CMP AL,'C' ; For Clockwise motion JNZ COMPARE1 CALL CLOCK_WISE JMP END_COMPARECOMPARE1: CMP AL,'A' ;For Anti-Clockwise motion JNZ COMPARE2 CALL ANTI_CLOCK_WISE JMP END_COMPARE

COMPARE2: CMP AL,'c' ;For Anti-Clockwise motion JNZ COMPARE3 CALL CLOCK_WISE JMP END_COMPARE

COMPARE3: CMP AL,'a' ;For Anti-Clockwise motion JNZ END_COMPARE CALL ANTI_CLOCK_WISE JMP END_COMPAREEND_COMPARE: JMP GETKEY ; If No valid key pressed,

EXIT_PROGRAM: MOV AH, 4ch ; Exit the program safely. INT 21h

; ********************************************************************* ;; Procedures used by this program ;; ********************************************************************* ;CLRSCR PROC ;Clears Display

PUSH AX MOV AH,0Fh ;Get current mode

Dept of ECE, NMIT, Bangalore.59

Page 60: 8086 Programs

Girisha G K, Asst Professor

INT 10h MOV AH,00h ;Set to current mode INT 10h

POP AX RETCLRSCR ENDP

ANTI_CLOCK_WISE PROC PUSH AX MOV AL, 11h CALL OUT_A CALL DELAY

MOV AL, 22h CALL OUT_A CALL DELAY

MOV AL, 44h CALL OUT_A CALL DELAY

MOV AL, 88h CALL OUT_A CALL DELAY POP AX RETANTI_CLOCK_WISE ENDP

CLOCK_WISE PROC PUSH AX MOV AL, 88h CALL OUT_A CALL DELAY

MOV AL,44h CALL OUT_A CALL DELAY

MOV AL,22h CALL OUT_A CALL DELAY

MOV AL,11h CALL OUT_A CALL DELAY POP AX RET

Dept of ECE, NMIT, Bangalore.60

Page 61: 8086 Programs

Girisha G K, Asst Professor

CLOCK_WISE ENDP

OUT_A PROC MOV DX,PA OUT DX,AL RETOUT_A ENDP

DELAY PROC MOV CX,01FFFhLOOP1: MOV AX,SpeedLOOP2: DEC AX JNZ LOOP2 LOOP LOOP1 RETDELAY ENDP

END ;This is the end of your program.

Dept of ECE, NMIT, Bangalore.61