====== Fundamentals of Addressing Modes ====== Addressing Modes is the way in which the argument of an instruction is specified. The addressing mode defines a rule for interpreting the address field of the instruction before the operand is reached. Addressing mode is used in instructions which operate on the data or in instructions which change the program flow. ===== Data addressing ===== Instructions which reach the data have the possibility of specifying the data placement. The data is an argument of the instruction, sometimes called an operand. Operands can be of one of the following: register, immediate, direct memory, and indirect memory. As in this part of the book the reader doesn't know any assembler instructions we will use the hypothetic instruction //copy// that copies the data from the source operand to the destination operand. The order of the operands will be similar to high-level languages where the left operand is the destination and the right operand is the source. Copying data from //a// to //b// will be done with an instruction as in the following example: copy b, a **Register operand** is used where the data which the processor wants to reach is stored or is intended to be stored in the register. If we assume that //a// and //b// are both registers named //R0// and //R1// the instruction for copying data from //R0// to //R1// will look as in the following example and as shown in the Fig.{{ref>addrregister}}. copy R1, R0
{{ :en:multiasm:cs:addressing_register.png?600 |Illustration of addressing registers}} Illustration of addressing registers
**An immediate operand** is a constant or the result of a constant expression. The assembler encodes immediate values into the instruction at assembly time. The operand of this type can be only one in the instruction and is always at the source place in the operands list. Immediate operands are used to initialise the register or variable, as numbers for comparison. An immediate operand as it's encoded in the instruction, is placed in code memory, not in data memory and can't be modified during software execution. Instruction which initialises register //R1// with the constant (immediate) value of //5// looks like this: copy R1, 5
{{ :en:multiasm:cs:addressing_immediate.png?600 |Illustration of immediate addressing mode}} Illustration of immediate addressing mode
**A direct memory operand** specifies the data at a given address. An address can be given in numerical form or as the name of the previously defined variable. It is equivalent to static variable definition in high-level languages. If we assume that the //var// represents the address of the variable the instruction which copies data from the variable to //R1// can look like this: copy R1, var
{{ :en:multiasm:cs:addressing_direct.png?600 |Illustration of direct addressing mode}} Illustration of direct addressing mode
**Indirect memory operand** is accessed by specifying the name of the register which value represents the address of the memory location to reach. We can compare the indirect addressing to the pointer in high-level languages where the variable does not store the value but points to the memory location where the value is stored. Indirect addressing can also be used to access elements of the table in a loop, where we use the index value which changes every loop iteration rather than a single address. Different assemblers have different notations of indirect addressing, some use brackets, some square brackets, and others //@// symbol. Even different assembler programs for the same processor can differ. In the following example, we assume the use of square brackets. The instruction which copies the data from the memory location addressed by the content of the //R0// register to //R1// register would look like this: copy R1, [R0]
{{ :en:multiasm:cs:addressing_indirect.png?600 |Illustration of indirect addressing mode}} Illustration of indirect addressing mode
**Variations of indirect addressing**. The indirect addressing mode can have many variations where the final address doesn't have to be the content of a single register but rather the sum of a constant value with one or more registers. Some variants implement automatic incrementation (similar to the "++" operator) or decrementation ("--") of the index register before or after instruction execution to make processing the tables faster. For example, accessing elements of the table where the base address of the table is named //data_table// and the register //R0// holds the index of the byte which we want to copy from a table to //R1// could look like this: copy R1, table[R0]
{{ :en:multiasm:cs:addressing_index.png?600 |Illustration of indirect index addressing mode}} Illustration of indirect index addressing mode
Addressing mode with pre-decrementation (decrementing before instruction execution) could look like this: copy R1, table[--R0] Addressing mode with post-incrementation (incrementing after instruction execution) could look like this: copy R1, table[R0++] ===== Program control flow destination addressing ===== The operand of jump, branch, or function call instructions addresses the destination of the program flow control. The result of these instructions is the change of the Instruction Pointer content. Jump instructions should be avoided in structural or object-oriented high-level languages, but they are rather common in assembler programming. Our examples will use the hypothetic //jump// instruction with a single operand—the destination address. **Direct addressing** of the destination is similar to direct data addressing. It specifies the destination address as the constant value, usually represented by the name. In assembler, we define the names of the addresses in code as //labels//. In the following example, the code will jump to the label named //destin//: jump destin
{{ :en:multiasm:cs:jump_direct.png?600 |Illustration of addressing in direct jump}} Illustration of addressing in direct jump
**Indirect addressing** of the destination uses the content of the register as the address where the program will jump. In the following example, the processor will jump to the destination address which is stored in //R0//: jump [R0]
{{ :en:multiasm:cs:jump_indirect.png?600 |Illustration of addressing in indirect jump}} Illustration of addressing in indirect jump
===== Absolute and Relative addressing ===== In all previous examples, the addresses were specified as the values which represent the **absolute** memory location. The resulting address (even calculated as the sum of some values) was the memory location counted from the beginning of the memory - address "0". It is presented in Fig{{ref>addrabsolute}}.
{{ :en:multiasm:cs:addressing_absolute.png?600 |Illustration of absolute addressing of the variable}} Illustration of absolute addressing of the variable
Absolute addressing is simple and doesn't require any additional calculations by the processor. It is often used in embedded systems, where the software is installed and configured by the designer and the location of programs does not change. Absolute addressing is very hard to use in general-purpose operating systems like Linux or Windows where the user can start a variety of different programs, and their placement in the memory differs every time they're loaded and executed. Much more useful is the **relative addressing** where operands are specified as differences from memory location and some known value which can be easily modified and accessed. Often the operands are provided relative to the Instruction Pointer which allows the program to be loaded at any address in the address space, but the distance between the currently executed instruction and the location of the data it wants to reach is always the same. This is the default addressing mode in the Windows operating system working on x64 machines. It is illustrated in Fig{{ref>addrrelative}}.
{{ :en:multiasm:cs:addressing_relative.png?600 |Illustration of IP relative addressing of the variable}} Illustration of IP relative addressing of the variable
Relative addressing is also implemented in many jump, branch or loop instructions.