Immediate and Direct Addressing
Defining the Modes
The two most basic addressing modes are called "immediate" and "direct."
- Immediate Addressing
- In immediate addressing, the instruction itself contains the value to be used. This is like using a constant like 7 or 39 in an expression in a higher level language.
- Direct Addressing
- In direct addressing, the instruction tells where the value can be found, but the value itself is out in memory. In a high level language, direct addressing is frequently used for things like global variables.
Examples in High Level Languages
Suppose we have a statement in C like
i = j + 3; i and j are variables, so they are out in memory. To execute this statement, we will need to fetch j from memory, and write our result to i. That means the instructions we generate need to have the addresses of i and j, and needs to read and write those addresses as appropriate. The number
3, on the other hand, is an actual value appearing in the statement. So, our code needs to include 3 itself. Even if we define a symbol, like
#define FRED 3
i = j + FRED; FRED gets replaced by 3, and immediate addressing still ends up being used. Translating to Assembly Code
In assembly code, the statement described there takes three steps:
- Fetch
jfrom memory. - Add the constant
3to it. - Put the result into memory at
i
In assembly code, this becomes:
ldaa j
adda #3
staa i Specifying Direct and Immediate Mode in Assembly Code
In a high level language, we don't (ordinarily) specify addressing modes. Instead, we just use variables and constants and rely on the language to know what we're talking about: if we use the constant
3, the compiler generates immediate mode; if we use the variable j the compiler generates direct mode. The assembler doesn't work that way. At this level, the assembler is perfectly happy to use a constant
1 as a direct address, or to use the address of j as an immediate value. So we have to tell the assembler what we want to do.
No comments:
Post a Comment