8051 Programming Examples
8051 Programming Examples
iteALPt
ocl
ear16RAM l
ocat
ionsst
art
ing
fr
om 60h
MOV R0,
#60H
MOV R1,
#16
CLR A
BACK:
MOV @R0,
A
I
NC R0
DJNZ R1,
BACK
End
2) Wri
teALPtocopyadatabl
ockof20byt
es
stor
edinext
ernal
RAM fr
om 2000ht
oint
ernal
RAM fr
om 40H
MOVDPTR,
#2000H
MOVR0,
#40H
MOVR2,
#20
BACK:
MOVX A,
@DPTR
MOV @R0,
A
I
NCDPTR
I
NCR0
DJNZ R2,
BACK
End
3) HW:Wri
teALPtocopyadatablockof15byt
es
stor
edinI
NTERNALRAM from 30ht oi
nter
nal
RAM fr
om 50Hi
nrever
seorder
.
Pr
ogr
am:
MOV R0,
#30H
MOV R1,
#5EH
MOV R2,
#15
BACK:
MOV A,
@R0
MOV @R1,
A
I
NC R0
DEC R1
DJNZ R2,
BACK
END
4) Writ
eALPtofindt
helar
gestnumberf
rom adata
blockof10byt
esstor
edinint
ernal
RAM fr
om 30H.
Storether
esul
tat70H.
Pr
ogr
am:
MOV R1,
#30H
MOV R2,
#09H
MOV A,
@R1
BACK:
INC R1
MOV B,
@R1
CJNE A,
B,NEXT
NEXT:
JNC DOWN;
MOV A,
B;i
fA<B,
THENCY=1;
lar
geroutoft
he
twonosi
sinr
egi
sterB,
sot
ransf
erl
argernoi
n
RegA
DOWN:
DJNZ R2,
BACK;
MOV 70H,
A
END
NOTE:
-Tof
indsmall
estnor
epl
aceJNCbyJCi
n
t
heaboveprogr
am
5) Writ
eALPt ocountthenumberofl
ogi
c1’
s
presenti
na8bi tdatastor
edi
nexter
nal
RAM at
3000h.Stor
ether esul
tinR7r
egi
ster
.
Pr
ogr
am:
6) Writ
eALPtocountno.ofevendatabyt
es
presenti
nablockof30databytesst
oredi
n
int
ernalRAM of8051f
rom 40h.Stor
etheresul
tat
70H.
I
fLSBbi
tofanynoi
szer
othenno.i
sev
en
I
fLSBbi
tofanynoi
s1t
henno.i
sodd
MOV R0,
#40H
MOV R2,
#30
MOV R4,
#00
Back:
MOV A,
@R0
JB Acc.
0,Down
I
NC R4
Down:
INC R0
DJNZ R2,
Back
MOV A,
R4
MOV70H,
A
End
HW ASSI
GNMENT:
(
I) Wr i
teALPtocountno.ofodddat abyt
es
presenti
nabl ockof30databytesst
oredi
n
i
nternalRAM of8051from 40h.Stor
ethe
resul
tat75H.
(
II
) Inasemest erastudenthast otake6
courses.Themar ksscoredbyt hestudent
i
nt hesesubject
sar estoredinint
ernalRAM
of8051f r
om 30Honwar ds.Findthe
averagemar ks&out putthem onport1
(
II
I) Assumet hatt hereare5BCDnumber s
storedi
ninternalRAM of8051f rom 50H.
Wr i
teaprogram t of
indthesum ofallthese
numbers.Storet heBCDr esul
tinregi
sters
R6&R7
7) Assumet
hati
nter
nal
RAM l
ocat
ionsf
rom 30ht
o
34Hcontai
ndail
ytemper at
ureval
uesfor5day
s.
Sear
chtoseeifanyv al
ueisequalt
o42.If42
exi
stsi
nthetabl
e,thengiveit
’sl
ocati
ontor
egist
er
Bother
wisemakeB=0
MOV B,
#00H
MOV R1,
#30H
MOV R2,
#05H
MOV A,
#42
Back:
CJNE A,
@R1,
Down
MOV B,
R1
SJMP Her
e
Down:
INC R1
DJNZ R2,
Back
Her
e:NOP
END