






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































ADDRESSING MODES The 8051 Microcontroller and EmbeddedSystems: Using Assembly and C Mazidi, Mazidi and McKinlayChung-Ping Young^ 楊中平
^ We can use EQU directive to access IMMEDIATEimmediate dataADDRESSINGMODE(cont’) ^ We can also use immediate addressingmode to send data to 8051 ports HANEL
^ Use registers to hold the data to bemanipulated REGISTERADDRESSINGMODE ^ The source and destination registersmust match in size^ ¾^ MOV HANEL MOV A,R0^ ;copy contents of R0 into AMOV R2,A^ ;copy contents of A into R2ADD A,R5^ ;add contents of R5 to AADD A,R7^ ;add contents of R7 to AMOV R6,A^ ;save accumulator in R6^ DPTR,A^ will give an errorMOV DPTR,#25F5HMOV R7,DPLMOV R6,DPH The movement of data between Rnregisters is not allowed ¾ MOV^ R4,R7^ is invalid
Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN ACCESSINGMEMORYSFR Registersand TheirAddresses(cont’)^ HANEL
0A8H Interrupt enable controlIE*
0B8H Interrupt priority controlIP*
0B0H Port 3P3*
0A0H Port 2P2*
90H Port 1P1*
80H Port 0P0*
83H High byteDPH
82H Data pointer 2 bytesDPTRLow byteDPL
81H Stack pointerSP
0D0H Program status wordPSW*
0F0H B registerB*
0E0H AccumulatorACC*
Address
Example 5-1 ACCESSING Write code to send 55H to ports P1 and P2, usingMEMORY(a) their names (b) their addresses Solution : SFR Registers (a)^ MOV A,#55Hand TheirAddresses(cont’) HANEL
^ Only direct addressing mode is allowed ACCESSINGfor pushing or popping the stackMEMORY^ ¾^ PUSH AStack andDirectAddressingMode HANEL is invalid ¾ Pushing the accumulator onto the stackmust be coded as^ PUSH 0E0H Example 5-2 Show the code to push R5 and A onto the stack and then pop themback them into R2 and B, where B = A and R2 = R5 Solution: PUSH 05 ;push R5 onto stackPUSH 0E0H ;push register A onto stackPOP 0F0H ;pop top of stack into B;now register B = register APOP 02 ;pop top of stack into R2;now R2=R
Example 5-3 Write a program to copy the value 55H into RAM memory locations ACCESSING40H to 41H using(a) direct addressing mode, (b) register indirect addressing modeMEMORYwithout a loop, and (c) with a loop Solution: Register (a)MOV^ A,#55HIndirectAddressingMode(cont’) HANEL ;load^ A^ with^ value
MOV^ 40H,A^ ;copy^ A^ to
RAM^ location^ 40H MOV^ 41H.A^ ;copy^ A^ to
RAM^ location^ 41H (b)MOV^ A,#55H^ ;load
A^ with^ value^ 55HMOV R0,#40H ;load the^ pointer.^ R0=40HMOV @R0,A ;copy A^ to^ RAM^ R0^ points^
to INC^ R0^ ;increment
pointer.^ Now^ R0=41h MOV^ @R0,A^ ;copy^ A^ to
RAM^ R0^ points^ to (c)MOV^ A,#55H^
;A=55HMOV R0,#40H ;load^ pointer.R0=40H,MOV R2,#02 ;load^ counter,^ R2= AGAIN:^ MOV^ @R0,A^
;copy^55 to^ RAM^ R0^ points
to INC^ R0^ ;increment
R0^ pointer DJNZ^ R2,AGAIN^ ;loop^ until
counter^ =^ zero
^ The advantage is that it makes ACCESSINGaccessing data dynamic rather thanMEMORYstatic as in direct addressing mode^ ¾^ Looping is not possible in directRegisteraddressing modeIndirectAddressing^ Example 5-4 Mode^ Write a program to clear 16 RAM locations starting at RAM address(cont’)60H^ Solution:^ CLR A HANEL
^ R0 and R1 are the only registers that ACCESSINGcan be used for pointers in registerMEMORYindirect addressing modeRegister ^ Since R0 and R1 are 8 bits wide, theirIndirectuse is limited to access anyAddressinginformation in the internal RAMMode(cont’) ^ Whether accessing externallyconnected RAM or on-chip ROM, weneed 16-bit pointer^ ¾^ In such case, the DPTR register is used HANEL
^ Indexed addressing mode is widely ACCESSINGused in accessing data elements ofMEMORYlook-up table entries located in theprogram ROMIndexedAddressing ^ The instruction used for this purpose isMode and^ MOVC A,@A+DPTROn-chip ROM^ ¾^ Use instructionAccess HANEL
MOVC, “C” means code ¾ The contents of A are added to the 16-bitregister DPTR to form the 16-bit addressof the needed data
^ The look-up table allows access to ACCESSINGelements of a frequently used tableMEMORYwith minimum operations^ Example 5-8 Look-up Table^ Write a program to get the x value from P1 and send x(cont’) HANEL
^ In many applications, the size of ACCESSINGprogram code does not leave anyMEMORYroom to share the 64K-byte codespace with dataIndexed^ ¾^ The 8051 has another 64K bytes ofAddressingmemory space set aside exclusively forMode anddata storageMOVX^ ^ This data memory space is referred to asexternal memory and it is accessed only by the^ MOVX HANEL instruction The 8051 has a total of 128K bytes ofmemory space ¾ 64K bytes of code and 64K bytes of data ¾ The data space cannot be shared betweencode and data