There are many ways that processors use to access data from memory. These methods of access are typically referred to as addressing modes. When programming assembly, you will encounter and use a variety of addressing modes depending on what you are trying to achieve.
The first type of addressing mode is called immediate mode. This is a situation where the data is embedded in the actual instruction itself. Think of these as constant values, where we know what the value is at the time of applying the instruction. One good example would be doing an operation like i += 1. The value 1 can be addressed in immediate mode, since the value is given in the instruction. The value of i would not be addressed in immediate mode since it references a memory location which needs to be accessed to know the value of i.
When we discussed computer memory, we discussed the idea of registers, which are memory storage locations within the processor. If we want to get data from the register of the processor, we would use register addressing mode, which is when the instruction is given a register to access in order to get the required value.
If we need to access data that is not stored in registers or constant values, we need to be able to address the computer’s memory. The first way to do this is by using direct addressing mode. This mode is used when we know the exact address in memory we need to access. With this memory addressing mode, we simply state the memory address we wish to access, and it is fetched. In some instances, we may need to find a memory address based on one we already have. For instance, if we store an array in memory, the array might start at address 2000, and then have a new entry every 4bytes. If this is the case, we know that 4 bytes from 2000 is entry 1, and 8 bytes from 2000 is entry 2, and so on. This type of referencing is called indexed address mode.
Indirect addressing mode allows us to store a memory address in a register and access it to point to the address in memory we wish to access. For example, if we have an array that starts at entry 2000, we can store 2000 in a register. Then, we can use indirect addressing to access the memory at the registers value. The value of this is that we can then increment the value in the register to 2004 to be at the next value in the array, and so on, like how a for loop would work. We can also include an offset when doing indirect addressing, and this is referred to as base pointer addressing mode.
Each of these addressing modes have applications in assembly programming. Throughout this series of articles, you will see each of the addressing modes in action, and learn when they should be used.