You are on page 1of 83

R.N.S.

INSTITUTE OF TECHNOLOGY
Channasandra, Bangalore-560061
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

MICROPROCESSOR LAB MANUAL 06ECL-68

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

MICROPROCESSOR LAB
Dept. of E&C, RNSIT 1

CONTENTS 1. Program to add two 8-bit numbers. 2. Program to subtract two 8-bit numbers. 3. Program to add two 16-bit numbers 4. Program to subtract two 16-bit numbers 5. Program to generate first 10 even numbers 6. Program to generate first 10 odd numbers 7. Program to multiply two 16-bit numbers 8. Program to divide a 16-bit number by a 8-bit number 9. Program to transfer a block of data from source to destination without
overlap. 10. Program to transfer a block of data from source to destination with overlap. 11. Program to check if a given 8-bit number belongs to the 2 out of 5 code. 12. Program to count number of 1s and 0s in a given 8-bit number. 13. Program to check if the given number is odd or even
Program to check if the given number is positive or negative Program to exchange 10 bytes of data Program to find LCM of two 16-bit numbers Program to find GCD/HCF of two 16-bit numbers Program to find the smallest and the largest number in the given array Program to sort an array in ascending or descending order using Bubble Sort . 20. Program to generate Fibonacci series 21. Program to search for an element using Linear Search 22. Program to find the second largest and second smallest number in an array 23. Program to multiply two 32-bit numbers 24. Program to find the factorial of a number 25. Program to find the average of N words. 26. Program to check if the given number is prime or not 27. Program to convert a given hexadecimal number to its BCD equivalent 28. Program to find the trace of a matrix 29. Program to convertBinary number to gray code
Dept. of E&C, RNSIT 2

14. 15. 16. 17. 18. 19.

30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47.

Program to convert Gray code to its Binary equivalent Program to convert BCD to Hexadecimal number Program to search for a number using Binary search. Program to find if a given number is palindrome or not. Program to multiply 2 matrices (Matrix Multiplication). Program to generate prime numbers Program to subtract two 8-bit BCD numbers Program to add two 8-bit BCD numbers. Program to transfer string from source to destination. Program to compare two strings Program to check if a given string is a bit-wise palindrome. Program to check whether the given data is nibble-wise palindrome Program to concatenate two strings Program to delete a string at the given position Program to insert a string at a given position Program to case reverse a given string Program to multiply 2 ASCII numbers Program to read and set system date & time

Interfacing programs 1. Keyboard Interfacing 2. Logic Controller Interfacing (i) Ring Counter (ii) BCD UP/DOWN Counter (iii) HEX UP/DOWN Counter (iv) JOHNSON Counter (v) Alternating LEDs (vi) Fighting LEDs 3. Stepper motor interface 4. Printer Interfacing 5. Seven-Segment Display Interfacing

1.

Program to add two 8-bit numbers.


Program: .model small

Dept. of E&C, RNSIT

.stack 64 .data num1 db 10h num2 db 20h .code mov ax,@data mov ds,ax mov al,num1 mov bl,num2 add al,bl result is stored in mov ah,4ch int 21h is given to DOS end Result:

; num1=10h ; num2=20h ; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AL register ; num2 is moved to BL register ; num1 and num2 are added and the AL register ; To terminate the process and control ; Directive to end the execution

i) num1=10h, num2 =20h The result of addition : AL=30h Flag status: NC (C=0), PE (P=1), NZ (Z=0), NA (A=0), PL (S=0), NV (O=0) ii) num1= 0FFh, num2= 20h The result of addition : AL=1Fh Flag status: CY (C=1), PO (P=0), NZ (Z=0), NA (A=0), PL (S=0), NV (O=0)

2. Program to subtract two 8-bit numbers


Program: .model small
Dept. of E&C, RNSIT 4

.stack 64 .data num1 db __h num2 db __h .code mov ax,@data mov ds,ax mov al,num1 mov bl,num2 add al,bl the result is

; num1=__h ; num2=__h ; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AL register ; num2 is moved to BL register ; num1 and num2 are subtracted and stored in AL register

mov ah,4ch int 21h is given to DOS end

; To terminate the process and control ; Directive to end the execution

Result: i) num1= 50h, num2 = 20h The result of addition : AL= 30h Flag status: NC (C=0), PE (P=1), NZ (Z=0), NA (A=0), PL (S=0), NV (O=0) ii) num1= 10h, num2= 20h The result of addition : AL= F0h Flag status: CY (C=1), PE (P=1), NZ (Z=0), NA (A=0), NG (S=1), NV (O=0)

3. Program to add two 16-bit numbers.


Flowchart:
Dept. of E&C, RNSIT 5

Program: .model small .stack 64 .data num1 db __h num2 db __h sum dw ? the result .code mov ax,@data mov ds,ax mov ax,num1 mov bx,num2 add ax,bx is mov sum,ax mov ah,4ch int 21h given to DOS end ; Directive to end the execution ; is moved to a location sum ; To terminate the process and control is ; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AL register ; num2 is moved to BL register ; num1 and num2 are added and the result

; num1=__h ; num2=__h ; a word size location is reserved for

Result: num1= 0FFFFh, num2= 0ABCDh The result of addition : sum= ABCCh Flag status: CY (C=1), PE (P=1), NZ (Z=0), AC (A=1), NG (S=1), NV (O=0)

4. Program to subtract two 16-bit numbers.


Program:
Dept. of E&C, RNSIT 6

.model small .stack 64 .data num1 db ____h num2 db ____h diff dw ? to store the

; num1=____h ; num2=____h ; a location (word size) is reserved result

.code mov ax,@data mov ds,ax mov ax,num1 mov bx,num2 sub ax,bx result mov sum,ax mov ah,4ch int 21h is given to DOS end Result:

; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AX register ; num2 is moved to BX register ; num2 is subtracted from num1 and the ; is moved to memory location diff ; To terminate the process and control ; Directive to end the execution

num1= 0ABCDh, num2 = 0FFFFh Sum= ABCEh Flag status: CY (C=1), PO (P=0), NZ (Z=0), AC (A=1), NG (S=1), NV (O=0)

Dept. of E&C, RNSIT

5. Program to generate first 10 even numbers.


Program: .model small .stack 64 .code mov ax,@data mov ds,ax mov si,2000h even numbers mov cx,0Ah mov al,00h up: mov [si],al location pointed add al,02h number inc si dec cx jnz up location up mov ah,4ch int 21h is given to DOS end Result: ds: 2000h 00 02 04 06 08 0A 0C 0E 10 12 ; Directive to end the execution ; To terminate the process and control ; SI is incremented ; CX is decremented ; If counter is not zero, jump to are stored ; CX is used as a counter ; First even number 00h is moved to Al ; The even numbers are moved to the by SI ; 02h is added to obtain the next even

; The 16 bit starting address of DS ; is moved to DS register ; Offset address is moved to SI where

Dept. of E&C, RNSIT

6. Program to generate first 10 odd numbers.


Program: .model small .stack 64 .code mov ax,@data mov ds,ax mov si,2000h even numbers mov cx,0Ah mov al,01h up: mov [si],al location pointed add al,02h number inc si dec cx jnz up location up mov ah,4ch int 21h is given to DOS end Result: ds: 2000h 01 03 05 07 09 0B 0D 0F 11 13 ; Directive to end the execution ; To terminate the process and control ; SI is incremented ; CX is decremented ; If counter is not zero, jump to are stored ; CX is used as a counter ; First even number 00h is moved to Al ; The even numbers are moved to the by SI ; 02h is added to obtain the next even

; The 16 bit starting address of DS ; is moved to DS register ; Offset address is moved to SI where

Dept. of E&C, RNSIT

7. Program to multiply two 16-bit numbers.


Flowchart: Program: .model small .stack 64 .data x dw ____h y dw ____h res dw ? store the result .code mov ax,@data mov ds,ax mov ax,x mov bx,y mul bx mov res,ax res and mov res+2,dx mov ah,4ch int 21h given to DOS end Result: i) x= 0FFFFh, y = 01111h res= 1110EEEFh Flag status: CY (C=1), OV (O=1)
Dept. of E&C, RNSIT 10

; x=____h ; y=____h ; a location (word size) is reserved to

; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AX register ; num2 is moved to BX register ; multiply both the numbers ; the Lbyte of product is moved to location ; Hbyte is moved to res+2 ; To terminate the process and control is ; Directive to end the execution

ii) x= 3333h, y = 2222h res= 06D392C6h Flag status: CY (C=1), OV (O=1)

8. Program to divide a 16-bit number by a 8-bit number.


Program: .model small .stack 64 .data x dw ____h y db __h res dw ? .code mov ax,@data mov ds,ax mov ax,x div byte ptr y mov res,ax remainder is present in DX register mov ah,4ch int 21h given to DOS end ; Directive to end the execution ; To terminate the process and control is

; x= ____h ; y= __h

; The 16 bit starting address of DS ; is moved to DS register ; num1 is moved to AL register ; Dividend is divided by divisor ; quotient is moved to a location res and

Result: i) x= 6F56h (dividend), y= 05h (divisor) Quotient: res= 1644h Remainder: DX= 02h ii) x= 0078h (dividend), y= 03h (divisor) Quotient: res= 0028h
Dept. of E&C, RNSIT 11

Remainder: DX= 00h

9. Program to transfer a block of data from source to destination without overlap


Program: .model small .stack 64 .code mov ax,@data mov ds,ax mov si,2000h SI mov di,3000h in DI mov cx,0Ah up: mov al, [si] moved to mov [di],al destination location inc si inc di loop up repeated till counter becomes zero mov ah,4ch int 21h is given to DOS end Result:
Dept. of E&C, RNSIT 12

; The 16 bit starting address of DS ; is moved to DS register ; Offset address of source in DS is moved to ; Offset address of destination in DS is stored ; CX is used as a counter ; The data present at location pointed by SI is AL register ; The content of AL is moved to ; Index registers DI and SI ; are incremented ; Counter is decremented and loop is

; To terminate the process and control ; End of program

(source) 88 (destination) 88

ds: 2000h

55

45

56

78

11

22

33

44

66

ds: 3000h

55

45

56

78

11

22

33

44

66

10. Program to transfer a block of data from source to destination with overlap.
Program: .model small .stack 64 .code mov ax,@data mov ds,ax mov si,2000h SI mov di,2005h in DI mov cx,0Ah dec si once dec di add si,cx to point to the add di,cx up: mov al, [si] mov [di],al location dec si decremented
Dept. of E&C, RNSIT 13

; The 16 bit starting address of DS ; is moved to DS register ; Offset address of source in DS is moved to ; Offset address of destination in DS is stored ; CX is used as a counter ; SI and DI registers are decremented

; The content of CX is added to SI and DI ; last data of source and destination arrays ; The source data is moved to AL register ; The data in AL is moved to destination ; Index registers DI and SI are

dec di loop up repeated till

; Counter is decremented and loop is counter becomes zero

mov ah,4ch int 21h is given to DOS end Result: (Before execution) 09 10

; To terminate the process and control ; End of program

ds: 2000h

01

02

03

04 xx

05 xx 05 07

06 xx 01 08

07 xx 02 09

08 xx 03 10

04

(After execution) 05

ds: 2000h

01

02

03

04 06

11. Program to check if a given 8-bit number belongs to the 2 out of 5 code.
Program: .model small .stack 64 .data num1 db __h res db 00h .code mov ax,@data mov ds,ax mov al,num1 and al,0E0h upper 3 bits cmp al, 0E0h jnz back mov cx,05h
Dept. of E&C, RNSIT

; num1= __h

; The 16 bit starting address of DS ; is moved to DS register ; Get the number into AL ; AND number with E0h to check for the ; compare it with E0h ; If not equal, go to back ; Use a counter for 5 bits
14

mov al,num1 mov bx,00h up: shr al,01h jnc down down inc bx down: loop up cmp bx,02h jnz back to back mov dl,01h code jmp exit back: mov dl,00h exit: mov res,dl mov ah,4ch int 21h is given to DOS end Result:

; Get the number into AL ; BX is used to count 1s ; Shift right the number by 1 bit ; If the bit is not equal to 1, jump to ; If the bit is equal to 1, increment ; Repeat the loop till count = 0 ; Compare the number of 1s with 02h ; If the number of 1s is not equal to 2, go ; Indication if the given data is a valid

; Indication if the given data is not a valid code

; To terminate the process and control ; End of program

i) num1 = E3h res = 01h (E3h is a valid 2 out of 5 code) ii) num1 = E7h res = 00h ( E7h is not a valid 2 out of 5 code)

12. Program to count number of 1s and 0s in a given 8-bit number. Program: .model small .stack 64 .data num1 db __h ; num1= __h res1 db ? ; res1 is to store the number of 1s res2 db ? ; res2 id to store the number of 0s .code mov ax,@data ; Address of DS is moved to DS register mov ds,ax mov al,num1 ; get the number to AL
Dept. of E&C, RNSIT 15

mov cx,08h mov bx,0000h up: shr al,01h jnc next next inc bl BL jmp disp next: inc bh BH disp: loop up mov res1,bl res1 mov res2,bh res2 mov ah,4ch given int 21h end

; initialize a counter ; shift the number once ; If bit not equal to 1 then go to ; if bit equal to 1 then increment ; go to disp ; if bit not equal to 1 then increment ; Repeat the loop till count = 0 ; Store the number of 1s in location ; Store the number of 0s in location ; Terminate the process and control is ; to DOS ; End of program

Result: i)

num1 = 54h res1= 03h (number of 1s) res2= 05h (number of 0s) num2 = 67h res1= 05h (number of 1s) res2= 03h (number of 0s)

ii)

13. Program to check if the given number is odd or even. Program: .model small .stack 64 .data num db __h
Dept. of E&C, RNSIT

; num= __h
16

res db ? .code mov ax,@data to DS register mov ds,ax mov al,num shr al,01h the carry flag jnc down mov bl,01h (no. is odd) jmp exit down: mov bl,00h exit: mov res,bl mov ah,4ch given int 21h end

; res is to store the number of 1s ; Address of Data Segment is moved

; Get the number to AL ; Shift the number once and check ; If bit not equal to 1, go to down ; If bit equal to 1, store 01h in BL ; Jump to exit ; Store 00h in BL if the number is even ; mov 00 or 01 to location res ; Terminate the process and control is ; to DOS ; End of program

Result: iii)

num1 = 0Bh res= 01h (the number is odd) num2 = 06h res= 00h (the number is even)

iv)

14. Program to check if the given number is positive or negative. Program: .model small .stack 64 .data num db __h res db ? .code mov ax,@data to DS register
Dept. of E&C, RNSIT

; num= __h ; res is to store the number of 1s ; Address of Data Segment is moved
17

mov ds,ax mov al,num shl al,01h the carry flag jnc down mov bl,0FFh (no. is odd) jmp exit down: mov bl,00h exit: mov res,bl mov ah,4ch given int 21h end

; Get the number to AL ; Shift the number once and check ; If bit not equal to 1, go to down ; If bit equal to 1, store 01h in BL ; Jump to exit ; Store 00h in BL if the number is even ; mov 00 or 01 to location res ; Terminate the process and control is ; to DOS ; End of program

Result: v)

num1 = 06h res= 00h (the number is positive) num2 = 0A6h res= FFh (the number is negative)

vi)

15. Program to exchange 10 bytes of data. Flowchart: Program: .model small .stack 64 .data .code
Dept. of E&C, RNSIT 18

mov ax,@data register mov ds,ax mov si,2000h mov di,3000h bytes mov cx,0Ah up: mov al,[si] AL which xchg [di],al array2 mov [si],al from AL inc si inc di loop up mov ah,4ch control is given int 21h end

; Address of DS is moved to DS ; Starting address of 1st set of 10 bytes ; Starting address of 2nd set of 10 ; Counter for 10 bytes ; The number of array1 is moved to ; is exchanged with number of ; This number is moved to array1 ; Increment the address in SI ; Increment the address in DI ; Repeat till count becomes zero ; Terminating the process and ; to DOS ; End of program

Result: Before Execution: ds: 2000 00 01 ds: 3000 10 11 After Execution: ds: 2000 10 11 ds: 3000 00 01 02 12 03 13 04 14 05 15 06 16 07 17 08 18 09 19

12 02

13 03

14 04

15 05

16 06

17 07

18 08

19 09

16. Program to find LCM of two 16-bit numbers. Flowchart:

Dept. of E&C, RNSIT

19

Program: .model small .stack 64 .data num dw 0025h,0010h lcm dw 2dup(?) .code mov ax,@data register mov ds,ax mov ax,num mov bx,num+2 mov dx,00h up: push ax use push dx div bx cmp dx,00h for 0 jz down down pop dx content to DX and pop ax add ax,num jnc below DX register by 01h inc dx below: jmp up down: pop lcm+2 num+2 is 0, pop lcm mov ah,4ch given int 21h end ; if remainder is not 0, pop stack ; AX ; add AX and num ; if carry is generated, increment ; if remainder is 0 then go to ; num is divided by num+2 ; the remainder of division is checked ; num is moved to AX ; num+2 is moved to BX ; DX is made 00h for 16-bit division ; AX and DX are pushed for later

; num = 0025h and num+2 = 0010h ; 2 data words are reserved for LCM ; address of DS is moved to DS

; jump to up and repeat ; if the remainder of division of num by ; pop the stack contents ; terminate the process and control is ; to DOS ; end of program

Result: i) num = 0025h, num+2 = 0010h


20 Dept. of E&C, RNSIT

lcm +2 : 0000h lcm : 0250h ii) num = 000Ah, num+2 = 0005h lcm+2 : 0000h lcm : 000Ah

17. Program to find GCD/HCF of two 16-bit numbers. Program: .model small .stack 64 .data num dw 0025h,0010h gcd dw ? .code mov ax,@data register mov ds,ax mov ax,num mov bx,num+2 back: cmp ax,bx je exit jb down up: mov dx,00h div bx cmp dx,00h je exit mov ax,dx move it to AX jmp back down: xchg ax,bx contents and jmp up exit: mov gcd,bx mov ah,4ch is given int 21h end
Dept. of E&C, RNSIT

; num = 0025h, num+2 = 0010h

; Address of DS is moved to DS

; 0025h is moved to AX ; num+2 is moved to BX ; compare contents of AX and BX ; if AX = BX, got o exit ; if AX < BX, go to down ; make DX = 00h ; divide contents of AX by BX ; if remainder = 00h, go to exit ; if remainder is not equal to 0, ; and go to back ; when AX < BX, exchange their ; go to up ; when remainder = 0, Bx is the GCD ; terminate the process and control ; to DOS ; end of program
21

Result: i) num = 0025h, num+2 = 0010h gcd : 0001h num = 0019h, num+2 = 0046h gcd : 0005h

ii)

18. Program to find the smallest and the largest number in the given array. Program: .model small .stack 64 .data .code mov ax,@data register mov ds,ax mov si,2000h mov cx,05h mov al,[si] to AL back: inc si cmp al,[si] number jc down otherwise mov al,[si] down: loop back mov ah,4ch given int 21h end
Dept. of E&C, RNSIT

; Address of DS is moved to DS

; Starting address of array ; Counter of 5 for 5 comparisons ; The first number of array is moved ; increment SI pointer ; compare the number with the next ; if 2nd no. > 1st no., go to down ; move the 2nd number to AL ; Repeat till count becomes zero ; terminate the process and control is ; to DOS ; End of program
22

Result: ds : 2000 56 78 12 67 45 90

Smallest number : AL = 12h Largest number : AL = 90h

19. Program to sort an array in ascending or descending order using Bubble sort Program: .model small .stack 64 .data num db 05,03,04,00,01 num1 dw $-num .code mov ax,@data register mov ds,ax mov bx,num1 dec bx moved to CX outloop: mov cx,bx mov si,0000h inloop: mov al,num[si] inc si cmp al,num[si] jb down (ja down) xchg al,num[si] mov num[si-1],al down: loop inloop dec bx
Dept. of E&C, RNSIT

; the array to be sorted is num ; num1 = no. of elements in array ; address of DS is moved to DS

; num1 is moved to BX ; BX is decremented and ; CX is used as comparison counter ; 1st number of array is moved to AL and ; compared with next number ; if 1st no.<next no., jump to down ; else exchange the numbers ; repeat loop till CX = 0 ; decrement the pass counter
23

jnz outloop zero mov ah,4ch control is given int 21h end
Result: Ascending order: ds: 000E Descending order: ds: 000E

; repeat loop till pass counter is ; terminate the process and ; to DOS ; end of program

00 01 03 04 05 05 04 03 01 00

20. Program to generate Fibonacci series. Flowchart: Program: .model small .stack 64 .data .code mov ax,@data register mov ds,ax mov si,2000h mov [si],00h inc si mov cx,07h 10 nos. mov al,01h mov [si],al mov [si+1],al up: inc si add al,[si-1] previous no. and da a
Dept. of E&C, RNSIT

; Address of DS is moved to DS

; SI is a pointer to store the numbers ; 00h is stored as the first number ; counter of 07h is used to generate ; 01h is stored as 2nd number ; 01h is stored as the 3rd number ; SI is incremented ; the current no. is added to ; the result is decimal adjusted
24

and mov [si+1],al SI


loop up

; stored in the location pointed by ; terminate the process and control is ; to DOS ; end of program

mov ah,4ch given int 21h end

Result: ds: 2000 00 01 01 02 03 05 08 13 21 34

21.Program to search for an element using Linear Seach Program: .model small .stack 64 .data num db 05h,04h,03h,02h,01h ; array of numbers loc dw (?) ; loc is defined to store location of search element .code mov ax,@data ; address of DS is moved to DS register mov ds,ax mov al,XXh ; search element is stored in AL mov cx,05h ; CX is used as a counter mov si,0000h ; SI points to first element of array and up: cmp al,num[si] ; is compared with the search element
Dept. of E&C, RNSIT 25

je exit inc si loop up mov bl,00h equal to any jmp disp display 00h in BL, exit: mov loc,si mov bl,0FFh SI to loc disp: mov ah,4ch control is given int 21h end Result: i)

; if equal, jump to exit ; else, increment si and ; repeat loop till CX = 0 ; if search element is not ; element of the array, ; indicating no. not found ; display FFh in BL and move if no. is found ; terminate the process and ; to DOS ; end of program

AL = 03h (search element) BL = FFh (number found) loc = 0002h ii) AL = 0Ah (search element) BL = 00h (number not found)

22.Program to find the second largest and second smallest number in an array. Program: .model small .stack 64 .data .code mov ax,@data register mov ds,ax mov si,2000h mov cx,04h N-1comparisons
Dept. of E&C, RNSIT

; address of DS is moved to DS

; SI points to the array ; CX is used as a counter for


26

mov al,[si] back: inc si cmp al,[si] jc down (jnc down) mov al,[si] down: loop back mov bl,al loc: inc al (dec al) mov si,2000h mov cx,05h up: cmp al,[si] jz below jump to below inc si loop up jmp loc below: mov ah,4ch int 21h end

; get the first number to AL ; compare with the next number ; if 1 no. < 2nd no., jump to down ; otherwise move 2nd no. to AL
st

; store the smallest no. in BL ; increment the content of AL ; reinitialize pointer ; set counter for N ; compare AL with array elements ; if AL = any array element, ; otherwise increment SI ; repeat comparisons ; terminate the process and control is ; given to DOS ; end of program

Result: i) ds:2000 45 55 BL = 20h (smallest no.) AL = 45h (2nd smallest no.) ii) ds:2000 11 BL = 99h (largest no.) AL = 77h (2nd largest no.) 44 99 20 65

77

99

33

23.Program to multiply two 32-bit numbers. Program: .model small .stack 64 . data num1 dd ____h
Dept. of E&C, RNSIT

; num1 is a 32-bit number


27

num2 dd ____h prod dw 4 dup(0) product .code mov ax,@data mov ds,ax mov ax,num1 and mul word ptr num2 mov prod[0],ax mov prod[2],dx mov ax,num1+2 mul word ptr num2 add prod[2],ax product

; num2 is a 32-bit number ; 4 16-bit location are reserved to store

; address of DS is moved to DS register ; lower word of num1 is moved to AX ; multiplied with lower word of num2 ; product is stored in prod and prod+2 ; higher word of num1 is moved to AX and ; multiplied with lower word of num2 ; the product is added to the previous

adc prod[4],dx adc prod[6],00h mov ax,num1 ; lower word is moved to AX and mul word ptr num2+2 ; multiplied with the higher word of num2 add prod[2],ax ; the product is added to the previous product adc prod[4],dx adc prod[6],00h mov ax,num1+2 ; higher word of num1 is moved to AX and mul word ptr num2+2 ; multiplied with higher word of num2 add prod[4],ax ; the product is added to the previous products adc prod[6],dx mov ah,4ch control is int 21h end Result: i) num1 = 11111111h, num2 = 22222222h prod: 42 86 CA 0E CF 8A 46 (The product is 02468ACF0ECA8642h) ii) num1 = 22222222h, num2 = 33333333h prod: C6 92 5F 2C 6D A0 D3 (The product is 06D3A06D2C5F92C6h) ; given to DOS ; end of program ; terminate the process and

02

06
28

Dept. of E&C, RNSIT

24.Program to find the factorial of a number. Program: .model small .stack 64 .data .code mov ax,@data mov ds,ax mov bx,01h mov ax,01h mov cx,__h found) up: mul bx inc bx loop up mov ah,4ch int 21h end

; address of DS is moved to DS register ; move 01h to BX and AX ; CX = __h (number whose factorial has to be ; multiply AX with BX ; increment BX ; repeat loop till CX = 0 ; terminate the process and control is ; given to DOS ; end of program

Result: i)

CX = 07h BX = 13B0h, DX = 0000h Factorial of 07h is 000013B0h CX = 09h


29

ii)

Dept. of E&C, RNSIT

BX = 8980h, DX = 0005h Factorial of 09h is 00058980h

25.Program to find the average of N words. Flowchart: Program: .model small .stack 64 . data .code mov ax,@data ; address of DS is moved to DS register mov ds,ax mov si,2000h ; SI points to the 5 words mov ax,word ptr [si] ; first word is moved to AX mov cx,04h ; counter is initialized to 04h mov dx,00h back: inc si ; increment SI to point to next word inc si add ax,word ptr [si] ; add the words jnc down ; if CY=1, increment DX, else go to down inc dx down: loop back ; repeat the loop till CX=0 mov bx,05h div bx ; divide the sum of the words by 05h mov bx,ax ; store the average mov ah,4ch ; terminate the process and control is int 21h ; given to DOS end ; end of program

Result:
Dept. of E&C, RNSIT 30

ds: 2000

11

00

56

00

34

00

90

00

01

00

DX = 0000h, BX = 003Ch

26.Program to check if the given number is prime or not Program: .model small .stack 64 . data num dw 07h .code mov ax,@data mov ds,ax mov bh,02h mov ax,num div bh mov bl,al up: mov ax,num div bh cmp ah,00h je down inc bh cmp bl,bh jb up mov al,0FFh jmp below down: mov al,00h below: mov ah,4ch int 21h end

; num is the number to be checked ; address of DS is moved to DS register ; store the data 02h in BH ; copy the number to AX ; divide the number by 02h ; move the quotient to BL ; the number is again moved to AX and ; divide by BH ; remainder is checked for zero ; if 0, go to down ; BH is incremented ; BL and BH are compared ; if BH<BL, jump to up ; if number is prime, store FFh in AX ; if number is not prime, store 00h in AX ; terminate the process and control is ; given to DOS ; end of program

Result: i)

num = 07h AL = 0FFh ( num is prime)


31

Dept. of E&C, RNSIT

ii)

num = 08h AL = 00h (num is not prime)

25.Program to convert a given hexadecimal number to its BCD equivalent. Program: .model small .stack 64 .data .code mov ax,@data mov ds,ax mov si,2000h mov ax,[si] mov dx,00h mov bx,03E8h div bx mov cl,04h shl al,cl exchanged inc si inc si mov [si],al location mov ax,dx mov bl,64h div bl or [si],al quotient mov al, ah mov ah,00h mov bl,0Ah div bl mov cl,04h shl al,cl nibble or al,ah
Dept. of E&C, RNSIT 32

; address of DS is moved to DS register ; SI points to the given hex number ; the hex number is copied to AX ; store 1000d in BX ; the number is divided by 1000d ; the nibble positions of AL are

; the data in AL is being moved to the provided by VTU ; the remainder is moved to AX and ; is divided by 64h ; the quotient is ORed with previous ; the remainder is moved to AL ; store 0Ah in BL ; divide the contents of AL ; initialize a counter to 4 ; shift t data left 4 times to get t higher

inc si move [si],al mov ah,4ch int 21h end Programs:

; terminate the process and control is ; given to DOS ; end of program

i) ds: 2000: F4h 11h (given hex number is 11F4h) ds: 2002: 45h 96h ii) ds:2000 : ds:2002: 9Ah 1Ah (given number is 9 Ah 1AH) 68h 10h (BCD equivalent is 6810)

26.Program to find the trace of a matrix. Program: .model small .stack .data n dw 03h trace db 00h .code mov ax,@data mov ds,ax mov cx,n mov si,2000h mov al,00h inc n up: add al,[si] add si,n diagonal loop up mov trace,al mov ah,4ch int 21h end
Dept. of E&C, RNSIT

; address of DS is moved to DS register ; the order of the matrix is used as a counter ; SI points to the first element of the matrix ; the 1st element is added to AL ; SI is made to point to the next element of the ; loop is repeated till CX=0 ; the result is stored in trace ; terminate the process and control is ; given to DOS ; end of program
33

Result: i) 05 04 05 06 07 09 02 07 08 09 04 06 08 trace = 0Fh Flowchart: trace = 12h ds: 2000 01 02 03 ii) ds: 2000 01 03

27.Program to convertBinary number to gray code. Program: .model small .stack .data n db __h res db 00h .code mov ax,@data mov ds,ax mov al,n shr al,01h xor al,n mov res,al mov ah,4ch int 21h end Result: i)

; given bbinary number ; equivalent gary code is tored in res ; address of DS is moved to DS register ; get the no. into AL register ; shift the no. once to the right ; XOR the shifted no. with the given no. ; mov the gray code to res ; terminate the process and control is ; given to DOS ; end of program

n = 08h res = 0Ch (Gray code) n = 04h res = 06h (Gray code)
34

ii)

Dept. of E&C, RNSIT

28.Program to convert Gray code to its Binary equivalent. Program: .model small .stack .data n db __h res db 00h . code mov ax,@data mov ds,ax mov cx,07h mov al,n back: shr al,01h xor al,n loop back mov res,al mov ah,4ch int 21h end

; given gray code number ; equivalent binary number is stored in res ; address of DS is moved to DS register ; counter for 07h is used ; move the given gray code to AL ; shift the number once to the right ; XOR the shifted no. with the given no. ; loop till CX=0 ; store the binary no. in res ; terminate the process and control is ; given to DOS ; end of program

Result: i)

n = 0Ch (given Gray code) res = 08h (Binary equivalent)

ii)

n = 06h res = 04h (Binary equivalent)

29.Program to convert BCD to Hexadecimal number.


Dept. of E&C, RNSIT 35

Program: .model small .stack .data . code mov ax,@data mov ds,ax mov si,2000h mov ax,[si] mov bx,ax and ax,0F000h mov cl,04h rol ax,cl mov cx,03E8h mul cx inc si inc si mov [si],ax mov ax,bx and ax,0F00h xchg ah,al mov cl,64h mul cl add [si],ax mov ax,bx and al,0F0h mov cl,04h rol al,cl mov cl,0Ah mul cl add [si],ax mov ax,bx and ax,000Fh add [si],ax mov ah,4ch int 21h end Result: i) ds: 2000h
Dept. of E&C, RNSIT

; address of DS is moved to DS register ; SI points to the BCD no. ; load the BCD no. in AX ; copy the no. to BX ; mask the 3 LS digits to get the MS digit ; rotate the no. to the left by 4 bits ; multiply the MS digit with 1000d (03E8h)

; store the product in the location pointed by SI ; load the no. again in AX ; obtain the 2nd MS digit by masking the others ; exchange the upper and lower bytes of AX ; multiply the no. with 100d (64h) ; add the product to the previous result ; load the no. again in AX ; obtain the 3rd digit by masking others ; rotate the no. left by 4 bits ; multiply the 3rd digit with 10d (0Ah) ; add the product to the previous result ; load the no. again in AX ; mask the no. to obtain the LS bit ; add the LS digit to the previous result ; terminate the process and control is ; given to DOS ; end of program

96

45

(the BCD number is 4596)


36

ds: 2002h ii) ds: 2000h ds: 2002h

11

F4

(the HEX equivalent is 11F4h)

10 68 (the BCD number is 6810) 1A 9A (the HEX equivalent is 1A9Ah

30.Program to search for a number using Binary search. Program: .model small .stack .data array dw 0001h, 0002h, 0003h, 0004h, 0005h ; array of elements N dw ($-array)/2 ; number of elements srch dw 0004h ; search element res dw 0FFFFh ; res = 0FFFFh, i.e. number not found pos dw 0000h ; pos indicates the position of elements mid dw 00h . code mov ax,@data ; address of DS is moved to DS register mov ds,ax mov bx, 00h ; BX points to the 1st element mov dx,N ; DX has the number of elements mov cx,srch ; search element is moved to CX register back: cmp bx,dx ; compare BX and DX ja nfound ; if BX>DX, jump to nfound mov mid,bx ; BX is moved to mid add mid,dx ; mid value is added to data in DX shr mid,01h ; obtain the mid element by dividing it by 2 mov si,mid add si,si ; add the mid value to SI cmp cx,array[si] ; compare each element with the mid element je success ; if equal, jump to success ja bigger ; if search element is greater, jump to bigger dec mid mov dx,mid ; else move mid to DX jmp back ; jump to back bigger: inc mid ; increment mid mov bx,mid ; move the mid position to BX
Dept. of E&C, RNSIT 37

jmp back success: mov res,00h inc mid mov ax,mid mov pos,ax nfound: mov ah,4ch int 21h end

; jump to back ; if element is found, display res = 00h

; move the position of the element to pos ; terminate the process and control is ; given to DOS ; end of program

Result: array: 0001h, 0002h, 0003h, 0004h, 0005h search element (srch) = 0004h res = 0000 (element found) pos = 0004h

33. Program to find if a given number is palindrome or not. Program: .model small .stack .data num dw ____h res db 00h palindrome or not . code mov ax,@data mov ds,ax mov ax,num mov bx,00h mov cx,10h back: rol ax,01h rcr bx,01h loop back cmp bx,num je exit mov res,00h jmp disp
Dept. of E&C, RNSIT

; num = ____h ; res indicates whether the given no. is

; address of DS is moved to DS register ; num is moved to AX register ; BX is used to store reversed no. ; initialize counter to 10h (16d) ; rotate original no. by 1 bit ; rotate BX to the right through carry flag ; repeat till counter becomes 0 ; compare reversed no. with original no. ; if equal, jump to exit ; indication if the number is not a palindrome
38

exit: mov res,0FFh disp: mov ah,4ch int 21h end

; indication if the given no. is a palindrome ; terminate the process and control is ; given to DOS ; end of program

Result: 43. num = 8001h res = 0FFh (8001h is a palindrome) 43. num = 4552h res = 00h (4552h is not a palindrome)

34.Program to multiply 2 matrices (Matrix Multiplication). Program: .model small .stack .data r1 db 1, 2, 3, 4h ; row1 of matrix1 r2 db 5, 6, 7, 8h ; row2 of matrix1 r3 db 1, 2, 3, 4h ; row3 of matrix1 c1 db 1, 2, 3, 4h ; column1 of matrix2 c2 db 5, 6, 7, 8h ; column2 of matrix2 L1 db 00 ; columns of product matrix = L1 L2 db 00 ; rows of product matrix = L2 res dw 6dup(0) . code mov ax,@data ; address of DS is moved to DS register mov ds,ax mov si,offset r1 ; offset of row1 is moved to SI mov L2,03h ; L2 = 03h (rows of result matrix) mov bp,00h rep: mov di,offset c1 ; offset of column1 is moved to DI mov L1,02h ; L1 =02h (columns of result matrix) up: call matmul ; call procedure matmul mov ds: res[bp],dx ; store the result add bp,02h ; increment BP by 02h
Dept. of E&C, RNSIT 39

add di,04h dec L1 jnz up add si,04h dec L2 jnz rep mov ah,4ch int 21h matmul proc mov bx,00h mov dx,00h mov cx,04h products back: mov al,[si][bx] mul byte ptr [di][bx] corresponding add dx,ax inc bx loop back ret matmul endp end

; add 04h to DI to point to next column ; decrement L1 ; loop to up ; add 04h to SI to point to the next row ; decrement L2 ; loop to rep ; terminate the process and control is ; given to DOS

; DX is used to store partial products ; counter of 04h is used for partial

; multiply the element of row with its column ; add all the partial products

; return to the main program ; end of program

Result: The given matrices are 1 2 3 4 5 6 7 8 1 2 3 4 1 5 2 6 3 7 4 8 1E 00 46 00 46 00 1E 00


Dept. of E&C, RNSIT 40

and

The product is 00

ds: 0022h

AE 46

00

i.e. the product is

1E

46 46 1E AE 46

35.Program to generate prime numbers. Program: .model small .stack .data n dw 0Ah n1 dw 03h . code mov ax,@data mov ds,ax mov si,2000h stored mov cx,n up1: mov ax,n1 mov bl,02h div bl mov bh,al up: mov ax,n1 div bl cmp ah,00h jz down inc bl cmp bl,bh jna up mov dx,n1 mov [si],dx inc si
Dept. of E&C, RNSIT

; n denotes the number of prime numbers ; n1 is the first prime number ; address of DS is moved to DS register ; SI points to the location where prime nos. are ; CX is used as a counter

; divide n1 by 02h ; store the quotient in BH ; load the number n1 into AX ; divide the no. by BL ; check if the quotient is 0 ; if equal, jump to down ; if unequal, increment BL ; check if BL>BH

41

inc n1 loop up1 jmp below down: inc n1 jmp up1 below: mov ah,4ch int 21h end

; increment n1 to check next number ; loop to up1 ; if number is not prime, increment n1 ; jump to up1 ; terminate the process and control is ; given to DOS ; end of program

Result: ds: 2000h 03 05 07 0B 0D 11 13 17 1D 1F

36.

Program to subtract two 8-bit BCD numbers.

Program: .model small .stack .data . code mov ax,@data mov ds,ax mov si,2000h mov al,[si] sub al,[si+1] das jc down mov [si+2],00h jmp exit
Dept. of E&C, RNSIT

; address of DS is moved to DS register ; SI points to the location on the numbers ; load the first no. into AL ; subtract it from second no. ; decimal adjust AL reg ; if cy=1, jump to down ; if cy=0, move 00h at si+2
42

down: mov ah,99h sub ah,al xchg ah,al add al,01h daa mov [si+2],01h exit: mov [si+3],al mov ah,4ch int 21h end

; if cy=1, subtract AL reg from 100h

; store 00h in SI+2 to indicate negative result ; store the AL content which is the difference ; terminate the process and control is ; given to DOS ; end of program

Result: i) Before Execution: ds:2000 After Execution: ds:2000 45 35 45 35 00 10

ii) Before Execution: ds:2000 After Execution: ds:2000

53 53

73 73

01

20

37.

Program to add two 8-bit BCD numbers.

Flowchart:

Program: .model small .stack .data .code mov ax,@data register mov ds,ax mov si,2000h mov al,[si] add al,[si+1] daa jc down generated
Dept. of E&C, RNSIT

; address of DS is moved to DS

; SI points to the location of nos. ; add the two BCD nos. ; decimal adjust the result ; jump to down if carry is
43

mov byte ptr [si+2],00h carry jmp exit down: mov byte ptr [si+2],01h exit: mov [si+3],al mov ah,4ch int 21h end

; display ooh at SI+2, to indicate no

; display 01h at SI+2 to indicate carry ; store the result in SI+3

; terminate the program

Result: i) ds:2000 ds:2000 ii) ds:2000 ds:2000 45 35 45 35 75 40 75 40 00 80 (sum = 80)

01

15 (sum = 115)

38.

Program to transfer string from source to destination.

Flowchart:

Program: .model small .stack .data str1 db indis$ str2 db 6dup($) .code mov ax,@data ES registers mov ds,ax mov es,ax mov cx,06h
Dept. of E&C, RNSIT

; str1 = india$ ; 6 locations are reserved for str2 ; same addresss is moved to DS and

; counter is initialized to 06
44

lea si,str1 lea di,str2 cld rep mov sb becomes zero lea dx,str2 mov ah,09h int 21h mov ah,4ch is int 21h end

; address of str1 is moved to SI ; address of str2 is moved to DI ; D flag is cleared ; str1 is transferred to str2 until cx

; to output str2 on DOS screen ; terminate the process and control ; given to DOS ; end of program

Result: Before Execution: str1 = india$ str2 = $$$$$$

After Execution: str1 = india$ str2 = India$

39.

Program to compare two strings.

Program: .model small .stack .data str1 db ____ str2 db ____ m1 db strings are equal$ m2 db strings are not equal$ .code mov ax,@data to DS and mov ds,ax mov es,ax mov cx,05h the string lea si,str1 SI register
Dept. of E&C, RNSIT

; str1= india ; str2= india ; message to display if strings are equal ; message to display if strings are not equal ; address of DS and ES is moved ; ES registers ; counter for no.of characters in ; effective address of str1 is in
45

lea di,str2 DI register cld to auto-increment repe cmpsb equal je down are equal down: lea dx,m2 not equal exit: mov ah,09h int 21h mov ah,4ch int 21h end Result: i)

; effective address of str2 is in ; clear D flag for SI and DI ; repeat if the 2 strings are ; go to down if the strings ; if not equal, display strings are

; to display the message

; terminate the program

str1 = india str2 = india strings are equal (on DOS screen)

ii)

str1 = pramod str2 = ppamod strings are not equal (on DOS screen) Program to check if a given string is a bit-wise palindrome.

40.

Program: .model small .stack 64 .data str1 db aba str2 db 4dup$ m1 db 0dh,0ah, string is a palindrome$ m2 db 0dh,0ah, string is not a palindrome$ m3 db 0dh,0ah, reversed string is$ .code mov ax,@data ; address of DS and ES is moved to DS and
Dept. of E&C, RNSIT 46

mov ds,ax mov es,ax mov cx,03h in the string lea si,str1 lea di,str2 dec cx add si,cx character of inc cx up: mov al,[si] mov [di],al inc di dec si loop up lea dx,m3 call disp lea dx,str2 call disp lea si,str1 lea di,str2 mov cx,03h cld repe cmpsb je down lea dx,m2 palindrome call disp jmp exit down: lea dx,m1 call disp palindrome int 21h disp proc mov ah,09h int 21h ret disp endp end
Dept. of E&C, RNSIT

; ES registers ; counter of the no. of characters ; Si points to the given string ; DI points to the reversed string ; SI is made to point to the last ; the given string ; reverse the string

; display the reversed string

; SI points to the given string ; DI points to reversed string

; check if the strings are equal ; if the strings are equal then, ; display string is a

; if the strings are not equal then, ; display string is not a

; procedure to display strings

; end
47

Result: i) Given string is aba Reversed string is aba String is a palindrome ii) Given string is apple Reversed string is elppa String is not a palindrome

41. Program to check whether the given data is nibble-wise palindrome. Program: .model small .stack 64 .data num dw 4224h temp dw 0 res db .code mov ax,@data mov ds,ax mov ax,num mov bx,ax
Dept. of E&C, RNSIT

; address of DS is moved to DS register ; copy the number to AX register ; copy the number to BX register
48

and ax,0Fh or temp,ax mov cx,3 next: push cx mov cx,4 shl temp,cl shr num,cl mov ax,num mov dx,0Fh and ax,dx or temp,ax pop cx loop next cmp temp,bx je pali mov res,00h palindrome jmp down pali: mov res,01h down: mov ah,4ch int 21h end Result:

; obtain the lower nibble of the no. ; initialize a counter ; push the counter value on stack ; initialize the counter to 4 ; shift the masked nibble to the left ; shift the given data to the right ; move the shifted data to register ; store 0Fh in DX register ; obtain the other nibble ; recover the value of the first counter ; compare the data with the shifted data ; indication if the number is not a

; indication if the number is a palindrome ; terminate the program

i) num = 4224h res = 01 (the number ia a palindrome) ii) num = 1234h res = 00 (the number is not a palindrome)

42.

Program to concatenate two strings.

Program: .model small .stack .data m1 db 0dh,0ah, enter string 1$


Dept. of E&C, RNSIT 49

m2 db 0dh,0ah, enter string 2$ m3 db 0dh,0ah, after concatenation$ str1 db 20h len1 db 00h res1 db 20dup$ str2 db 20h len2 db 00h res2 db 20dup$ .code mov ax,@data ; address of DS is moved to DS mov ds,ax ; register lea dx,m1 call disp ; to display m1 lea dx,str1 call read ; to read string 1 lea dx,m2 call disp ; to display m2 lea dx,str2 call read ; to read string 2 lea si,res1 ; SI points to string1 mov cl,len1 xor ch,ch add si,cx ; CX is added to SI to point to the end of string1 lea di,res2 ; DI points to string 2 mov cl,len2 ; length of string2 is used as counter up: mov al,[di] mov [si],al ; string2 characters are moved to string1 inc si inc di loop up mov byte ptr [si],$ ; the string is terminated by a $ lea dx,m3 call disp ; to display m3 mov ah,4ch int 21h disp proc ; procedure to display strings mov ah,09h int 21h
Dept. of E&C, RNSIT 50

ret disp endp read proc mov ah,0Ah int 21h ret read endp end Result: i)

; procedure to read strings

;end

string1 = micro string2 = processors after concatenation microprocessors string1 = karna string2 = taka after concatenation karnataka

ii)

43. Program to delete a string at the given position. Program: .model small .stack .data m1 db 0dh,0ah, enter string$ m2 db 0dh,0ah, enter position for delete$ m3 db 0dh,0ah, string after deletion is$ str db 20h len db 00h res db 20dup$ .code mov ax,@data ; address of DS is moved to DS mov ds,ax ; register lea dx,m1 call disp ; to display m1 lea dx,str call read ; to read string lea dx,m2
Dept. of E&C, RNSIT 51

call disp mov ah,01h int 21h to be deleted dec al sub al,30h 30h to get binary dec len mov cl,len sub cl,al xor ch,ch xor ah,ah lea si,res where deletion has add si,ax up: mov al,[si+1] mov[si],al position are inc si loop up mov byte ptr [si],$ lea dx,m3 call disp lea dx,res call disp mov ah,4ch int 21h disp proc mov ah,09h int 21h ret disp endp read proc mov ah,0ah int 21h ret read endp end
Dept. of E&C, RNSIT

; to display m2 ; to read position where the string is

; the position value is subtracted with equivalent

; SI is made to point to the position ; to take place ; all the characters from the deletion ; moved 1 location down ; the string is terminated by a $ ; to display m3 ; to display the string after deletion

; procedure to display strings

; procedure to read strings

; end
52

Result: String = India Positon is 02 String after deletion is Idia

44.Program to insert a string at a given position. Program: .model small .stack .data m1 db 0dh,0ah, enter string $ m2 db 0dh,0ah, enter position for insertion$ m3 db 0dh,0ah, enter character for insertion$ m4 db 0dh,0ah, string after insertion is$ str db 20h len db 00h res db 20dup$ .code mov ax,@data ; address of DS is moved to DS mov ds,ax ; register lea dx,m1 call disp ; to display m1 lea dx,str call read ; to read string lea dx,m2 call disp ; to display m2 mov ah,01h int 21h ; to get position for insertion
Dept. of E&C, RNSIT 53

dec al sub al,30h mov cl,len xor ch,ch lea si,res to it so as point add si,cx sub cl,al of insertion) up: mov al,[si-1] mov[si],al up dec si loop up lea dx,m3 call disp mov ah,01h int 21h mov [si],al lea dx,m4 call disp lea dx,res call disp mov ah,4ch int 21h disp proc mov ah,09h int 21h ret disp endp read proc mov ah,0ah int 21h ret read endp end

; 30h is subtracted from ASCII value

; SI points to the string and CX is added ; to the position after the string ; counter = (length of string position

; the characters are moved one position

; to display m3 ; to read the character to be inserted ; the character is inserted to the string ; to display m4

; to display the string after deletion

; procedure to display strings

; procedure to read strings

; end

Result:
Dept. of E&C, RNSIT 54

String = idia Position = 02 Character to be inserted = n

8 0 8 6

8 2 5 5

PC0 PC1 PC2 PA0 PA1 PA2 PA3 PA4

String after insertion is india

44. Program to count the number of vowels in a given string. Program: .model small .stack .data str1 db pramod$
Dept. of E&C, RNSIT

; given string
55

str2 db aeiou$ res db 00h .code mov ax,@data mov ds,ax lea si,str1 lea di,str2 mov cx,05h mov dl,06h xor bh,bh up1: mov al,[si] up: mov bl,[di] compared with cmp al,bl jne dn inc bh otherwise, dn: inc di the next character loop up lea di,str2 inc si mov cx,05h dec dl jnz up1 mov res,bh res mov ah,4ch int 21h end

; vowel string aeiou ; res to store number of vowels ; address of DS is moved to DS ; register ; SI points to the given string ; DI points to vowel string ; CX = length of vowel string ; DL = length of given string ; BH is used as a vowel counter ; each character of given string is ; each character of vowel string ; if equal, BH is incremented ; pointers are updated so as to check

; the number of vowels is stored in

; terminate the program

Result: i) string = pramod res = 02h (no. of vowels)

45.Program to case reverse a given string. Program:


Dept. of E&C, RNSIT 56

.model small .stack .data str db pRamOD$ .code mov ax,@data register mov ds,ax mov cx,06h string) lea si,str1 back: mov al,[si] cmp al,A checked if it jb last yes then 20h cmp al,Z between a and z ja next add al,20h
8086 Logic Controller PA LEDs

; given string ; address of DS is moved to DS

; counter for 06h (length of ; SI points to the given string ; the character of the string is ; lies between A and Z. If ; is added to it to make it

jmp last nex t: cm p al,

a jb last then 20h is cmp al,z between A ja last sub al,20h last: mov [si],al its position inc si loop back
Dept. of E&C, RNSIT

; if no then the character is checked if it lies ; between a and z. If yes ; subtracted from it to make it ; and Z ; the character is stored back in

; the loop is repeated for all


57

characters lea dx,str1 screen mov ah,09h int 21h mov ah,4ch int 21h end Result: Given string = pRamOD Case reversed string is PrAMod 46. Program to multiply 2 ASCII numbers. Flowchart: Program: .model small .stack .data x db 31h y db 32h .code mov ax,@data register mov ds,ax mov al,x mov bl,y and al,0Fh and bl,0Fh mul bl aam or ax,3030h mov ah,4ch int 21h end Result: x = 31h
Dept. of E&C, RNSIT

; the string is output on DOS

; terminate the program

; get the two numbers

; move the address of DS to DS

; get the first ASCII no. ; get the second ASCII no. ; mask the MS bits ; multiply the 2 nos. ; ASCII adjust after addition ; OR AX with 3030h ; terminate the program

y = 32h
58

AX = 3032h

47. Program to read and set system date & time. Program: .model small .stack .data .code mov ax,@data register mov ds,ax mov ah,2ah int 21h mov cx,1985 1985 mov dh,3 mov dl,23 mov ah,2bh 21h
8086 Logic Controller PA LEDs

; move the address of DS to DS

; to read system date using DOS ; to set system date to 23rd March ; CX-year, DH-month, DL-day ; to set system date using DOS

int 21h mo v ah,

2ch

; to read system time using DOS int 21h int 21h mov ch,17 ; to set system time to 5:35 PM mov cl,35h ; CH-hours, CL-minutes xor dx,dx ; DH-seconds, DL-hundredth of mov ah,2dh ; to set system time using DOS

a second 21h int 21h mov ah,4ch


Dept. of E&C, RNSIT

; terminate the program using


59

DOS int 21h int 21h end ; end of program

Interfacing Programs 1.Keyboard Interfacing Interfacing circuit:

Program: .model small .stack


Dept. of E&C, RNSIT 60

.data pa equ 9800h pb equ 9801h pc equ 9802h cw equ 9803h key db ? .code mov ax,@data register mov ds,ax mov al,90h as i/p in mov dx,cw out dx,al row0: mov bl,00h mov ah,01h nxt_row: mov al,ah port mov dx,pc out dx,al nop mov dx,pa register in al,dx cmp al,00 indicates that jnz col_serc col_serc to find the key add bl,08h rcl ah,01h row cmp ah,08h jz row0 jmp nxt_row col_serc: rcr al,01 jc keyf key value inc bl rotation jmp col_serc
Dept. of E&C, RNSIT

;addresses of the ports of 8255 PPI

; store the address of DS in DS

; CW for making PC as o/p and PA ; I/O mode ; to select row 0 ; and is OUTed on PC which acts as O/P

; the status of PA is read into AL

; if AL is not equal to 00 then it ; a key is pressed so jump to ; otherwise add 08h to BL ; rotate AH once to select next

; rotate right to find which bit is 1 ; when CY = 1, store BL as the ; increment BL after each

61

keyf: mov key,bl mov ah,4ch int 21h end

; end

Result: The keyboard interfacing program is verified and the key pressed is observed on the DOS screen. Key pressed is 3 and key = 03h

2.Logic Controller Interfacing I. Interfacing diagram: Ring Counter

Truth Table for ring counter: D7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 D6 0 0 0 1 0 0 D5 0 0 1 0 0 0 D4 0 1 0 0 0 0 D3 1 0 0 0 0 0


62

D2

D1

D0

Dept. of E&C, RNSIT

0 1

1 0

0 0

0 0

0 0

0 0

0 0

0 0

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data register mov ds,ax mov al,80h mode mov dx,cw out dx,al mov al,01h mov cx,16 mov dx,pa back: out dx,al call delay rol al,01h loop back
8086 Logic Controller PA LEDs

; address of ports of 8255 PPI

; store the address of DS in DS

; CW to configure 8255 in I/O ; with PA as O/P ; initial bit pattern ; counter for 16 is used ; output the bit pattern on PA ; call delay routine ; rotate the bit pattern left once ; and loop back mo v ah, 4ch int 21h

delay proc mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up
Dept. of E&C, RNSIT

; routine for delay ; registers BX and SI are used to ; BX is decremented


63

dec si decremented jnz up1 value and is zero ret delay endp end

; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes

Result: The ring counter is verified using the above truth table.

II. UP/DOWN-Counter Interfacing diagram:

BCD

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code
Dept. of E&C, RNSIT

; address of ports of 8255 PPI

64

mov ax,@data register mov ds,ax mov al,80h mode mov dx,cw out dx,al mov al,00h mov dx,pa back: out dx,al call delay add al,01h adjusted daa cmp al,99h jne back out dx,al mov ah,4ch int 21h delay proc mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up dec si decremented jnz up1 value and is zero

; store the address of DS in DS

; CW to configure 8255 in I/O ; with PA as O/P ; initial BCD count ; count is output on the PA bits ; call delay routine ; count is incremented and decimal

; counter is made to count till 99

; routine for delay ; registers BX and SI are used to ; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes

ret delay endp end Result: The BCD counter is verified for a count from 00 to 99 and 99 to 00 with a finite delay between each count.

III. HEX UP/DOWN -Counter


Dept. of E&C, RNSIT 65

Flowchart:

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data register mov ds,ax mov al,80h mode mov dx,cw out dx,al mov al,00h mov dx,pa back: out dx,al call delay add al,01h adjusted cmp al,0FFh jne back out dx,al mov ah,4ch int 21h delay proc mov si,1FFFh +5V GND mov bx,0FFFFh up1: Stepper generate delay
B W

; address of ports of 8255 PPI

; store the address of DS in DS

; CW to configure 8255 in I/O ; with PA as O/P ; initial BCD count ; count is output on the PA bits ; call delay routine ; count is incremented and decimal ; counter is made to count till 99

; routine for delay ; registers BX and SI are used to

Motor Interfacing Card


8 0 8 6

Dept. of E&C, RNSIT

66

up: dec bx jnz up dec si decremented jnz up1 value and is zero ret delay endp end

; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes

Result: The HEX counter is verified for a count from 00 to FFh and FFh to 00 with a finite delay between each count.

IV. Johnson Counter Truth Table for Johnson counter: D7 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 D6 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 D5 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 D4 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 D3 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1


67

D2

D1

D0

Dept. of E&C, RNSIT

0 0

0 0

0 0

0 0

0 0

0 0

0 0

1 0

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data mov ds,ax mov al,80h mov dx,cw out dx,al mov al,00h back: stc rcr al,01h CY, so that mov dx,pa FFh out dx,al call delay cmp al,0FFh jnz back out dx,al clc back1: shr al,01h with MSB mov dx,pa till pattern out dx,al call delay cmp al,00h jnz back1 out dx,al mov ah,4ch int 21h
Dept. of E&C, RNSIT

; address of ports of 8255 PPI

; store the address of DS in DS register ; CW to configure 8255 in I/O mode ; with PA as O/P ; initial bit pattern ; set the carry flag ; rotate the bit pattern through ; MSB=1, till the pattern becomes

; the previous bit pattern is shifted ; made 0 by clearing the CY flag ; becomes 00h

68

delay proc mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up dec si decremented jnz up1 value and is zero ret 7 endp delay Segment LEDs end

; routine for delay ; registers BX and SI are used to ; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes

; end of program

Result: The johnson counter is verified using the above truth table.
Display Interface Card

V. Alternating Counter
8

Interfacing diagram:
0 8 6

Shift Register Shift Register Shift Register Shift Register


PB PC

Dept. of E&C, RNSIT

69

1 1 0 1 0 1 0 1 2 0 1 0 1 0 1 0 Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data register mov ds,ax mov al,80h mode mov dx,cw out dx,al mov cx,05h back: mov al,55h mov dx,pa out dx,al call delay mov al,0AAh out dx,al call delay loop back mov ah,4ch
Dept. of E&C, RNSIT

55h AAh

; address of ports of 8255 PPI

; store the address of DS in DS

; CW to configure 8255 in I/O ; with PA as O/P ; counter for 05h ; the bit pattern 55h is output on PA

; call delay routine ; the bit pattern AAh is output on PA ; call delay routine

70

int 21h delay proc mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up dec si decremented jnz up1 value and is zero ret delay endp end ; routine for delay ; registers BX and SI are used to ; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes

Result: The Alternating counter is verified by sending bit patterns 55h and AAh on PA. VI. Fighting Counter Truth Table for fighting Counter: D7 table entry 0 1 1 1 1 1 1 1 0 D6 D5 D4 D3 D2 D1 D0 Look-up

0 0 1 1 1 1 1 0 0

0 0 0 1 1 1 0 0 0

0 0 0 0 1 0 0 0 0

0 0 0 0 1 0 0 0 0

0 0 0 1 1 1 0 0 0

0 0 1 1 1 1 1 0 0

0 1 1 1 1 1 1 1 0

00h 81h C3h E7h FFh E7h C3h 81h 00h


71

Dept. of E&C, RNSIT

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data mov ds,ax mov al,80h mov dx,cw out dx,al lea bx,c to BX reg mov cl,00h back: mov al,cl moved to xlat the PA mov dx,pa out dx,al call delay inc cl cmp cl,09h 09 times jnz back mov ah,4ch int 21h delay proc push bx mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up dec si decremented jnz up1
Dept. of E&C, RNSIT

; address of ports of 8255 PPI

; store the address of DS in DS register ; CW to configure 8255 in I/O mode ; with PA as O/P ; offset of look-up table is moved ; CX = 00h ; the values in the look up table are ; AL reg and are output on

; call delay routine ; the above steps are being repeated

; routine for delay

; registers BX and SI are used to ; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial
72

value and is repeated till SI becomes zero pop bx ret delay endp end

; end of program

Result: The fighting counter is verified using the above look-up table.

3.Stepper motor interface Interface Diagram:

Count = (Rotation Angle)/(step Angle) = 360/1.8 = 200


Dept. of E&C, RNSIT 73

Program: .model small .stack .data pa equ 9800h cw equ 9803h .code mov ax,@data register mov ds,ax mov al,80h mode mov dx,cw out dx,al mov cx,200 mov al,11h mov dx,pa rotate: out dx,al call delay rol al,01h energize next coil loop rotate mov ah,4ch int 21h delay proc push ax push bx mov si,1FFFh up1: mov bx,0FFFFh generate delay up: dec bx jnz up dec si decremented jnz up1 value and is zero
Dept. of E&C, RNSIT

; address of ports of 8255 PPI

; store the address of DS in DS

; CW to configure 8255 in I/O ; with PA as O/P port ; counter for 360 rotation ; bit pattern to energize coil A

; call delay routine ; the bit pattern is rotated to ; and is repeated count times

; routine for delay

; registers BX and SI are used to ; BX is decremented ; when BX=0, Si is ; and BX is reloaded with initial repeated till SI becomes
74

pop bx pop ax ret delay endp end

Result: The stepper motor is rotated in both clockwise and anti-clockwise direction and the rotation is done for an angle 360 keeping the count as 200.

4.Printer Interfacing Program: .model small .stack .data msg db GOOD LUCK .code mov ax,@data mov ds,ax mov si,00h mov cx,09h up: mov ah,05h where the mov dl,msg[si] int 21h inc si loop up mov ah,4ch interrupt int 21h end

; message to be printed ; store the address of DS in DS register ; initialize memory pointer ; initialize the counter ; DOS interrupt to print a character ; character is present in DL register ; increment memory pointer ; repeat until CX=0 ; terminate the program using DOS

; end of program

Result: GOOD LUCK was printed on the paper.

Dept. of E&C, RNSIT

75

5.Seven-Segment Display Interfacing. Interfacing diagram:

Dept. of E&C, RNSIT

76

Codes to display message HELP: a 1


Dept. of E&C, RNSIT

b 1 0

c 0

d 0

e 1

g = 91h

77

= 61h

= 0E3h

= 31h

Codes to display message FIRE: a b c 0 1 1

d 1

e 0

f 0

g 0

h 1 = 71h

= 0F3h

= 11h

0 61h Program:
Dept. of E&C, RNSIT

78

.model small .stack .data pb equ 9801h pc equ 9802h cw equ 9803h m1 db 31h, 0E3h, 61h, 91h m2 db 61h, 11h, 0F3h, 71h .code send macro val,port mov al,val mov dx,port out dx,al endm mov ax,@data register mov ds,ax send 80h,cw mode back: lea si,m1 call disp mov ah,01h pressed int 16h jnz stop call delay lea si,m2 display m2 [FIRE] call disp call delay jmp back stop: mov ah,4ch int 21h disp proc mov cx,04 next: mov bl,08 character mov al,[si] nextb: send al,pb
Dept. of E&C, RNSIT

; addresses of the ports of 8255 PPI

; code for HELP ; code for FIRE ; macro to out values to ports of 8255

; address of DS is moved to DS

; CW to configure 8255 in I/O and PB,PC as O/P ; to display m1 [HELP] ; function to check if a key is ; If a key is pressed, then stop ; if a key is not pressed,

; Display procedure ; counter of 4 for 4 characters ; counter for 08 bits in each

; the character is out on PB and is


79

rotated ror al,01 push ax mov al,00h transition on PC send al,pc mov al,01h send al,pc pop ax dec bl jnz nextb inc si character loop next ret disp endp delay proc delay mov bx,0FFFh b2: mov cx,0FFFFh b1: loop b1 dec bx jnz b2 ret delay endp end

; to make a low to high

; to display next

; end of display procedure ; procedure to generate

Result: The message HELP FIRE is successfully displayed using the above codes.

Viva questions:
Dept. of E&C, RNSIT 80

1) How many bit 8086 microprocessor is? 2) What is the size of data bus of 8086? 3) What is the size of address bus of 8086? 4) What is the max memory addressing capacity of 8086? 5) Which are the basic parts of 8086? 6) What are the functions of BIU? 7) What are the functions of EU? 8) How many pin IC 8086 is? 9) What IC8086 is? 10) What is the size of instruction queue in 8086? 11) What is the size of instruction queue in 8088? 12) Which are the registers present in 8086? 13) What do you mean by pipelining in 8086? 14) How many 16 bit registers are available in 8086? 15) Specify addressing modes for any instruction? 16) What do you mean by assembler directives? 17) What .model small stands for? 18) What is the supply requirement of 8086? 19) What is the relation between 8086 processor frequency & crystal frequency? 20) Functions of Accumulator or AX register? 21) Functions of BX register? 22) Functions of CX register? 23) Functions of DX register? 24) How Physical address is generated? 25) Which are pointers present in this 8086? 26) Which is by default pointer for CS/ES? 27) How many segments present in it? 28) What is the size of each segment? 29) Basic difference between 8085 and 8086? 30) Which operations are not available in 8085? 31) What is the difference between min mode and max mode of 8086? 32) What is the difference between near and far procedure? 33) What is the difference between Macro and procedure? 34) What is the difference between instructions RET & IRET? 35) What is the difference between instructions MUL & IMUL? 36) What is the difference between instructions DIV & IDIV? 37) What is difference between shifts and rotate instructions? 38) Which are strings related instructions?
Dept. of E&C, RNSIT 81

39) Which are addressing modes and their examples in 8086? 40) What does u mean by directives? 41) What does u mean by Prefix? 42) What .model small means? 43) Difference between small, medium, tiny, huge? 44) What is dd, dw, db? 45) Interrupts in 8086 and there function. 46) What is the function of 01h of Int 21h? 47) What is the function of 02h of Int 21h? 48) What is the function of 09h of Int 21h? 49) What is the function of 0Ah of Int 21h? 50) What is the function of 4ch of Int 21h? 51) What is the reset address of 8086? 52) What is the size of flag register in 8086? Explain all. 53) What is the difference between 08H and 01H functions of INT 21H? 54) Which is faster- Reading word size data whose starting address is at even or at odd address of memory in 8086? 55) Which are the default segment base: offset pairs? 56) Can we use SP as offset address holder with CS? 57) Which are the base registers in 8086? 58) Which is the index registers in 8086? 59) What do you mean by segment override prefix? 60) Whether micro reduces memory requirements? 61) What do you mean by macro? 62) What is diff between macro and procedure? 63) Types of procedure? 64) What TASM is? 65) What TLINK is? 66) What TD is? 67) What do u mean by assembler? 68) What do u mean by linker? 69) What do u mean by loader? 70) What do u mean by compiler? 71) What do u mean by emulator? 72) Stack related instruction? 73) .stack 100 means? 74) What do you mean by 20 dup (0)?

Dept. of E&C, RNSIT

82

Dept. of E&C, RNSIT

83

You might also like