Introduction to Micro Controllers Embedded System Design Addressing
Introduction to Micro Controllers & Embedded System Design Addressing Mode Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst. edu A. R. Hurson 1
Introduction to Micro Controllers & Embedded System Design • 8051 supports the following addressing modes: • • • Register Implied Direct Indirect Immediate Relative Absolute Long Indexed A. R. Hurson 2
Introduction to Micro Controllers & Embedded System Design • Register addressing • In 8051 programmer has access to eight “working registers” numbered R 0 to R 7. • Three least significant bits of op. code are used to specify a register. • The 8051 assembly language indicates register addressing with the symbol Rn (0 n 7). A. R. Hurson 3
Introduction to Micro Controllers & Embedded System Design • Register addressing • Example ADD A, R 7 11101111 Op. code register Op. code Instruction format A. R. Hurson Register 4
Introduction to Micro Controllers & Embedded System Design • Register addressing • Example: Is MOV R 4, R 7 a valid instruction? No. Note: We can move data between accumulator and Rn, 0 n 7 but we cannot move data between Rn registers. A. R. Hurson 5
Introduction to Micro Controllers & Embedded System Design • Register addressing • Note: The source and destination registers must match in size. Hence, MOV DPTR, A will give an error. • How to move a large value to registers? See the following: MOV MOV DPTR, #25 F 5 H R 7, DPL R 6, DPH A. R. Hurson 6
Introduction to Micro Controllers & Embedded System Design • Register addressing Note: MOV A, R 7 is the same as MOV A, 7 A. R. Hurson 7
Introduction to Micro Controllers & Embedded System Design • Register addressing • Note: we can move data between accumulator and Rn ( 0 n 7), but we cannot move data between Rn registers directly. • As the result, the following instruction is illegal. MOV R 5 , R 7 A. R. Hurson 8
Introduction to Micro Controllers & Embedded System Design • Implied • Some instructions are referring to specific register such as A (accumulator), DPTR (data pointer), PC (program counter), and B register so address bits are not needed. In another words, op. code indicates the register involved in the operation. • Example INC DPTR MUL AB A. R. Hurson 9
Introduction to Micro Controllers & Embedded System Design • Direct addressing allows access to any on-chip variable or hardware register. An additional byte is appended to the op. code specifying the location to be used. • Example MOV P, A Op. code Direct addressing Instruction format A. R. Hurson 10
Introduction to Micro Controllers & Embedded System Design • Direct addressing • Example MOV R 7, 40 H ; Content of RAM location 40 H is saved in R 7 MOV 56 H, A ; Content of accumulator is saved in RAM location 56 H MOV A, R 2 ; Content of R 2 is saved in accumulator MOV A, #2 ; Accumulator is initialized by 2 MOV A, 55 H ; Content of RAM location 55 H is saved in accumulator A. R. Hurson 11
Introduction to Micro Controllers & Embedded System Design • Indirect addressing • In 8051, R 0 and R 1 may operate as a “pointer” register. In this case, the low order bit of op. code indicates which register. • In 8051 assembly language, indirect addressing is identified as “@” preceding either R 0 or R 1. A. R. Hurson 12
Introduction to Micro Controllers & Embedded System Design • Indirect addressing • Example: Assume R 1 contains 40 H and internal memory address 40 H contains 55 H, then the instruction MOV A, @R 1 moves 55 H into the accumulator Op. code Instruction format A. R. Hurson i R 0 or R 1 13
Introduction to Micro Controllers & Embedded System Design • Indirect addressing • The following code clears RAM locations at addresses 60 H to 7 FH: MOV R 0, #60 H LOOP: MOV @R 0, #0 INC R 0 CJNE R 0, #r 0 H, Loop A. R. Hurson 14
Introduction to Micro Controllers & Embedded System Design • Immediate addressing • In assembly language of 8051, immediate addressing is identified by “#” symbol. The operand may be a numeric constant, a symbolic variable, or an arithmetic expression using constant, symbols, and operators. Assembler calculates the expression and substitutes the result into the instruction. Op. code Immediate data Instruction format A. R. Hurson 15
Introduction to Micro Controllers & Embedded System Design • Immediate addressing • Example: Is MOV DPTR, #68975 a valid instruction? No! 68975 > 65535 is too large Note: We can move data between accumulator and Rn, 0 n 7 but we cannot move data between Rn registers. A. R. Hurson 16
Introduction to Micro Controllers & Embedded System Design • Immediate addressing • Example: MOV A, #12 • Note: There is just one exception when initializing the data pointer, we need a 16 -bit constant, therefore MOV DPTR, #8000 H is a 3 -byte instruction with a 16 -bit constant 8000 H. A. R. Hurson 17
Introduction to Micro Controllers & Embedded System Design • Immediate addressing COUNT EQU 30 MOV R 4, #COUNT ; R 4 = 1 EH MOV DPTR, #MYDATA ; DTPR = 200 H MYDATA: A. R. Hurson ORG DB 200 H “America” 18
Introduction to Micro Controllers & Embedded System Design • Example: Write a program to copy value 55 H into RAM locations 40 H to 45 H using; • Direct addressing mode • Register indirect addressing mode • With a loop A. R. Hurson 19
Introduction to Micro Controllers & Embedded System Design Direct addressing mode MOV MOV MOV A. R. Hurson A, #55 H 40 H, A 41 H, A 42 H, A 43 H, A 44 H, A ; Load accumulator with 55 H ; Load accumulator to location 40 H ; Load accumulator to location 41 H ; Load accumulator to location 42 H ; Load accumulator to location 43 H ; Load accumulator to location 44 H 20
Introduction to Micro Controllers & Embedded System Design Register indirect addressing mode MOV MOV INC MOV INC A. R. Hurson A, #55 H R 0, #40 H @R 0, A R 0 @R 0, A R 0 ; Load accumulator with 55 H ; Load the pointer ; Load A to location pointed by R 0 ; Increment pointer ; Load A to location pointed by R 0 ; Increment pointer 21
Introduction to Micro Controllers & Embedded System Design With a loop MOV MOV AGAIN: MOV INC DJNZ A. R. Hurson A, #55 H R 0, #40 H R 2, #05 @R 0, A R 0 R 2, AGAIN ; Load accumulator with 55 H ; Load the pointer ; Load the loop counter ; Load A to location pointed by R 0 ; Increment pointer ; Repeat until counter is zero 22
Introduction to Micro Controllers & Embedded System Design Analyze the following program HERE: ORG MOV CLR MOVC MOV INC MOVC MOV SJMP ORG MYDATA: DB END A. R. Hurson 0000 H DPTR, #200 H A A, @A+DPTR R 0, A DPTR A A, @A+DPTR R 1, A DPTR A, @A+DPTR R 2, A HERE 200 H “USA” ; Load the pointer ; Load the loop counter ; Infinite loop ; Data is stored at address 200 H ; End of program 23
Introduction to Micro Controllers & Embedded System Design Analyze the following program BACK: HERE: ORG MOV MOV CLR MOVC MOV INC DJNZ SJMP ORG MYDATA: DB END A. R. Hurson 0000 H DPTR, #MYDATA R 0, #40 H R 2, #7 A A, @A+DPTR @R 0, A DPTR R 0 R 2 , BACK HERE 250 H “AMERICA” ; Data is stored at address 250 H ; End of program 24
Introduction to Micro Controllers & Embedded System Design Analyze the following program BACK: HERE: ORG MOV CLR MOVC JZ MOV INC SJMP ORG MYDATA: DB END A. R. Hurson 0000 H DPTR, #MYDATA R 0, #40 H A A, @A+DPTR HERE @R 0, A DPTR R 0 BACK HERE 250 H “AMERICA”, 0 ; Data is stored at address 250 H ; End of program 25
Introduction to Micro Controllers & Embedded System Design Analyze the following program BACK: XSQRT: A. R. Hurson ORG MOV MOV MOVC MOV SJMP 0000 H DPTR, #300 H A, #0 FFH P 1, A A, P 1 A, @A+DPTR P 2, A BACK ORG DB END 300 H 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 26
Introduction to Micro Controllers & Embedded System Design • Relative addressing • This addressing mode is used with certain jump instructions. A relative address is an 8 -bit signed value which is added to the contents of the program counter to form address of the next instruction to be fetched and executed. • Naturally, the range for jump is -128 to +127 locations. Op. code Relative offset Instruction format A. R. Hurson 27
Introduction to Micro Controllers & Embedded System Design • Relative addressing • Example: Assume label THREE represents an instruction at location 1040 H and instruction SJMP THREE is in memory location 1000 H and 1001 H, then the assembler assigns a relative offset of 3 EH as byte-two of the instruction since 1002 H + 3 EH = 1040 H A. R. Hurson 28
Introduction to Micro Controllers & Embedded System Design • Calculating off set for Relative addressing 010 A 0109 0108 0107 5 0106 4 0105 3 0104 2 0103 1 0102 A. R. Hurson 0101 05 0100 80 00 FF PC SJMP 0107 H 29
Introduction to Micro Controllers & Embedded System Design • Calculating off set for Relative addressing 2043 2042 -1 2041 F 6 -2 2040 80 -3 PC SJMP 2038 H -4 -5 -6 -7 -8 -9 A. R. Hurson -10 30
Introduction to Micro Controllers & Embedded System Design • Absolute addressing is just used with ACALL and AJMP instructions. These 2 -byte instructions allow branching within the current 2 K page of code memory by providing the 11 least significant bits of destination address in op. code (i. e. , A 10 -A 8 and byte 2 of instruction A 7 -A 0). A 10 -A 8 Op. code A 7 -A 0 Instruction format A. R. Hurson 31
Introduction to Micro Controllers & Embedded System Design • Absolute addressing • The upper five bits of the destination address are the current value of the upper five bits of program counter. Therefore, the next instruction after the branch and the destination instruction must be within the same 2 K page. A. R. Hurson 32
Introduction to Micro Controllers & Embedded System Design • Absolute addressing FFFF 2 K page 31 F 800 32 2 K pages. Within each the upper 5 address bits are common. 17 FF 1000 2 K page 2 0 FFF 0800 07 FF 2 K page 1 2 K page 0 0000 A. R. Hurson 33
Introduction to Micro Controllers & Embedded System Design • Long addressing is used with LCALL and LJMP instructions. These instruction are 3 bytes long. Bytes 2 and 3 form the 16 -bit destination address. Op. code A 15 -A 8 A 7 -A 0 Instruction format A. R. Hurson 34
Introduction to Micro Controllers & Embedded System Design • Index addressing uses a base register (either the program counter or the data pointer) and an offset (the accumulator) in forming the effective address for JMP or MOVC instructions. • Example: MOVC A, @A+PC PC or DPTR + ACC = effective address Base register A. R. Hurson Offset 35
Introduction to Micro Controllers & Embedded System Design • Special Function Registers (SFR) A. R. Hurson Symbol Name Address ACC* Accumulator 0 E 0 H B* B register 0 F 0 H PSW* Program status word 0 D 0 H SP Stack Pointer 81 H DPTR DPL DPH Data Pointer (2 bytes) Low byte High byte 82 H 83 H P 0 * Port 0 80 H P 1* Port 1 90 H P 2 * Port 2 0 A 0 H P 3* Port 3 o. B 0 H IP* Interrupt priority control 0 B 8 H 36
Introduction to Micro Controllers & Embedded System Design • Special Function Registers (SFR) A. R. Hurson Symbol Name Address IE Interrupt enable control 0 A 0 H TMOD Timer/counter mode control 89 H TCON* Timer/counter control 88 H T 2 CON* Timer/counter 2 control 0 C 8 H T 2 MOD Timer/counter mode control 0 C 9 H TH 0 Timer/counter 0 high byte 8 CH TL 0 Timer/counter 0 low byte 8 AH TH 1 Timer/counter 1 high byte 8 DH TL 1 Timer/counter 1 low byte 8 BH 37
Introduction to Micro Controllers & Embedded System Design • Special Function Registers (SFR) Symbol Name Address TH 2 Timer/counter 2 high byte 0 CDH TL 2 Timer/counter 2 low byte 0 CCH RCAP 2 H T/C capture register high byte 0 CBH RCAP 2 L T/C capture register low byte 0 CAH SCON* Serial control 98 H SBUF Serial data buffer 99 H PCON Power control 87 H * Bit addressable A. R. Hurson 38
- Slides: 38