title: Pointer/memory arithmetic in Assembly
body: <p>I'm trying to get a hold of assembly, but there's one probably very simple thing I don't understand.</p>
  
  <p>Consider this following simple example</p>
  
  <pre><code>long long * values = new long long[2];
  values[0] = 10;
  values[1] = 20;
  
  int j = -1;
  
  values[j+2] = 15;  // xxxxxxx
  </code></pre>
  
  <p>Now, the last line (marked with xxxxxx) disassembles to:</p>
  
  <pre><code>000A6604  mov         eax,dword ptr [j]  
  000A6607  mov         ecx,dword ptr [values]  
  000A660A  mov         dword ptr [ecx+eax*8+10h],0Fh  
  </code></pre>
  
  <p>First question: What is actually stored in eax and ecx, is it the actual values (i.e. -1 for "j", and the two long long values 10 and 20 for "values"), or is it merely a memory address (e.g. someting like &amp;p, &amp;values) pointing to some place where the values are being stored?</p>
  
  <p>Second question, I know what the third line is supposed to do, but I'm not quite sure why this actually works.
  So my understand is, it copies the value 0x0F into the specified memory location. The memory location is basically
  - the location of the first element stored in ecx
  - plus the size of long long in bytes (= 8) * the value of eax (which equals j, so -1)
  - plus the generic offset of 16 bytes (2 times the size of long long).
  What I don't get is: In this expression, ecx seems to be a memory address, while eax seems to be a value (-1). How is this possible? Seeing they were defined in pretty much the same way, shouldn't eax and ecx either both contain memory addresses, or both values?</p>
  
  <p>Thanks.</p>