Embedded Systems 简明教程

Embedded Systems - Addressing Modes

addressing mode 表示如何寻址给定的存储器位置。有五种不同的方式或五种寻址模式可用于执行此指令,如下所示 −

An addressing mode refers to how you are addressing a given memory location. There are five different ways or five addressing modes to execute this instruction which are as follows −

  1. Immediate addressing mode

  2. Direct addressing mode

  3. Register direct addressing mode

  4. Register indirect addressing mode

  5. Indexed addressing mode

Immediate Addressing Mode

让我们从一个示例开始。

Let’s begin with an example.

MOV A, #6AH

一般而言,我们可以编写,

In general, we can write,

MOV A, #data

它被称为 immediate ,因为 8 位数据即刻传输到累加器(目标操作数)。

It is termed as immediate because 8-bit data is transferred immediately to the accumulator (destination operand).

以下插图描述了上述指令及其执行。操作码 74H 保存于 0202 地址。数据 6AH 保存于程序存储器中的 0203 地址。在读取操作码 74H 后,下一个程序存储器地址中的数据将传输到累加器 A(E0H 是累加器的地址)。由于该指令为 2 字节并且在一个周期内执行,程序计数器将递增 2,并将指向程序存储器的 0204。

The following illustration describes the above instruction and its execution. The opcode 74H is saved at 0202 address. The data 6AH is saved at 0203 address in the program memory. After reading the opcode 74H, the data at the next program memory address is transferred to accumulator A (E0H is the address of accumulator). Since the instruction is of 2-bytes and is executed in one cycle, the program counter will be incremented by 2 and will point to 0204 of the program memory.

immidiate addressing mode

Note' symbol before 6AH indicates that the operand is a data (8 bit). In the absence of ' ,十六进制数字将作为地址。

Note − The '' symbol before 6AH indicates that the operand is a data (8 bit). In the absence of '', the hexadecimal number would be taken as an address.

Direct Addressing Mode

这是另一种寻址操作数的方式。在此,数据地址(源数据)提供为操作数。让我们举个例子。

This is another way of addressing an operand. Here, the address of the data (source data) is given as an operand. Let’s take an example.

MOV A, 04H

寄存器库#0(第 4 个寄存器)的地址为 04H。当执行 MOV 指令时,存储于寄存器 04H 中的数据将移动到累加器。由于寄存器 04H 保存数据 1FH,因此 1FH 移动到累加器。

The register bank#0 (4th register) has the address 04H. When the MOV instruction is executed, the data stored in register 04H is moved to the accumulator. As the register 04H holds the data 1FH, 1FH is moved to the accumulator.

Note − 我们未使用 ' in direct addressing mode, unlike immediate mode. If we had used ' ,数据值 04H 会传输到累加器,而不是 1FH。

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 the accumulator instead of 1FH.

现在,看一看以下插图。它显示了指令如何执行。

Now, take a look at the following illustration. It shows how the instruction gets executed.

direct addressing mode

如上图所示,这是一条 2 字节指令,需要 1 个周期才能完成。PC 将递增 2,并将指向 0204。指令 MOV A, address 的操作码为 E5H。当执行 0202 处的指令(E5H)时,累加器激活并准备接收数据。然后 PC 转到下一个地址 0203 并查找位置 04H 的地址,即源数据(传送到累加器)所在的位置。在 04H 处,控制找到数据 1F 并将其传输到累加器,因此完成执行。

As shown in the above illustration, this is a 2-byte instruction which requires 1 cycle to complete. The PC will be incremented by 2 and will point to 0204. The opcode for the instruction MOV A, address is E5H. When the instruction at 0202 is executed (E5H), the accumulator is made active and ready to receive data. Then the PC goes to the next address as 0203 and looks up the address of the location of 04H where the source data (to be transferred to accumulator) is located. At 04H, the control finds the data 1F and transfers it to the accumulator and hence the execution is completed.

Register Direct Addressing Mode

在此寻址模式中,我们直接使用寄存器名称(作为源操作数)。让我们尝试在示例中进行理解。

In this addressing mode, we use the register name directly (as source operand). Let us try to understand with the help of an example.

MOV A, R4

一次,寄存器可以获取从 R0 到 R7 的值。有 32 个这样的寄存器。为了使用 32 个寄存器仅使用 8 个变量来寻址寄存器,使用寄存器库。有 4 个寄存器库从 0 到 3 命名。每个库包含 8 个寄存器从 R0 到 R7 命名。

At a time, the registers can take values from R0 to R7. There are 32 such registers. In order to use 32 registers with just 8 variables to address registers, register banks are used. There are 4 register banks named from 0 to 3. Each bank comprises of 8 registers named from R0 to R7.

一次,可以选取单个寄存器库。通过名为 Special Function Register (SFR)的 Processor Status Word (PSW)可以选取寄存器库。PSW 是一个 8 位 SFR,其中每位可以根据需要进行编程。位从 PSW.0 指定到 PSW.7。PSW.3 和 PSW.4 用于选取寄存器库。

At a time, a single register bank can be selected. Selection of a 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 as required. Bits are designated from PSW.0 to PSW.7. PSW.3 and PSW.4 are used to select register banks.

现在,看一看以下插图,从而清晰了解它是如何工作的。

Now, take a look at the following illustration to get a clear understanding of how it works.

register direct mode

操作码 EC 用于 MOV A,R4。操作码存储于地址 0202 处,当执行它时,控制直接转到所选 PSW 中的寄存器库的 R4(即 PSW 中选择的)。如果选取寄存器库#0,则寄存器库#0 的 R4 中的数据将移动到累加器。此处 2F 存储于 04H。04H 表示寄存器库#0 的 R4 的地址。

Opcode EC is used for MOV A, R4. The opcode is stored at the 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 the accumulator. Here 2F is stored at 04H. 04H represents the address of R4 of register bank #0.

数据(2F)移动以粗体突出显示。2F 从数据存储器位置 0CH 传输到累加器,并显示为虚线。0CH 是寄存器库#1 的寄存器 4(R4)的地址位置。上述指令为 1 字节,需要 1 个周期才能完全执行。这意味着,你可以使用寄存器直接寻址模式来保存程序存储器。

Data (2F) movement is highlighted in bold. 2F is getting transferred to the accumulator from data memory location 0C H and is shown as dotted line. 0CH is the address location of Register 4 (R4) of register bank #1. The instruction above is 1 byte and requires 1 cycle for complete execution. What it means is, you can save program memory by using register direct addressing mode.

Register Indirect Addressing Mode

在此寻址模式中,数据地址存储于操作数中的寄存器内。

In this addressing mode, the address of the data is stored in the register as operand.

MOV A, @R0

此处 R0 中的值被视为地址,其中包含要传输到累加器的值。 Example : 如果 R0 的值为 20H,并且数据 2FH 存储在地址 20H 中,那么在执行此指令后,值 2FH 将传输到累加器中。请参见以下说明:

Here the value inside R0 is considered as an address, which holds the data to be transferred to the accumulator. Example: If R0 has the value 20H, and data 2FH is stored at the address 20H, then the value 2FH will get transferred to the accumulator after executing this instruction. See the following illustration.

register indirect mode

因此 MOV A, @R0 的操作码为 E6H。假设选择了寄存器组 #0,则寄存器组 #0 的 R0 保存数据 20H。程序控制移动到 20H,它在其中找到数据 2FH 并将其传输到 2FH,累加器。这是一条 1 字节指令,程序计数器递增 1 并且移动到程序存储器的 0203。

So the opcode for MOV A, @R0 is E6H. Assuming that the 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 the accumulator. This is a 1-byte instruction and the program counter increments by 1 and moves to 0203 of the program memory.

Note - 仅允许 R0 和 R1 形成寄存器间接寻址指令。换句话说,程序员可以使用 @R0 或 @R1 创建指令。所有寄存器组均被允许。

Note − Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words, the programmer can create an instruction either using @R0 or @R1. All register banks are allowed.

Indexed Addressing Mode

我们将举两个示例以理解索引寻址模式的概念。请看以下说明:

We will take two examples to understand the concept of indexed addressing mode. Take a look at the following instructions −

MOVC A, @A+DPTR

MOVC A, @A+DPTR

and

MOVC A, @A+PC

MOVC A, @A+PC

其中 DPTR 是数据指针,PC 是程序计数器(两者都是 16 位寄存器)。考虑第一个示例。

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

MOVC A, @A+DPTR

源操作数为 @A+DPTR。它包含此位置的源数据。在这里,我们正在将 DPTR 的内容与累加器的当前内容相加。此加法将给出一个新地址,该地址是源数据的地址。然后将此地址指出的数据传输到累加器。

The source operand is @A+DPTR. It contains the source data from this location. Here we are adding the contents of DPTR with the current content of the accumulator. This addition will give a new address which is the address of the source data. The data pointed by this address is then transferred to the accumulator.

indexed mode

操作码为 93H。DPTR 的值为 01FE,其中 01 位于 DPH(高 8 位)中,FE 位于 DPL(低 8 位)中。累加器的值为 02H。然后执行 16 位加法,01FE H+02H 得到 0200 H。位于位置 0200H 的数据将传输到累加器。累加器中的前一个值(02H)将被 0200H 中的新值替换。说明中的累加器中的新数据将突出显示。

The opcode is 93H. DPTR has the value 01FE, where 01 is located in DPH (higher 8 bits) and FE is located in DPL (lower 8 bits). Accumulator has the value 02H. Then a 16-bit addition is performed and 01FE H+02H results in 0200 H. Data at the location 0200H will get transferred to the accumulator. The previous value inside the accumulator (02H) will be replaced with the new data from 0200H. The new data in the accumulator is highlighted in the illustration.

这是一条 1 字节指令,执行需要 2 个周期,与之前的指令(每个都是 1 个周期)相比,此指令所需的执行时间较长。

This is a 1-byte instruction with 2 cycles needed for execution and the execution time required for this instruction is high compared to previous instructions (which were all 1 cycle each).

另一个示例 MOVC A, @A+PC 的工作方式与上述示例相同。在这里,不是将 DPTR 与累加器相加,而是将程序计数器(PC)中的数据与累加器相加以获得目标地址。

The other example MOVC A, @A+PC works the same way as the above example. Instead of adding DPTR with the accumulator, here the data inside the program counter (PC) is added with the accumulator to obtain the target address.