Interpreting Operands
With ATT syntax, we address the registers, memory, and immediate values using the following symbols:
$is used in front of immediate values.%is used in front of registers.()are used to dereference a memory address typically (i.e., get the value from memory).
Examples
$-17represents the integer -17.
$0xCrepresents the integer 12 (that is, C in base 16 is 12 in base 10).
0x108is a memory address.
%raxaccesses the value stored at the register addressed withrax.
(%rax)accesses the value stored in memory (whereraxis holding a memory address, opcode dependent).
Scaling, Shifting, and Indexing
An operand can be given in the format of A ( B, I, S ) where
Bis a base value that can be indexed, scaled, and shifted with I, S, and A.Irepresents an index. We add the value of I to the base value B.
Sis the scale for the index. We scale the value of I by S. The value of S must be 1, 2, 4, or 8.
Ais a shift, similar to I. We add A to the result of applying I and S to B.
Examples
The following are semi-abstract, descriptive examples that describe the memory address that gets accessed (assuming the instruction we are using would dereference it).
9(%rax)- Assume rax is storing a memory address.
- We shift 9 memory addresses over from the address stored at rax.
- We access the value stored at that resulting address.
(%rbx, %rdi)- Assume rbx is storing a memory address.
- rdi stores a value that we will add to rbx to get a new memory address.
- The parenthesis will tell us to access the value at that final memory address.
-12(%rbx, %rdi)- Assume rbx is storing a memory address.
- rdi stores a value we need to add to that memory address to get a resulting memory address
- -12 tells us to subtract 12 from the previous memory address to get a new memory address
- Access the value at that final memory address.
(,%rdi,4)- rdi is storing a value that gets multiplied by 4
- Assume the resulting value is a memory address that we can visit.
- We get the value at that memory address.
10(,%rdi,4)- rdi is storing a value that gets multiplied by 4
- Assume the resulting value is a memory address that we can visit.
- Shift 10 memory addresses over from the previously resulting memory address.
- Access that value.
10(%rbx, %rdi, 4)- Assume rbx stores a memory location
- Multiply the value stored at rdi by 4 and then add the result to the memory location stored in rbx for a new resulting memory address.
- Now shift 10 memory addresses over from the previous result.
- Access the value at that address.