33
8051 Addressing modes Addressing mode: a way to address an operand. Operand: the data we are operating upon (in most cases source data). >> a direct address of memory, >> register names, >> any numerical . a simple data move instruction of 8051: MOV A,#6AH The data 6A is the operand, often known as source data. the data 6AH is moved to accumulator A. 5 different ways to execute this instruction 5 addressing modes for 8051.

Microprocessor Notes 2

Embed Size (px)

DESCRIPTION

Microprocessor Notes part 2

Citation preview

Page 1: Microprocessor Notes 2

� 8051 Addressing modes Addressing mode:

a way to address an operand.

Operand:

the data we are operating upon (in most cases source data).

>> a direct address of memory,

>> register names,

>> any numerical .

a simple data move instruction of 8051:

MOV A,#6AH

The data 6A is the operand, often known as source data.

the data 6AH is moved to accumulator A.

5 different ways to execute this instruction

5 addressing modes for 8051.

Page 2: Microprocessor Notes 2

The CPU can access data in various ways, which are called addressing modes >> Immediate addressing mode >> Direct addressing mode >> Register (direct) addressing mode >> Register indirect addressing mode >> Indexed addressing mode

� Immediate Addressing Mode >>transfers an 8-bit data immediately to the accumulator

(destination operand). In general, MOV A, #data

an example. MOV A, #6AH Note: The ‘#’ symbol before 6AH indicates that operand is a data (8 bit). If ‘#’ is not present then the hexadecimal number would be taken as address.

Page 3: Microprocessor Notes 2

(E0H is the address of accumulator).

The opcode for MOV A, # data : 74H. >>saved in program memory at 0202 address.

The data 6AH >>saved in program memory 0203

When the opcode 74H is read, transferring whatever data at the next program memory address (here at 0203) to accumulator A program counter: add 2 and move to 0204 of program memory.

Page 4: Microprocessor Notes 2

� The source operand is a constant >>The immediate data must be preceded by

the pound sign, “#” >> Can load information into any registers,

including 16-bit DPTR register >>> DPTR can also be accessed as two 8-bit

registers, the high byte DPH and low byte DPL

� We can use EQU directive to access immediate data

� We can also use immediate addressing mode to send data to 8051 ports

          

Page 5: Microprocessor Notes 2

� Use registers to hold the data to be manipulated  

 

� The source and destination registers must match in size

>>MOV DPTR,A will give an error

� The movement of data between Rn registers is not allowed

>>MOV R4,R7 is invalid

 

Page 6: Microprocessor Notes 2

� Direct Addressing Mode � It is most often used addressing mode

to locations 30 – 7FH >>The entire 128 bytes of accessed >>The register bank locations by the register

names MOV A,4 ;is same as (direct addressing mode) MOV A,R4 ;which means copy R4 into A (register addressing mode)

Direct

� Contrast this with immediate addressing mode >>There is no “#” sign in the operand

Page 7: Microprocessor Notes 2

� The SFR ( Special Function Register) can be accessed by their names or by their addresses

� The SFR registers have addresses between 80H and FFH >>Not all the address space of 80 to FF is used by SFR >>The unused locations 80H to FFH are reserved and must not be used by the 8051 programmer

>>> another way of addressing an operand.

operand : the address of the data (source data )

an example.

MOV A, 04H

Note: We have not used ‘#’ in direct addressing mode, unlike immediate mode. If we had used ‘#’, the data value 04H would have been transferred to accumulator instead of 1FH.

Page 8: Microprocessor Notes 2

04H : the address of register 4 of register bank#0.

what ever data is stored in register 04H is moved to accumulator.

register 04H holds the data 1FH. So the data 1FH is moved to accumulator.

a 2 byte instruction

the address of the location (04H) where the source data (to be transferred to accumulator) is located

Page 9: Microprocessor Notes 2

Program counter:

increment by 2 and stand in 0204.

The opcode for instruction MOV A, address is E5H.

When the instruction at 0202 is executed (E5H), accumulator is made active and ready to receive data.

program control goes to next address that is 0203

At 04H the control finds the data 1F and transfers it to accumulator

>>SFR Registers and Their Addresses

Page 10: Microprocessor Notes 2
Page 11: Microprocessor Notes 2

� Stack and Direct Addressing Mode

Only direct addressing mode is allowed for pushing or popping the stack >>PUSH A is invalid >> Pushing the accumulator onto the stack must be coded as PUSH 0E0H

Page 12: Microprocessor Notes 2

� Register Direct Addressing Mode use the register name directly (as source operand).

MOV A, R4

registers can take value from R0,R1…to R7.

there are 32 such registers.

accessing 32 registers with just 8 variables to address registers?

the use of register banks:

4 register banks named 0,1,2 and 3.

Each bank has 8 registers named from R0 to R7.

At a time only one register bank can be selected.

Selection of register bank is made possible through a Special Function Register (SFR) named Processor Status Word (PSW).

PSW is an 8 bit SFR where each bit can be programmed.

Page 13: Microprocessor Notes 2

Bits are designated from PSW.0 to PSW.7

Register banks are selected using PSW.3 and PSW.4

These two bits are known as register bank select bits as they are used to select register banks.

the PSW register and the Register Bank Select bits with status:

 

 

data is transferred to accumulator from the register (based on which register bank is selected).

 

Page 14: Microprocessor Notes 2

 

opcode for MOV A, R4 is EC.

04 H is the address of R4 of register bank #0.

0C H is the address location of Register 4 (R4) of register bank #1.

The opcode is stored in program memory address 0202

and when it is executed

the control goes directly to R4 of the respected register bank (that is selected in PSW).

If register bank #0 is selected then

the data from R4 of register bank #0 will be moved to accumulator. (Here it is 2F stored at 04 H).

Page 15: Microprocessor Notes 2

Movement of data (2F) in this case is shown as bold line.

Now please take a look at the dotted line.

Here 2F is getting transferred to accumulator from data memory location 0C H.

Now understand that

Programmers usually get confused with register bank selection.

Also keep in mind that data at R4 of register bank #0 and register bank #1 (or even other banks) will not be same.

So wrong selection of register banks will result in undesired output.

 

� Register Indirect Addressing Mode address of the data (source data to transfer) is given in the register operand.

MOV A, @R0

Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.

Example: If R0 holds the value 20H, and we have a data 2F H stored at the address 20H,

then the value 2FH will get transferred to accumulator after executing this instruction.

Page 16: Microprocessor Notes 2

 

 

the opcode for MOV A, @R0 is E6H.

Assuming that register bank #0 is selected.

the R0 of register bank #0 holds the data 20H.

Program control moves to 20H where it locates the data 2FH and it transfers 2FH to accumulator.

This is a single byte instruction

and the program counter increments 1 and moves to 0203 of program memory.

Note: Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words programmer can must make any instruction either using @R0 or @R1. All register banks are allowed.

� A register is used as a pointer to the data >>Only register R0 and R1 are used for this

purpose

Page 17: Microprocessor Notes 2

>>R2 – R7 cannot be used to hold the address of an operand located in RAM � When R0 and R1 hold the addresses of RAM locations, they must be preceded by the “@” sign

 

� The advantage is that it makes accessing data dynamic rather than static as in direct addressing mode

>>Looping is not possible in direct addressing mode  

 

 

Page 18: Microprocessor Notes 2

� R0 and R1 are the only registers that can be used for pointers in register indirect addressing mode � Since R0 and R1 are 8 bits wide, their use is limited to access any information in the internal RAM � Whether accessing externally connected RAM or on-chip ROM, weneed 16-bit pointer

>>In such case, the DPTR register is used  

 

 

� Indexed Addressing Mode

� Indexed addressing mode is widely used in accessing data elements of look-up table entries located in the program ROM � The instruction used for this purpose is MOVC A,@A+DPTR

Page 19: Microprocessor Notes 2

>>Use instruction MOVC, “C” means code

>>The contents of A are added to the 16-bit register DPTR to form the 16-bit address of the needed data

two examples:

MOVC A, @A+DPTR and MOVC A, @A+PC

where DPTR is data pointer and PC is program counter (both are 16 bit registers). Lets take the first example.

MOVC A, @A+DPTR

The source operand is @A+DPTR

and we know we will get the source data (to transfer) from this location.

It is nothing but adding contents of DPTR with present content of accumulator.

This addition will result a new data which is taken as the address of source data (to transfer).

The data at this address is then transferred to accumulator. Take a look at the picture below.

Page 20: Microprocessor Notes 2

 

 

The opcode for the instruction is 93H.

DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits) and FE is located in DPL (lower 8 bits).

Accumulator now has the value 02H.

A 16 bit addition is performed

and now 01FE H+02 H results in 0200 H.

What ever data is in 0200 H will get transferred to accumulator.

The previous value inside accumulator (02H) will get replaced with new data from 0200H.

New data in the accumulator is shown in dotted line box.

Page 21: Microprocessor Notes 2

This is a 1 byte instruction with 2 cycles needed for execution.

What you infer from that?

The execution time required for this instruction is high compared to previous instructions (which all were 1 cycle).

The other example

MOVC A, @A+PC

works the same way as above example.

The only difference is, instead of adding DPTR with accumulator, here data inside program counter (PC) is added with accumulator to obtain the target address.

� In many applications, the size of program code does not leave any room to share the 64K-byte code space with data

>>The 8051 has another 64K bytes of memory space set aside exclusively for data storage

>>This data memory space is referred to as external memoryand it is accessed only by the MOVX instruction

Page 22: Microprocessor Notes 2

� The 8051 has a total of 128K bytes of memory space >> 64K bytes of code and 64K bytes of data >> The data space cannot be shared between code and data

� The look-up table allows access to elements of a frequently used table with minimum operations   

� In many applications we use RAM locations 30 – 7FH as scratch pad

>> We use R0 – R7 of bank 0 >> Leave addresses 8 – 1FH for stack

usage >> If we need more registers, we

simply use RAM locations 30 – 7FH           

Page 23: Microprocessor Notes 2

� BIT ADDRESSES � Many microprocessors allow program to access registers and I/O ports in byte size only

>>However, in many applications we need to check a single bit � One unique and powerful feature of the 8051 is single-bit operation

>> Single-bit instructions allow the programmer to set, clear, move, and complement individual bits of a port, memory, or register

>> It is registers, RAM, and I/O ports that need to be bit-addressable

� ROM, holding program code for execution, is not bit-addressable

� The bit-addressable RAM

The bit-addressable RAM location are 20H to 2FH

>> These 16 bytes provide 128 bits of RAMbit-addressability, since 16 × 8 = 128

>> 0 to 127 (in decimal) or 00 to 7FH

Page 24: Microprocessor Notes 2

>> The first byte of internal RAM location 20H has bit address 0 to 7H

>> The last byte of 2FH has bit address 78H to 7FH � Internal RAM locations 20-2FH are both byte-addressable and bitaddressable

>> Bit address 00-7FH belong to RAM byte addresses 20-2FH

>> Bit address 80-F7H belong to SFR P0, P1, …   

Page 25: Microprocessor Notes 2

� Bit Addressable RAM

 

Page 26: Microprocessor Notes 2

  

� To avoid confusion regarding the addresses 00 – 7FH

>> The 128 bytes of RAM have the byte addresses of 00 – 7FH can be accessed in byte size using various addressing modes � Direct and register-indirect

>> The 16 bytes of RAM locations 20 – 2FH have bit address of 00 – 7FH

Page 27: Microprocessor Notes 2

>> We can use only the single-bit instructions and these instructions use only direct addressing mode

� Instructions that are used for signal-bit operations are as following

 

� I/O Port Bit Addresses

� While all of the SFR registers are byteaddressable, some of them are also bitaddressable

>> The P0 – P3 are bit addressable

Page 28: Microprocessor Notes 2

� We can access either the entire 8 bits or any single bit of I/O ports P0, P1, P2, and P3 without altering the rest � When accessing a port in a single-bit manner, we use the syntax SETB X.Y

>> X is the port number P0, P1, P2, or P3

>> Y is the desired bit number from 0 to 7 for data bits D0 to D7

>> ex. SETB P1.5 sets bit 5 of port 1 high  

� Notice that when code such as SETB P1.0 is assembled, it becomes SETB 90H >> The bit address for I/O ports >> P0 are 80H to 87H >> P1 are 90H to 97H >> P2 are A0H to A7H >> P3 are B0H to B7H  

Page 29: Microprocessor Notes 2

  

    

Page 30: Microprocessor Notes 2

� Registers Bit- Addressability

� Only registers A, B, PSW, IP, IE, ACC, SCON, and TCON are bit-addressable >> While all I/O ports are bit-addressable � In PSW register, two bits are set aside for the selection of the register banks >> Upon RESET, bank 0 is selected >> We can select any other banks using the bit-addressability of the PSW   

  

Page 31: Microprocessor Notes 2

  

 

   

   

      

Page 32: Microprocessor Notes 2

    

� Using BIT � The BIT directive is a widely used directive to assign the bit-addressable I/O and RAM locations

>> Allow a program to assign the I/O or RAM bit at the beginning of the program, making it easier to modify them  

� Using EQU  

� Use the EQU to assign addresses >>Defined by names, like P1.7 or P2 >>Defined by addresses, like 97H or

0A0H  

Page 33: Microprocessor Notes 2