68
Q1. Byte and word move using different addressing modes Program: .model small .data Num dw 4321h .code Mov ax,@data Mov ds,ax Mov ax,1234h ;immediate addressing Mov bx,ax ;register addressing Mov ax,num ;direct addressing Mov si,1000h Mov al,[si] ;indirect addressing Mov bl,[si+100h] ;relative addressing Mov bx,1000h Mov ax,[si+bx] ;base index addressing Mov cx,[si+bx+100h] ;relative base index addressing Mov ah,4ch Int 21h End Output: Ax=1234h Bx=1234h Ax=4321h…..

Microprocessor Lab

Embed Size (px)

Citation preview

Page 1: Microprocessor Lab

Q1. Byte and word move using different addressing modes

Program:

.model small

.dataNum dw 4321h.codeMov ax,@dataMov ds,axMov ax,1234h ;immediate addressingMov bx,ax ;register addressingMov ax,num ;direct addressingMov si,1000hMov al,[si] ;indirect addressingMov bl,[si+100h] ;relative addressingMov bx,1000hMov ax,[si+bx] ;base index addressingMov cx,[si+bx+100h] ;relative base index addressingMov ah,4chInt 21hEnd

Output:

Ax=1234hBx=1234hAx=4321h…..

2. Program to transfer a block of data without

Page 2: Microprocessor Lab

overlap

.model small

.datad1 db 1,2,3,4,5d2 db 10 dup(0).codemov ax,@data ;Initialize the data segmentmov ds,axlea si,d1 ;Load offset address of d1 in silea di,d2 ;Load offset address of d2 in dimov cx,05 ;load cx with countup:mov al,[si] ;Move the 1st element of d1 to almov [di],al ;Move to d2inc si ;Increment si inc di ;Increment didec cx ;decrement the counterjnz up ;Repeat till cx becomes zeromov ah,4ch ;Terminate the programint 21hend

d1 d2

0102030405

0102030405

Page 3: Microprocessor Lab

3. Program to transfer a block of data with overlap

.model small

.datad1 db 1,2,3,4,5d2 db 10 dup(0).codemov ax,@data ;Initilize the data segmentmov ds,axlea si,d1 ;Load the offset address of d1 in silea di,d2 ;Load the offset address of d2 in dimov cx,05h ;load cx with countup:mov al,[si] ;Move the first element of d1 to almov [di],al ;Move to d2inc si ;Increment si inc di ;Increment didec cx ;decrement the counterjnz up ;Repeat till cx becomes zeromov cx,05h ;Load cx with countlea si,d1+2 ;Load si with offset address of d1+2lea di,d2 ;Load the offset address of d2 in diup1:mov al,[di] ;Move the first element of d2 to almov [si],al ;Move al to d1INC di ;Increment di inc si ;Increment siloop up1 ;Repeat the loopmov ah,4ch ;Terminate the programint 21hend

d1 d2(temp location) (after block transfer)(with overlap)

0102030405

0102030405

01020102030405

Page 4: Microprocessor Lab

4. write an ALP to interchange 2 blocks of data

.model small

.datad1 db 1,2,3,4,5d2 db 22h,33h,44h,55h,66h.codemov ax,@data ;Initilize the data segmentmov ds,axlea si,d1 ;Load offset address of d1 in silea di,d2 ;Load offset address of d2 in dimov cx,05h ;load cx with countup:mov al,[si] ;Move the first element of d1 to almov bl,[di] ;Move the first element of d2 to blmov [di],al ;Move al to d2mov[si],bl ;Move bl to d1inc si ;Increment siinc di ;Increment didec cx ;decrement the countjnz up ;If not=0, Jump to label upmov ah,4ch ;Terminate the programint 21hend

d1 d2 d1 d2

0102030405

1122334455

0102030405

1122334455

Before block exchange After block exchange

Page 5: Microprocessor Lab

5. Addition and subtraction of N numbers Program to add N data words

.model small

.datan1 dw 1111h,2222h,3333h,4444h,0ffffhn2 dw 02 dup(0).codemov ax,@data ;Initialize Data segmentmov ds,axlea si,n1 ;Load si with the offset address of n1mov ax,00h ;Clear accumulatormov cx,05h ;Load cx with countmov dx,00 ;Clear dxclc ;clear carryup:add ax,[si] ;Add the first element with accumulatorinc si ;Increment si twiceinc si ;to point to the next data worddec cx ;Decrement the countjnz up ;If cx≠0, repeat the loopjnc down ;else if carry =0, jump to label downinc dx ;if carry=1, increment dxdown:mov [si],ax ;Store the result in memory pointed

to ;by simov [si+2],dx ;Store carry in next memory locationmov ah,4ch ;Terminate the programint 21hend

I/p:1111h,2222h,3333h,4444h,0ffffho/p:1aaa9h

Page 6: Microprocessor Lab

6. Write an ALP to add/subract 2 32 bit signed/unsigned numbers.

.model small

.datad1 dd 0f2222222hd2 dd 33333333hd3 dd 10 dup(0).codemov ax,@data ;Initialize the data segmentmov ds,axmov ax,00h ;Clear axlea si,d1 ;Load si with offset address of d1lea di,d2 ;Load di with offset address of d2lea bx,d3+5 ;Load bx with offset address of d3+5mov cx,04h ;Load cx with with the countmov dl,00h ;clear dlclc ;clear carryup:mov al,[si] ;Move the lower byte of Ist number to aladc al,[di] ;Add with carry the second no.mov [bx],al ;Store the result in memory pointed to

;by bxinc si ;Increment siinc di ;Increment didec bx ;decrement bxdec cx ;Decrement cxjnz up ;if cx is not equal to zero, repeat

;the loopjnc down ;If no carry,jmp to label downinc dl ;else increment carry counterdown:mov [bx],dl ;Move the carry to memory locationmov ah,4ch ;terminate the programint 21hend

Input: 0f2222222h and 33333333h( unsigned number addition) Output:125555555h

Page 7: Microprocessor Lab

6.Program to add/subtract 2 64 unsigned/signed numbers

.model small

.datad1 dq 1234567812345678hd2 dq 1234567812345678hres db 09 dup(0).codemov ax,@data ;Initialize data segmentmov ds,axclc ;Clear carrymov cx,08h ;Load cx with countlea si,d1 ;Load si with offset of d1lea di,d2 ;Load di with offset of d2lea bx,res+8 ;Load bx with offset of the resultmov ah,00 ;Mov 00 to ahup:mov al,[si] ;Mov the LS byte of first no. to aladc al,[di] ;Add with carry to the LS byte of

the ;second numbermov [bx],al ;Move the result to memory pointed by bxdec bx ;Decrement bx inc si ;Increment siinc di ;Increment didec cx ;Decrement count jnz up ;If cx≠0, repeat the loopjnc exit ;else if carry=0,store 00 in memoryinc ah ;else increment carryexit:mov [bx],ah ;Store carry in memory locationmov ah,4ch ;Terminate the programint 21hend

Input: 1234567812345678h,1234567812345678hOutput:2468ACF02468ACF0H

Page 8: Microprocessor Lab

7 Program to divide a 32 bit number by a 16 bit number

.model small

.datanum dd 12345678hnum1 dw 2345hres dw 02 dup(0).codemov ax,@data ;Initialize data segmentmov ds,axlea si,num ;Load si with EA of nummov dx,[si+2] ;load dx with higher word of dividendmov ax,[si] ;load ax with lower word mov bx,num1 ;move the divisor into bxdiv bx ;dx:ax/bxlea di,res ;load di with EA of resmov [di],dx ;move remainder to memorymov [di+2],ax ;move quotient to memorymov ah,4ch ;terminate the programint 21hend

Input: dividend-12345678h, divisor-2345hOutput:Quotient:8422h, Remainder:134Eh

Page 9: Microprocessor Lab

8. To find the Largest/smallest number in an array of 16 bit numbers

.model small

.dataarray dw 1111h,2222h,8567h,4589h,5555hres dw 02 dup(0).codemov ax,@datamov ds,ax ;Initialize Data segmentlea bx, array ;Load BX with EA of arraymov cx,05h ;cx=number of elements in the arraymov ax,[bx] ;move the 1st word of the array to axup:cmp ax,[bx+2] ;compare the 1st and 2nd elementjnc below ;if 1st number>2nd number compare 1st

with ;3rd number and so on.;Jc below ;for smallest numbermov ax,[bx+2]below:inc bxinc bxdec cxjnz up ;if cx0, repeat mov res,ax ;store the largest number in location resmov ah,4ch ;terminate the programint 21hend

Input: 1111h,2222h,8567h,4589h,5555h Output:8567h stored in location res

Page 10: Microprocessor Lab

9 . Program to sort a given set of 16 bit unsigned integers in ascending/descending order

.model small

.datan1 dw 0ffffh,5555h,3333h,7777hcount dw 04h .code mov ax,@data ;Initialize Data segment mov ds,ax mov bx,count ;bx=number of elements to be sorted dec bxl2: mov cx,bx ;number of iterations mov si,00h ;si=0l1: mov ax,n1[si] ;move the 1st number of n1 to ax inc si inc si cmp ax,n1[si] ;compare with the next number jc rep1 ;if 1st no.>2nd no, no exchange ;jnc rep1 ;jnc for descending order. xchg ax,n1[si] ;else exchange 1st and 2nd no mov n1[si-2],axrep1:loop l1 ;repeat till cx=0 dec bx jnz l2 ;all the iterations over?

mov ah,4ch int 21h end

Input: 0ffffh, 5555h, 3333h, 7777hOutput: 3333h, 5555h, 7777h, 0ffffh,

Page 11: Microprocessor Lab

10. Program to search a character in a given string

.model small

.datastring db "bmscollege$"enter db "j"msg db 0ah,0dh,"character found$"msg1 db 0ah,0dh,"charcter not found$"char db 02 dup(0).codemov ax,@data ;initialize DS and ESmov ds,axmov es,axmov cl,0ah ;CL=countlea si,string ;Load the EA of string into siup:mov al,[si] ;mov the first character to alcmp al,enter ;compare with the character to

be ;searchedjz found ;jump to foundinc si ;else repeat the loopdec cljnz upnfound:lea dx,msg1 ;display, char not foundmov ah,09hint 21hjmp end1found:lea dx,msg ;display, char foundmov ah,09hint 21hend1:mov ah,4ch ;terminateint 21hend

Output: character not found

Page 12: Microprocessor Lab

11 Program to find the factorial of a given number(no<=8)

.model small

.datano dw 08hres dw 02 dup(0).codemov ax,@data ;initialize DSmov ds,axmov ax,no ;move the number to ax and bxmov bx,nocmp ax,02h ;compare with 02hjz down ;if no=2,factorial=2,jump to down up:dec bx ;else decrement bxmul bx ;multiply ax and bxcmp bx,01h ;compare bx with 01jz down ;if 0, store the resultjmp updown:mov res,dxmov res+2,axmov ah,4ch ;end of the programint 21hend

I/P=08hO/P=9D80h

Page 13: Microprocessor Lab

12. Program to check whether the given data byte is positive or negative

.model small

.datan1 db 09Hn2 db 0ah,0dh,"the data is positive $"n3 db 0ah,0dh,"the data is negative $".codemov ax,@data ;Initialize data segmentmov ds,axclc ;Clear carrymov al,n1and al,10h ;Check the MSBjz d1 ;If 0, display the no is positivemov dx,offset n3mov ah,09hint 21hjmp end1d1:mov dx,offset n2 ;Else display the no is negativemov ah,09hint 21hend1:mov ah,4chint 21hend

Input:09hOutput:the data is positive

Input:0ffhOutput:the data is negative

Page 14: Microprocessor Lab

13. Program to check whether data byte is odd or even

.model small

.datan1 db 0feHn2 db 0ah,0dh,"the given data is odd $"n3 db 0ah,0dh,"the given data is even$"n4 db 04 dup(0).codemov ax,@data ;Initialize data segmentmov ds,axmov ah,00hmov bl,02hmov al,n1 ;move the number to aldiv bl ;divide by 2cmp ah,00h ;compare the remainder with 0.jz down ;if 0, the number is positivelea dx,n2mov ah,09h ;Display “the given …odd”int 21hjmp end1 ;jump to end1

down:lea dx,n3 ;display” the given …even”mov ah,09hint 21hend1:mov ah,4ch ;end of the programint 21hend

Input:0fehOutput displayed on the screen:“the given data is even”

Input:09hOutput displayed on the screen:”the given data is odd”

Page 15: Microprocessor Lab

14.Program to check whether the given word is a bitwise palindrome

.model small

.datamsg1 db 0ah,0dh,'it is a palindrome$'msg2 db 0ah,0dh,'it is not a palindrome$'num dw 4421h.codemov ax,@data ;Initialize data segmentmov ds,axmov ax,num ;Move number to axmov cl,10h ;Load cx with the countmov bx,00 ;Move 00 to bxclc ;Clear carryback:rol ax,01 ;Rotate left ax once, through carryrcr bx,01 ;Rotate bx once without carrydec cl ;Decrement the countjnz back ;If cl≠0, repeat the loopcmp ax,bx ;if cl=0, compare ax and bxjz pali ;If ax=bx, jump to label palimov ah,09hlea dx,msg2 ;Display “it is not a palindrome”int 21hjmp lastpali:mov ah,09hlea dx,msg1 ;Display ”it is a palindrome”int 21hlast:mov ah,4ch ;Terminate the programint 21hend

Page 16: Microprocessor Lab

15. Program to check whether the given data word is a nibble wise palindrome

.model small

.datanum dw 8448hmsg1 db 0ah,0dh,"it is a palindrome$"msg2 db 0ah,0dh,"it is not a palindrome$".codemov ax,@data ; Initialize data segmentmov ds,axmov ax,num ;Move number to axmov cl,04 ;Move 04 to clmov bx,ax ;Move ax to bxclc ;Clear carryup:ror bl,01 ; Rotate right bl once, through carrydec cl ;Decrement cljnz up ;Repeat the loop if cl≠0,cmp bh,bl ;if cl=0, compare bh with bljz pali ;If bh=bl, jump to label palimov ah,09hlea dx,msg2int 21hjmp end1 ;Jump to label end1pali:mov ah,09h ;Display “it is a palindrome”lea dx,msg1int 21hend1:mov ah,4ch ;Terminate the programint 21hend

Page 17: Microprocessor Lab

16. Program to add 2 ASCII numbers

.model small

.data;N1 db ‘9’;N2 db ‘9’ N1 db ‘5’ N2 db ‘8’N3 db 02 dup(0).codeMov ax,@data ;Initialize data segmentMov ds,axMov al,n1 ;move the first no to alMov bl,n2 ;second number to blMov ah,00h ;clear ahAdd al,bl ; add al and blAaa ;ASCII adjust after additionOr ax,3030h ;Or ax with 3030h for displayPush axMov dl,ah ;display the content of ahMov ah,02hInt 21hPop axMov dl,al ;display the content of alMov ah,02hInt 21hMov ah,4ch ;end of the programInt 21hEnd

Input : N1=’9’ N2=’9’

Output: 18Input: N1 = ‘5’ N2 = ‘8’Output: 13

Page 18: Microprocessor Lab

17. Program to convert a hex number to ascii number

.model small

.datanum db 35h;num db 0AAhres db 2 dup(0).codeMov ax,@dataMov ds,ax ; Initialize data segmentMov al,num ;move the number to alAnd al,0f0h ;mask the lower nibble of the digitMov cl,04hRol al,cl ;bring the digit to LSB positionCmp al,0ah ;if the number is < 0ah,jump to D1Jc d1Add al,07h ;if the number is>0ah, add 37hD1:add al,30hLea si,res ;store the result in memoryMov [si],alMov al,numAnd al,0fh ;mask the upper nibbleCmp al,0ah ;compare with 0ahJc d2 ;if the number is<0ah, jump to D2Add al,07h ;if the number is >0ah, add 37hD2:add al,30hMov [si+1],al ;store the result in memoryMov ah,4ch ;end of the programInt 21hEnd

Input:35hOutput:33h and 35h, stored in memory locations [si] & [si+1]Input:0AAhOutput: 41h and 41h stored in memory locations [si] & [si+1]

Page 19: Microprocessor Lab

18.Program to convert an 8 bit BCD number to binary number

.model small

.dataD1 db 99h;D1 db 85hD2 db 02 dup(0).codeMov ax,@dataMov ds,ax ; Initialize data segmentMov al,D1 ;move the BCD number to alAnd al,0f0h ;mask the lower nibbleMov cl,04hRol al,cl ; bring the upper nibble to LS positionMov bl,alMov al,0ahMul bl ;multiply with 0ahMov bl,al ;store the result in blMov al,D1 ;get the BCD number to alAnd al,0fh ;mask the upper nibbleAdd al,bl ;add al and blMov D2,al ;store the binary no in D2Mov ah,4ch ;end of the programInt 21hEnd

Input : 99Output: 63hInput : 85Output: 55h

Page 20: Microprocessor Lab

19.Program to convert an 8 bit binary number to BCD number

.model small

.datanum db 0ffh;num db 63hres db 03 dup(0).codeMov ax,@dataMov ds,ax ; Initialize data segmentMov al,num ;move the binary number to alMov ah,00h ;clear ahMov bl,64hdiv bl ;divide ax by 64hLea si,resMov [si],al ;move the quotient to [si]Mov al,ah ;move the remainder to almov ah,0h ;clear ahmov bl,0ahdiv bl ;divide ax by blmov [si+1],al ;move the quotient to [si+1]mov [si+2],ah ;move the remainder to [si+2]mov ah,4ch ;end of the programint 21hend

Input :0ffhOutput:02 05 05 stored in locations [si,si+1,si+2]Input :063hOutput:09 09 stored in locations [si+1, si+2]

Page 21: Microprocessor Lab

20. Program to check whether the code belongs to 2/5 code

.model small

.datan1 db 0ah,0dh,"the code is 2 out of 5 code$"n2 db 0ah,0dh,"the code is not a 2 out of 5 code$"n3 db 0fh.codemov ax,@datamov ds,ax ;initialize the data segmentmov al,n3 ;move the number to almov dl,00hand al,0e0h ;and it with 0ehjnz invalid ;if 1st three MSBs are not zero jump

to ;invalidmov bl,05hmov al, n3 ;if 1st three MSBs are non-zero, move

the ;number to alup:ror al,01h ;check for number of ones in remaining

5 ;bitsjnc d1 ;check for carryinc dl ;increment dl if carry=1d1:dec bl ;decrement the countjnz up ;if bl≠0, repeatcmp dl,02h ;compare dl with 02.jnz invalid ;if dl≠0, jump to invalidlea dx,n1 ;display ”the code is 2 out of 5 code” mov ah,09hint 21hjmp end1 ;jump to end1invalid:lea dx,n2; display “the code is not amov ah,09h ; 2 out of 5 codeint 21hend1:mov ah,4ch ;end of the programint 21hend

Input:0fhOutput: the code is not a 2 out of 5 codeInput:09hOutput: the code is a 2 out of 5 code

Page 22: Microprocessor Lab

21.Program to find the LCM of two data bytes

.stack 64

.data;num dw 05h,02hnum dw 09h,2dhlcm dw 2 dup(0).code

mov ax,@data mov ds,ax ;Initiliaze data segment mov ax,num ;move the 1st number to ax mov bx,num+2 ;and 2nd number to bx mov dx,0 ;clear dx cmp ax,bx ;compare ax and bx jnc again ;if ax>bx, jump to again xchg ax,bx ;otherwise exchange ax and bx mov num,ax mov num+2,bx again: push ax ;push ax and dx onto the stack push dx div bx ;divide ax by bx cmp dx,00h ;compare the remainder with 0 je store pop dx pop ax add ax,num jnc no_increment inc dxno_increment: jmp again store: pop lcm+2 pop lcm mov ah,4ch int 21h end

Input:05h, 02hOutput:0ahInput:

Page 23: Microprocessor Lab

22. Program to find the GCD of two numbers

.model small

.datanum dw 1bh,09hGcd dw ?.codeMov ax,@dataMov ds,ax ;Initiliaze data segment

Mov ax,num ;move the 1st number to axMov bx,num+2 ;and 2nd number to bxAgain:cmp ax,bx ;Je exitJb downDivaxbx:mov dx,0Div bxCmp dx,0Je exitMov ax,dxJmp againDown:xchg ax,bxJmp divaxbxExit:mov gcd,bxMov gcd+2,axMov ah,4chInt 21hEnd

Page 24: Microprocessor Lab

23. Program to divide a packed bcd number by an unpacked bcd number and display the quotient and remainder on the screen

.model small

.dataN1 db 6N2 db 7N3 db 02 dup(0).codeMov ax,@dataMov ds,axMov ah,n1Mov al,n2mov ch,09hAaddiv chOr ax,3030hPush axMov dl,ahMov ah,02hInt 21hmov dl, " "mov ah,02hint 21hpop axMov dl,alMov ah,02hInt 21hMov ah,4chInt 21hEnd

Input:6 and 7Output: Quotient 7, Remainder 4 displayed on the screen

Page 25: Microprocessor Lab

24.Program to multiply two unpacked BCD numbers and display the result

.model small

.dataN1 db 9N2 db 5N3 db 02 dup(0).codeMov ax,@dataMov ds,axMov al,n1Mov bl,n2mul blAamOr ax,3030hPush axMov dl,ahMov ah,02hInt 21hpop axMov dl,alMov ah,02hInt 21hMov ah,4chInt 21hEnd

Input: N1=9, N2=5Output:45 displayed on the screen

Page 26: Microprocessor Lab

25.program to multiply two 32 bit unsigned numbers

.model small

.datanum1 dd 0ff0aff0hnum2 dd 03304002hres dq 00h.stack 100h.codeMov ax,@dataMov ds,axLea si,num1Lea di,num2Lea bx,resclcmov ax,[si]mul word ptr[di]mov[bx],axmov cx,dxmov ax[si+2]mul word ptr[di]add ax,cxadc dx,00hpush dxpush axmov ax,[si]mul word ptr[di+2]pop cxadd ax,cxadc dx,00hmov[bx+2],axmov cx,dxmov ax,[si+2]mul word ptr[di+2]add ax,cxadc dx,00hpop cxadd ax,cxadc dx,00hmov[bx+4],axmov[bx+6],dxmov ah,4ch

Page 27: Microprocessor Lab

int 21hend

Input:Num1:0ff0aff0hNum2:03304002hOutput:32d32d18dd5fe0h

Page 28: Microprocessor Lab

26.Program to find the square of a given data byte

.model small

.datanum db 0ffhres db 04 dup(0).codemov ax,@data ;Initialize data segmentmov ds,axmov ah,00h ;Clear ahmov al,nummov bl,almul bllea si,resmov[si],ahmov[si+1],almov ch,02up:mov dl,[si]and dl,0f0hmov cl,04hror dl,clcmp dl,0ahjb downadd dl,07hdown:add dl,30hmov ah,02hint 21hmov dl,[si]and dl,0fhcmp dl,0ahjc down1add dl,07hdown1:add dl,30hmov ah,02hint 21hinc sidec chjnz upmov ah,4chint 21hendInput:04h

Page 29: Microprocessor Lab

Output:10h is displayed on the screenInput:0ffhOutput:FE01h is displayed on the screen

Page 30: Microprocessor Lab

27. Program to find the Cube of a given number

.model small

.datanum dw 05h;num dw 0ffhcube dw 02 dup(0).codeMov ax,@dataMov ds,axMov dx,0hMov ax,numMul numMov cx,dxMul numAdd ax,cxMov cube,axJnc skipInc dxSkip:mov cube+2,dxMov ah,4chInt 21hEnd

Input:0ffhOutput:fd02ffh in locations cube and cube+2Input:05hOutput:7d in locations cube and cube+2

Page 31: Microprocessor Lab

28. Program to find number of ones in a given data word and display the count on the screen

.model small

.datan1 dw 0fff0hn3 db 04 dup(0)mes1 db 0ah,0dh,"no of 1's$".codemov ax,@datamov ds,axmov bx,00hlea si,n3clcmov cl,10hmov ax,n1up:rcl ax,01jnc d1inc bld1:mov [si],bldec cxjnz upmov dl,[si]and dl,0f0hmov cl,04hror dl,clcmp dl,0ahjc d11add dl,07hd11:add dl,30hmov ah,02hint 21hmov dl,[si]and dl,0fhcmp dl,0ahjc d12add dl,07hd12:add dl,30hmov ah,02hint 21hmov ah,4chint 21h

Page 32: Microprocessor Lab

end

Input:0fff0hOutput:0c displayed on the screen

Page 33: Microprocessor Lab

29.ascii multiplication.model small.dataN1 db 6N2 db 7N3 db 02 dup(0).codeMov ax,@dataMov ds,axMov ah,n1Mov al,n2mov ch,09h;Mov ah,00hAaddiv chOr ax,3030hPush axMov dl,ahMov ah,02hInt 21hpop axMov dl,alMov ah,02hInt 21hMov ah,4chInt 21hEnd

Page 34: Microprocessor Lab

29 . Program to reverse the string

.model small

.datames1 db 0ah,0dh,"enter the string",0ah,0dh,"$"blank db " ", 0ah,0dh,"$"string db 18 dup(0).codemov ax,@datamov ds,axmov es,axmov dx,offset mes1mov ah,09hint 21hmov si,offset stringadd si,14hmov dl,24hmov [si],dlcops:mov ah,01hint 21hcmp al,0dhjz revdec simov [si],aljmp copsrev:mov dx,offset blank mov ah,09hint 21hmov ah,09hmov dx,siint 21hmov ah,4chint 21hend

Page 35: Microprocessor Lab

30.Program to transfer the string

.model small

.datasrc db 'string transfer$'dst db 25 dup(0).codemov ax,@datamov ds,axmov es,axlea si,srclea di,dstcldmov cx,0fh rep movsbmov ah,4chint 21hend

Page 36: Microprocessor Lab

31. program to search a sub string in a main string

data segmentmainstr db 80 dup(0)subst db 80 dup(0)mes1 db "enter the string",0ah,0dh,"$"mes2 db "enter the string to be searched",0ah,0dh,"$"sfound db "string found$"nfound db "string not found$"null equ 0dhcr db 0ah,0dh,"$"data endscode segmentassume cs:code,ds:datastart:mov ax,datamov ds,axmov es,axmov ah,09hlea dx,mes1int 21hmov si,00up:mov ah,01hint 21hcmp al,0dhjz accmov mainstr[si],alinc sijmp upacc:mov mainstr[si],allea dx,crmov ah,09hint 21hlea dx,mes2int 21hmov di,00t3:mov ah,01hint 21hcmp al,0dhjz outmov subst[di],alinc dijmp t3

Page 37: Microprocessor Lab

out:mov subst[di],allea dx,crmov ah,09hint 21hmov si,00mov di,00again:mov al,mainstr[si]cmp al,nulljz t2cmp al,subst[di]jz downmov di,00inc sijmp againdown:inc siinc dimov al,subst[di]cmp al,nulljz foundjmp againfound:mov ah,09hlea dx,sfoundint 21hjmp quitt2:mov ah,09hlea dx,nfoundint 21hquit:mov ah,4chint 21hcode endsend start

Output on the screenEnter the string: bmscollegeEnter the string to be searched: collegeOutput: string found

Page 38: Microprocessor Lab

3 2. Program to multiply two data words and display the answer by calling a far procedure

data segment;mul1 db 12h;mul2 db 24hmul1 db 0ffhmul2 db 0ffhdata endsstack_seg segmentdw 40 dup(0)tps label wordstack_seg endssbrt segment publicextrn disp:farsbrt endscode segmentassume cs:code,ds:datastart:mov ax,datamov ds,axmov sp,offset tpsmov al,mul1mul mul2mov cx,axmov al,ahcall dispmov al,clcall dispmov ah,4chint 21hcode endsend start

Page 39: Microprocessor Lab

Far procedure to display the number on the screen

public dispsbrt segment publicdisp proc farassume cs:sbrtpush axpush dxpush cxmov dl,aland dl,0f0hmov cl,04hror dl,clcmp dl,0ahjb downadd dl,07hdown:add dl,30hpush axmov ah,02hint 21hpop axmov dl,aland dl,0fhcmp dl,0ahjc down1add dl,07hdown1:add dl,30hpush axmov ah,02hint 21hpop axpop cxpop dxpop axretdisp endpsbrt endsend

Input:12h 24hOutput:0288h, displayed on the screen

Page 40: Microprocessor Lab

INTERFACING EXPERIMENTS

Keyboard Interface

24 keys are arranged in a 3x8 matrix fashion. The row lines are driven by pc0,pc1,pc2. The column lines are read through port A. When no key is pressed, all the return lines are low. The rows are driven high one after another in sequence. When a row is driven high, pressing a key in that row causes the corresponding return lines to be read as high. Then it scans for the column for which the key is pressed. The row and column position can be used to encode the key.

PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7000

011

022

03 3

044

05 5

06 6

077

08 8

099

0A

0B

0C

0D

0E

0F

10AC

11CE

12CHK

13

14MC

15MR

16M-

17M+

Page 41: Microprocessor Lab

1.Program to scan the keyboard for key closure and store the code of the key pressed in a memory location/display on the screen

.model small

.datares db ?.codemov ax,@data mov ds,ax mov dx,123h mov al,90h out dx,al start:mov ah,00h mov cx,03h mov bl,01h repeat:mov al,bl mov dx,122h out dx,al mov dx,120h in al,dx cmp al,00h jnz key add ah,08h rol bl,01h loop repeat jmp start key:ror al,01h jc store inc ah jmp key;hex to ASCII conversion;to dispaly the code of the key pressed on the screenstore: mov res,ah mov dl,ah mov cl,04h and dl,0f0h ror dl,cl cmp dl,0ah jc d11 add dl,07h d11: add dl,30h

Page 42: Microprocessor Lab

mov ah,02h int 21h mov dl,res and dl,0fh cmp dl,0ah jc d2 add dl,07h d2: add dl,30h mov ah,02h int 21h mov ah,4ch int 21h end

Page 43: Microprocessor Lab

2.Program to scan the keyboard for key closure and store the code of the key pressed in a memory location/display on the screen display row no and column no on the screen

.model small

.datares db ?.code mov ax,@data mov ds,ax mov dx,123h mov al,90h out dx,al start:mov ah,00h mov cx,03h mov bl,01h repeat:mov al,bl mov dx,122h out dx,al mov dx,120h in al,dx cmp al,00h jnz key add ah,08h rol bl,01h loop repeat jmp start key: ror al,01h jc store inc ah jmp keystore: mov res,ah mov dl,res call disp mov dl," " mov ah,02h int 21h mov ah,res cmp ah,10h jc loop1

Page 44: Microprocessor Lab

sub ah,10h jmp loop2loop1: cmp ah,08h jc loop2 sub ah,08h loop2:inc ah mov dl,ah mov res,ah call disp mov dl, " " mov ah,02h int 21h cmp bl,04h jne down dec bl down: mov res,bl mov dl,bl call disp jmp end1

disp proc mov cl,04h and dl,0f0h ror dl,cl cmp dl,0ah jc d11 add dl,07h d11: add dl,30h mov ah,02h int 21h mov dl,res and dl,0fh cmp dl,0ah jc d2 add dl,07h d2: add dl,30h mov ah,02h int 21h ret

disp endp

Page 45: Microprocessor Lab

end1: mov ah,4ch int 21h end

Page 46: Microprocessor Lab

Seven segment display interface

This interface provides 4 digit 7 seven segment display by the output of 4 cascaded SIPO shift registers. Data to be displayed is transmitted serially( bit by bit) to the interface over the port line PB0. Each bit is clocked into the shift registers by providing a common clock through port line PC0. Seven segment device used is common anode type. Hence low input must be given to each seven segment to glow and high to blank.

Page 47: Microprocessor Lab

3.program to implement constant display on seven segment display

.model small

.datacode1 db 099h,0b0h,0a4h,0f9hcount db 0fh.code

mov ax,@datamov ds,axlea si, code1mov al,80hmov dx,123hout dx,almov cx,04hnext1:mov bl,08h

mov al,[si] next:rol al,01h mov dx,121h out dx,al push ax mov al,0ffh mov dx,122h out dx,al mov al,00h out dx,al pop ax dec bl jnz next inc si loop next1 mov ah,4ch int 21h end

Page 48: Microprocessor Lab

4.program to implement rolling display on seven segment display

.model small

.datacode1 db 099h,0b0h,0a4h,0f9h,0ffh,0ffh,0ffh,0ffhcount db 0fh.codemov ax,@datamov ds,axmov al,80hmov dx,123hout dx,alstart:lea si,code1 call disp dec count jnz start mov ah,4ch int 21h disp proc near mov cx,08hnext1:mov bl,08h mov al,[si] next:rol al,01h mov dx,121h out dx,al push ax mov al,0ffh mov dx,122h out dx,al mov al,00h out dx,al pop ax dec bl jnz next inc si push bx call delay pop bx loop next1 ret

Page 49: Microprocessor Lab

disp endp delay proc near mov bx,0fffh outer:mov di,0ffffh inner:dec di jnz inner dec bx jnz outer ret delay endp end

Page 50: Microprocessor Lab

4.Program to control the speed of the stepper motor

.model small

.code mov ax,@data mov ds,ax mov dx,123h mov al,80h out dx,al mov dx,120h mov al,88h mov cx,0ffhrotate: out dx,al push cx call delay ror al,01h ;rol for anticlockwise pop cx loop rotate mov ah,4ch int 21h

delay proc mov cx,60h ;cx=20h.. to vary the speedouter: mov bx,0ffffhinner: dec bx jnz inner loop outer ret delay endp end

Page 51: Microprocessor Lab

Program to implement half adder using logic controller

.model smallSum db 0,1,1,2.codeMov ax,@dataMov ds,axMov dx,123hMov al,82hOut dx,alMov dx,121hLea bx,sumIn al,dxAnd al,03hxlatMov dx,120hOut dx,almov ah,4chInt 21hEnd

Input: PB0, PB1Output:PA0,PA

Input OutputPB1 PB0 PA1(c) PA0(s)0 0 0 00 1 0 11 0 0 11 1 1 0

Page 52: Microprocessor Lab

Program to implement full adder using logic controller

.model smallSum db 0,1,1,2,1,1,1,3.codeMov ax,@dataMov ds,axMov dx,123hMov al,82hOut dx,alMov dx,121hLea bx,sumIn al,dxAnd al,07h ;lower 3 bits of PB are used as inputsxlatMov dx,120hOut dx,al ;lower 2 bits of PA are used as outputsmov ah,4chInt 21hEnd

Page 53: Microprocessor Lab

Details of the assembler directives used

These are nothing but the commands to the assembler. These are not instructions to the 8086 microprocessor. Directive directs the assembler, reserves the memory location or reserves and pre assigns the memory locations according to the direction. Directives are nothing but the pseudo mnemonics. Different directives are required to perform different tasks. Assembler directives enable us to control the way the program is assembled and listed. They do not generate any machine code for execution.

.MODEL directive: this directive helps in providing shortcuts while defining segments. To use this directive, we have to initialize the memory model before defining any segment. The format is ‘.model memory model’. The memory model can be SMALL, MEDIUM, COMPACT or LARGE. The small memory model is used if the program is less than 64kbytes.

.DATA: This is the simplified directive to define the data segment.

.CODE: This is the simplified directive to define the code segment.

The general format is .CODEAll the executable codes must be placed in this segment.

DB directive: Define byte directive: The DB directive is used to define a variable or to set aside one or more storage locations of type byte in memory.

DW directive: Define word directive: The DD directive is used to define a variable of type double word or to reserve memory locations which can be accessed as type double word.

END directive: The END directive is put after the last statement of a program to tell that this is the End of the program module.

ENDP directive: This directive is used along with the name of the procedure to indicate the end of a procedure to the assembler. This directive together with the procedure directive, ‘PROC’ is used to ‘bracket’ a procedure.

PROC directive: The start of the procedure is indicated by the PROC directive. The general format is PROC_NAME PROC[DIST]Where dist is optional and can be either NEAR of FAR. The default value for dist is NEAR. For a procedure that is in the same segment as the calling program, we use the NEAR option.

.STACK directive: This is a simplified segment directive to define the stack segment.The general format is.STACK[size]the default stack size is 1024 bytes..STACK 64 results in reserving 64 bytes fro the stack operations.

Page 54: Microprocessor Lab
Page 55: Microprocessor Lab