In this tutorial we're going to examine the different kinds of addressing methods in Assembly. This is essential, not only for understanding assembly code, but also for writing your own assembly programs as you'll have to decide which addressing method to use based on program context. There are several different addressing methods available, this article will focus on the most common.
We'll be using AT&T syntax in this tutorial. Getting familiar with AT&T syntax is wise as its still very common and the primary syntax for the GNU assembler. You can read up on the key differences between syntaxes here:
Let's jump in.
The data to access is included directly in the instruction itself:
Ex: movl $1, %eax
The example moves the value "1" into the %eax register. Notice that we just provide the data that we want the computer to store rather than an address we want it to read from. The "$" tells the computer to treat this value as a constant.
The source is a register to access rather than a constant or a memory location:
Ex: movl %ebx, %eax
This moves whatever data is stored in the %ebx register into the %eax register.
The source contains the address of the data we want to access:
Ex: movl 100, %eax
This moves the value stored at address "100" into the %eax register.
The source contains a pointer to the data we want to access. However, we can manipulate this pointer before we dereference it. Indexed addressing follows this format -> segment:displacement(base register, index register, scale factor):
Ex: movl -4(%ebx, %edx, 8), %eax
This example adds the index register (edx) times the scale factor (8) to the base register (ebx) and then adds the displacement value (-4). Programmatically it looks like *(ebx + (edx * 8) - 4).
The source is a register which contains a pointer to the data we want to access:
Ex: movl (%ebx), %eax
This moves the value at the address stored in the %ebx register into %eax.
Similar to indirect addressing except you include an offset to "add to the registers value before using it for lookup [dereferencing it]," (bigsearcher.com):
Ex: movl -4(%ebx), %eax
This would move the value at the address stored inside the ebx register minus four into the %eax register. Remember, you add/subtract before you dereference.
Hopefully the reader has a conceptual as well as technical understanding of the different kinds of Assembly addressing modes. If a mode still doesn't make sense, Google it! Note that some sources use slightly different names for the different addressing modes. Onwards and upwards!