UMBC CMSC 391 -- Programming Microcontrollers  


Addressing

The following is extracted from the the Phillips Semiconductors' 80C51_FAM_ARCH_1.pdf, which contains Chapter 1, 80C51 Family Architecture of an unknown document posted on the Internet. QUOTE

Indirect addressing

The instruction specifies a register which contains the address of the operand. Both internal and external RAM can be indirectly addressed.

The address register for 8-bit addresses can be R0 or R1 of the selected bank, or the Stack Pointer. The address register for 16-bit addresses can only be the 16-bit "data pointer" register, DPTR.

Register addressing

The register banks, containing registers R0 through R7, can be accessed by certain instructions which car a 3-bit register specification within the opcode of the instruction. Instructions that access the registers in this way are code efficient, since this mode eliminates an address byte. When the instruction is executed, one of the eight registers in the selected bank is accessed. One of four banks is selected at execution time by the two bank select bits in the PSW.

Register-Specific Instructions

Some instructions are specific to a certain register. For example some instructions always operate on the Accumulator, or Data Pointer, etc., so no address byte is needed to point to it. The opcode itself does that. Instructions that refer to the Accumulator as A assemble as accumulator specific opcodes.

Immediate Constants

The value of a constant can follow the opcode in Program Memory. For example, MOV A, #100 loads the Accumulator with the decimal number 100. The same number could be specified in hex digits as 64H.

Indexed Addressing

Only program Memory can be accessed with indexed addressing, and it can only be read. This addressing mode is intended for look-up tables in Program Memory. A 16-bit base register (either DPTR or the Program Counter) points to the base of the table, and the Accumulator is set up with the table entry number.

The address of the table entry in Program Memory is formed by adding the Accumulator to the base pointer.

Another type of indexed addressing is used in the "case jump" instruction. In this case the destination address of a jump instruction is computed as the sum of the base pointer and the Accumulator data.

UNQUOTE

All Manuals Say The Same Thing, NOT

Programming and Interfacing the 8051 Microcontroller, Sencer Yeralan and Ashutosh Ahluwalia, Addison Wesley, have a different view on this with examples. Note that this code uses the MCS-51 assembler, which has a slightly different syntax for things like assembler directives.

Immediate addressing (immediate constant addressing)

        mov     a, #0h        ; put 0 into the accumulator
        mov     a, #11h       ; put 11h into the accumulator
        mov     a, #27        ; put 27 (decimal) = 1bh into the accumulator
  

Direct addressing

The direct addressing mode refers to specifying an internal data register or an SFR by its address. Addresses in the interval [80h..0FFh] refer to the SFRs. For example, 90h is the address of Port 1. Writing to the SFR 90h effectively sends out a signal from Port 1. Similarly, reading the SFR 90h effectively inputs the signals on Port 1 pins.
        mov     90h, a        ; copy the accumulator contents to SFR 90h (Port 1)
  

I received an email from pjrc.com about this: QUOTE I would highly recommend advising students to use symbolic constants, rather than numbers, when they use direct addressing modes. UNQUOTE


Another example: QUOTE

Connect Port 4 bits to eight pushbuttons.  Type, assemble, and download the following
program.


Port4   equ     0E8h           ; Port 4
Port1   equ     090h           ; Port 1

        org     8000h          ; set the origin
        mov     a, Port4       ; copy the contents of Port 4
        mov     Port1, a       ; copy the accumulator contents to Port 1
        ljmp    8000h          ; repeat

This program reads the pushbuttons connected to Port 4 and copies their states 
to the LEDs connected to Port 1 bits.  Press the RESET button to abort the program
      
  
UNQUOTE

Register Addressing Mode

The register addressing mode refers to either the source or the destination being one of the eight registers of the currently selected register bank. The following code selects register bank 2 and places the value of the accumulator and the B register into registers 0 and 7. Register addressing is used to specify the destinations of the move instructions.
        org     8000h          ; set the origin
        mov     PSW, #10h      ; select register bank 2
        mov     R0, a          ; copy the contents of the accumulator into register 0
        mov     R7, b          ; copy the contents of the B register into register 7
        ljmp    0              ; return to the monitor
  

Register-Specific Addressing Mode

Some instructions are specific to the registers used. No further addressing is required. Recalling that the SFR E0h is the accumulator, consider the following tow move instructions
        org     8000h          ; set the origin
        mov     a, #1          ; move the constant 1 into the accumulator
        mov     0E0h, 1        ; mov the constant 1 into SFR E0h
        ljmp    0              ; return to the monitor
  

Type and assemble the program. Then view the list file (with the .LST extension). Observe that the first move instruction is a 2-byte instruction, consisting of the bytes 74 and 01 (in hexadecimal). Note that the first 74 is interpreted as "take the following byte and place into the accumulator." That is the accumulator being the destination is implicitly coded in the instruction.

The second instruction moves the constant 1 into the SFR E0. This SFR is the accumulator. Therefore, effectively the two move instructions accomplish the same task. This instruction consists of three bytes: 75, E0, and 01, in hexadecimal. The first byte, 75, is interpreted as "of the following 2 bytes, the first is the address of a register, into which put the second byte." That is, the instruction "75" does not implicitly specify a target, rather it indicates that the address of the target is following.

Register Indirect Addressing Mode

The address of the source or destination is not given explicitly. Instead the content of a register is used as the target address. Two registers, R0 and R1, of the currently selected register bank are used to specify the addresses.
        org     8000h               ; set the origin
        mov     PSW, #0             ; select register bank 0
        mov     r0, #78             ; move 78h into register 0
        mov     @r0,#1              ; set the register whose address is specified
                                    ; in the R0 register to to the constant 1
        ljmp    0
  

There is also register indirect addressing mode instructions that transfer data between the accumulator and external data memory.
        org     8000h               ; set the origin
        mov     a, #1               ; put the constant 1 into the accumulator

        mov     DPTR, #9000h        ; set the data pointer to 9000h
        movx    @DPTR, a            ; copy the accumulator into external data
                                    ; at the address specified by the data
                                    ; pointer.  In this case, the 1 is copied
                                    ; into external memory location 9000h

        mov     DPTR, #9001h        ; set the data pointer to 9001h
        movx    a, @DPTR            ; copy the contents of external data
                                    ; memory at location 9001 into the 
                                    ; accumulator

        mov     a, #1               ; put the constant 1 into the accumulator
        mov     R0, #0              ; put 0 into register 0 of the current
                                    ; bank

        ljmp    0
  

Register Indexed Addressing Mode

A useful application of this powerful addressing mode is implementing look-up tables. In this mode, the source or destination address is obtained by adding the value held in the accumulator to the base address. The base address may either be the data pointer DPTR, or the program counter PC.

The following program evaluates the pattern of a seven-segment display given a single hexadecimal digit. It is assumed that the accumulator holds a value between 0 and Fh (15). The pattern is intended to be output from a port that is connected to a seven-segment display. The least significant bit is connected to segment1, and bit 6 is connected to segment g.

        org     8000h                ; set the origin
        mov     DPTR, #8100h         ; put the table base address into the 
                                     ; data pointer
        mov     a, #0                ; get the pattern 0
        movc    a, @a+DPTR           ; a contains the pattern for 0

        ljmp    0                    ; return to the monitor

        org     8100h                ; the table starts at 8100h
        db      0c0h                 ; 0
        db      0f9h                 ; 1
        db      0a4h                 ; 2
        db      0b0h                 ; 3
        db      99h                  ; 4
        db      92h                  ; 5
        db      82h                  ; 6
        db      0f8h                 ; 7
        db      80h                  ; 8
        db      90h                  ; 9
        db      88h                  ; a
        db      83h                  ; b
        db      0c6                  ; c
        db      0a1h                 ; d
        db      86h                  ; e
        db      8eh                  ; f
  


©2004, Gary L. Burt