Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86/x64 Add Displacement addressing

I'm writing a compiler for x86/x64 CPU instructions and I can't seem to figure out what people mean by 'displacement' address. For example the Add instruction is detailed here: http://www.c-jump.com/CIS77/CPU/x86/X77_0150_encoding_add_edx_displacement.htm

I'm just trying to implement the add instruction where a register is added to a normal memory address. The problem is, the address is a 'displacement address'. Does that mean the address is a signed value that's the offset from the instruction location?

like image 930
Ryan Brown Avatar asked Nov 11 '12 08:11

Ryan Brown


1 Answers

There are a few different forms of indirect operands in x86:

  1. [reg]
  2. [reg + displacement]
  3. [displacement]
  4. [reg * constant + reg]
  5. [reg * constant + reg + displacement]

The "displacement" is just a constant that gets added to the rest of the address. In cases where there is no component of the address other than the constant, it is still called a "displacement". This is mainly for consistency with the other addressing forms.

Another way to look at it is that all addresses are of the form

[reg * constant + reg + displacement]

With each of the components allowing a value of 0.

The [displacement] form is just the encoding where all components other than the displacement are zero.

As a compiler writer the last 2 forms are particularly interesting. They make it easy to encode things like pArray[index]->field + 1in a single instruction.

like image 90
Scott Wisniewski Avatar answered Nov 15 '22 08:11

Scott Wisniewski