You are on page 1of 4

Simple Programs in 8051 assembly language

Statement 1: - exchange the content of FFh and FF00h Solution: - here one is internal memory location and other is memory external location. so first the content of ext memory location FF00h is loaded in acc. then the content of int memory location FFh is saved first and then content of acc is transferred to FFh. now saved content of FFh is loaded in acc and then it is transferred to FF00h. Mov dptr, #0FF00h ; take the address in dptr Movx a, @dptr ; get the content of 0050h in a Mov r0, 0FFh ; save the content of 50h in r0 Mov 0FFh, a ; move a to 50h Mov a, r0 ; get content of 50h in a Movx @dptr, a ; move it to 0050h Statement 2: - store the higher nibble of r7 in to both nibbles of r6 Solution: first we shall get the upper nibble of r7 in r6. Then we swap nibbles of r7 and make O operation with r6 so the upper and lower nibbles are duplicated Mov a, r7 ; get the content in acc Anl a, #0F0h ; mask lower it Mov r!, a ; send it to r! "wap a ; xchange #pper and lower ni les of acc $rl a, r! ; $% operation Mov r!, a ; finall& load content in r! Statement 3: - treat r6!r7 and r"!r# as two $6 bit registers. %erform subtraction between them. &tore the result in '0h (lower byte) and '$h (higher byte). Solution: - first we shall clear the carry. Then subtract the lower bytes afterward then subtract higher bytes. 'lr c ; clear carr& Mov a, r( ; get first lower &te "# a, r! ; s# tract it with other Mov )0h, a ; store the res#lt Mov a, r5 ; get the first higher &te "# a, r7 ; s# tract from other Mov )*h, a ; store the higher &te

Statement 4: - divide the content of r0 by r$. &tore the result in r' (answer) and r* (reminder). Then restore the original content of r0. Solution:-after getting answer to restore original content we have to multiply answer with divider and then add reminder in that. Mov a, r0 ; get the content of r0 and r* Mov , r* ; in register A and + ,iv a ; divide A & + Mov r), a ; store res#lt in r) Mov r-, ; and reminder in rMov , r* ; again get content of r* in + M#l a ; m#ltipl& it & answer Add a, r; add reminder in new answer Mov r0, a ; finall& restore the content of r0 Statement 5: - transfer the block of data from '0h to *0h to external location $0'0h to $0*0h. Solution: - here we have to transfer $0 data bytes from internal to external +,. &o first- we need one counter. Then we need two pointers one for source second for destination. Mov r7, #0Ah ; initiali.e co#nter & *0d Mov r0, #)0h ; get initial so#rce location Mov dptr, #*0)0h ; get initial destination location /xt0 Mov a, @r0 ; get first content in acc Movx @dptr, a ; move it to external location 1nc r0 ; increment so#rce location 1nc dptr ; increase destination location ,2n. r7, nxt ; decrease r73 if .ero then over otherwise move next Statement 6: - find out how many e.ual bytes between two memory blocks $0h to '0h and '0h to *0h. Solution: - here we shall compare each byte one by one from both blocks. /ncrease the count every time when e.ual bytes are found Mov r7, #0Ah ; initiali.e co#nter & *0d Mov r0, #*0h ; get initial location of lock* Mov r*, #)0h ; get initial location of lock) Mov r!, #00h ; e4#al &te co#nter3 "tarts from .ero /xt0 Mov a, @r0 ; get content of lock * in acc Mov , a ; move it to +

Mov a, @r* ; get content of lock ) in acc '2ne a, , nomatch ; compare oth if e4#al 1nc r! ; increment the co#nter /omatch0 inc r0 ; otherwise go for second n#m er 1nc r* d2n. r7, nxt ; decrease r73 if .ero then over otherwise move next Statement 7: - given block of $00h to '00h. Find out how many bytes from this block are greater then the number in r' and less then number in r*. &tore the count in r". Solution: - in this program- we shall take each byte one by one from given block. 0ow here two limits are given higher limit in r* and lower limit in r'. &o we check first higher limit and then lower limit if the byte is in between these limits then count will be incremented. Mov dptr, #0*00h ; get initial location Mov r7, #0FFh ; co#nter Mov r(, #00h ; n#m er co#nter Mov )0h, r) ; get the #pper and lower limits in Mov )*h, r; )0h and )*h /xt0 Movx a, @dptr ; get the content in acc '2ne a, )*h, lower ; check the #pper limit first "2mp o#t ; if n#m er is larger 5ower0 2nc o#t ; 2#mp o#t '2ne a, )0h, limit ; check lower limit "2mp o#t ; if n#m er is lower 5imit0 2c o#t ; 2#mp o#t 1nc r( ; if n#m er within limit increment co#nt $#t0 inc dptr ; get next location ,2n. r7, nxt ; repeat #ntil lock completes Statement 8:-count number of interrupts arriving on external interrupt pin /0T$. &top whencounter overflows and disable the interrupt. 1ive the indication on pin%0.0 Solution: -as we know whenever interr#pt occ#rs the 6' 2#mps to one partic#lar location where it7s 1"% is written3 "o we have to 2#st write one 1"% that will do the 2o Movr), #00h ; initiali.e the co#nter Movie, #8(h ; ena le external interr#pt * 9ere0 "2mp here ; contin#o#s loop $rg 00*-h 1ncr) ; interr#pt *location ; increment the co#nt

'2ner), #00h, o#t ; check whether it overflows Movie, #00h ; if &es then disa le interr#pt 'lr p030 ; and give indication $#t 0 reti ; otherwise keep co#nting Statement 9: -continuously scan port %0. /f data is other then FFh write a subroutine that will multiply it with $0d and send it to port %$ Solution: -here we have to use polling method. 2e shall continuously pole port %0 if there is any data other then FFh. /f there is data we shall call subroutine Agin0 Mov p0, #0ffh 5oop0 Mova, p0 '2ne a, #0FFh, dat "2mp loop ,at0 acall m#lti; "2mp agin M#lti Mov ,#*0d M#l a Mov p*, a %et ; initiali.e port 60 as inp#t port ; get the data in acc ; compare it with FFh ; if same keep looping if different call s# ro#tine ; again start polling ; load *0d in register + ; m#ltipl& it with received data ; send the res#lt to 6* ;ret#rn to main program

You might also like