Professional Documents
Culture Documents
TheCPUcanaccessdatainvariousways,which
arecalledaddressingmodes
Immediate
Register
Direct
RegisterIndirect
Indexed
IMMEDIATEADDRESSINGMODE
Thesourceoperandisaconstant
The immediate data must be preceded by the
poundsign,#
Can load information into any registers,
including16bitDPTRregister
DPTR can also be accessed as two 8bit
registers,thehighbyteDPHandlowbyteDPL
Ex:
MOVA,#25H;load25HintoA
MOVR4,#62;load62intoR4
MOVB,#40H;load40HintoB
MOVDPTR,#4521H;DPTR=4512H
MOVDPL,#21H;Thisisthesame
MOVDPH,#45H;asabove
MOVDPTR,#68975;illegal!!Value>65535
(FFFFH)
WecanuseEQUdirectivetoaccessimmediatedata
CountEQU30
......
MOVR4,#COUNT;R4=1EH
MOVDPTR,#MYDATA;DPTR=200H
ORG200H
MYDATA:DBAmerica
Wecanalsouseimmediateaddressingmodetosenddata
to8051ports
MOVP1,#55H
REGISTERADDRESSINGMODE
Useregisterstoholdthedatatobemanipulated
MOVA,R0;copycontentsofR0intoA
MOVR2,A;copycontentsofAintoR2
ADDA,R5;addcontentsofR5toA
ADDA,R7;addcontentsofR7toA
MOVR6,A;saveaccumulatorinR6
Thesourceanddestinationregistersmustmatchinsize
MOVDPTR,A;willgiveanerror
MOVDPTR,#25F5H
MOVR7,DPL
MOVR6,DPH
ThemovementofdatabetweenRnregistersisnot
allowed
MOVR4,R7;Isinvalid
DIRECTADDRESSINGMODE
DirectaddressingmodeisoftenusedtoaccessRAMlocations
30H7FH
Theentire128bytesofRAMcanbeaccessed
Theregisterbanklocationsareaccessedbytheregistername
MOVA,4;issameas
MOVA,R4;whichmeanscopyR4intoA
Thereisno#signintheoperand
MOVR0,40H;savecontentof40HinR0
MOV56H,A;savecontentofAin56H
Example:
Write a program to toggle P1 a total of 200 times. Use RAM
location 32H to hold your counter value instead of registers R0
R7
Solution:
MOVP1,#55H;P1=55H
MOV32H,#200;loadcountervalueintoRAMloc32H
LOOP1:CPLP1;toggleP1
ACALLDELAY
DJNZ32H,LOOP1;repeat200times
SFRRegistersandTheirAddresses
TheSFR(SpecialFunctionRegister)canbeaccessedbytheir
namesorbytheiraddresses
MOV0E0H,#55H;isthesameas
MOVA,#55h;load55HintoA
MOV0F0H,R0;isthesameas
MOVB,R0;copyR0intoB
TheSFRregistershaveaddressesbetween80HandFFH
Notalltheaddressspaceof80toFFisusedbySFR
Theunusedlocations80HtoFFHarereservedandmustnotbe
usedbythe8051programmer
Example:
Writecodetosend55HtoportsP1andP2,using
(a)theirnames
(b)theiraddresses
(a)MOVA,#55H;A=55H
MOVP1,A;P1=55H
MOVP2,A;P2=55H
(b)FromTable51,P1address=80H;P2address=A0H
MOVA,#55H;A=55H
MOV80H,A;P1=55H
MOV0A0H,A;P2=55H
StackandDirectAddressingMode
Onlydirectaddressingmodeisallowedforpushingorpopping
thestack
PUSHA;isinvalid
Pushingtheaccumulatorontothestackmustbecodedas
PUSH0E0H
Example:
ShowthecodetopushR5andAontothestackandthenpopthem
backthemintoR2andB,whereB=AandR2=R5
Solution:
PUSH05;pushR5ontostack
PUSH0E0H;pushregisterAontostack
POP0F0H;poptopofstackintoB
;nowregisterB=registerA
POP02;poptopofstackintoR2
;nowR2=R6
RegisterIndirectAddressingMode
Aregisterisusedasapointertothedata
OnlyregisterR0andR1areusedforthis
purpose
R2R7cannotbeusedtoholdtheaddressof
anoperandlocatedinRAM
WhenR0andR1holdtheaddressesofRAM
locations,theymustbeprecededbythe@
sign
MOVA,@R0;movecontentsofRAMwhose
;addressisheldbyR0intoA
MOV@R1,B;movecontentsofBintoRAM
;whoseaddressisheldbyR1
Example:
Writeaprogramtocopythevalue55HintoRAM
memorylocations40Hto41Husing
(a)directaddressingmode,
(b) register indirect addressing mode without a
loop,and
(c)withaloop
(a)
MOVA,#55H;loadAwithvalue55H
MOV40H,A;copyAtoRAMlocation40H
MOV41H.A;copyAtoRAMlocation41H
(b)
MOVA,#55H;loadAwithvalue55H
MOVR0,#40H;loadthepointer.R0=40H
MOV@R0,A;copyAtoRAMR0pointsto
INCR0;incrementpointer.
MOV@R0,A;copyAtoRAMR0pointsto
(c)
MOVA,#55H;A=55H
MOVR0,#40H;loadpointer.R0=40H,
MOVR2,#02;loadcounter,R2=3
AGAIN:MOV@R0,A;copy55toRAMR0pointsto
INCR0;incrementR0pointer
DJNZR2,AGAIN;loopuntilcounter=zero
Theadvantageisthatitmakesaccessingdatadynamic
ratherthanstaticasindirectaddressingmode
Loopingisnotpossibleindirectaddressingmode
Example54
Writeaprogramtoclear16RAMlocationsstartingatRAM
address60H
Solution:
CLRA;A=0
MOVR1,#60H;loadpointer.R1=60H
MOVR7,#16;loadcounter,R7=16
AGAIN:MOV@R1,A;clearRAMR1pointsto
INCR1;incrementR1pointer
DJNZR7,AGAIN;loopuntilcounter=zero
Example:
Writeaprogramtocopyablockof10bytesofdatastartingfrom35Htoamemory
Locationstartingfrom60H
Solution:
MOVR0,#35H;sourcepointer
MOVR1,#60H;destinationpointer
MOVR3,#10;counter
BACK:MOVA,@R0;getabytefromsource
MOV@R1,A;copyittodestination
INCR0;incrementsourcepointer
INCR1;incrementdestinationpointer
DJNZR3,BACK;keepdoingfortenbytes
R0andR1aretheonlyregistersthatcanbeusedforpointers
inregisterindirectaddressingmode
SinceR0andR1are8bitswide,theiruseislimitedtoaccess
anyinformationintheinternalRAM
WhetheraccessingexternallyconnectedRAMoronchip
ROM,weneed16bitpointer
Insuchcase,theDPTRregisterisused
IndexedAddressingModeandOn
chipROMAccess
Indexedaddressingmodeiswidelyusedinaccessing
dataelementsoflookuptableentrieslocatedinthe
programROM
Theinstructionusedforthispurposeis
MOVCA,@A+DPTR
UseinstructionMOVC,Cmeanscode
The contents of A are added to the 16bit register
DPTRtoformthe16bitaddressoftheneededdata
Example:
MOVCA,@A+DPTR;getthenextchar
Inthisprogram,assumethatthewordUSAis
burnedintoROMlocationsstartingat200H.And
thattheprogramisburnedintoROMlocations
startingat0.Analyzehowtheprogramworksand
statewhereUSAisstoredafterthisprogramis
run.
MOVR1,A;saveitinR1
Solution:
MOVR2,A;saveitinR2
ORG0000H;burnintoROMstartingat0
Here:SJMPHERE;stayhere
MOVDPTR,#200H;DPTR=200Hlookuptableaddr
;Dataisburnedintocodespacestartingat200H
CLRA;clearA(A=0)
ORG200H
MOVCA,@A+DPTR;getthecharfromcodespace
MYDATA:DBUSA
MOVR0,A;saveitinR0
END;endofprogram
INCDPTR;DPTR=202pointtonextchar
CLRA;clearA(A=0)
MOVCA,@A+DPTR;getthenextchar
INCDPTR;DPTR=201pointtonextchar
CLRA;clearA(A=0)
Thelookuptableallowsaccesstoelementsofa
frequentlyusedtablewithminimumoperations
Example:
BACK:MOVA,P1;GETX
Writeaprogramtogetthexvalue
fromP1andsendx2toP2,
continuously
Solution:
MOVA,@A+DPTR;GETX
;SQAURE
;FROMTABLE
MOVP2,A;ISSUEITTOP2
SJMPBACK;KEEPDOINGIT
ORG0
ORG300H
MOVDPTR,#300H;LOADTABLEADDRESS
MOVA,#0FFH;A=FF
XSQR_TABLE:DB
0,1,4,9,16,25,36,49,64,81
MOVP1,A;CONFIGUREP1INPUTPORT
END
IndexedAddressingModeand
MOVX
registers,wesimplyuse
RAMlocations307FH
BitAddresses
addressable
ROM,holdingprogramcodefor
execution,
is
not
bit
addressable
The bitaddressable
locationare20Hto2FH
These16bytesprovide128bits
ofRAMbitaddressability,since
168=128
RAM
0to127(indecimal)or00to
7FH
Thelastbyteof2FHhasbitaddress78Hto
7FH
InternalRAMlocations202FHarebothbyte
addressableandbitaddressable
Bitaddress007FHbelongtoRAMbyteaddresses
202FH
Bitaddress80F7HbelongtoSFRP0,P1,
Toavoidconfusionregardingtheaddresses007FH
The128bytesofRAMhavethebyteaddressesof007FH
canbeaccessedinbytesizeusingvariousaddressingmodes
Directandregisterindirect
The16bytesofRAMlocations202FHhavebitaddressof00
7FH
Wecanuseonlythesinglebitinstructionsandthese
instructionsuseonlydirectaddressingmode
Instructionsthatareusedforsignalbitoperationsareas
following