5.3 Direct addressing mode

The is our first memory operand. A direct operand specifies an address, and the value at that address is the value of the operand. In a direct operand, the address cannot change. However, the value at that address can change over time.

A direct operand is specified by an expression that evaluates to the address. Let us take a look at the following confusing example:

mov $0,0  
    

What does it do?

The source operand is $0, which means it is an immediate operand that specifies a value of 0. The destination operand is 0, which is just an expression. The destination operand is a direct operand that specifies location 0! In other words, this instruction copies a value of zero to location zero in memory.

Since location 0 is usually not allocated to a process, the execution of this instruction is likely to result in a GPF (general protection fault) or segmentation fault (aka segfault). A segfault essentially means that a process attempt to access ammeory location that it has no permission to access.

In general, we do not use a numerical constant for direct operands. Instead, we use labels that are defined as the first location of some allocated memory. Let us take a look at listing 5.3.

.data 
label1: .int 0x5 
.text 
... # some code 
mov $23, label1 
... # some more code

Let us take a look at the mov $23, label1 instruction. The source operand is obvious, it is a constant of 23. What about the destination operand? The lack of a dollar sign means it is a direct operand. The expression consists of label1 itself. How was label1 defined? It is defined as the first byte of the 32-bit integer with an initial value of 5. Consequently, this instruction overwrite the location (that was initialized to 5) with a new value of 23.