title: Can I multiply a register's value by an immediate number to add the result to another register?
body: <p>Learning Assembly with NASM, Ubuntu, 32 bits.</p>
  
  <p>My array in <code>.data</code>:</p>
  
  <pre><code>ary db 1,2,2,4,5 ; Five elements of one byte each
  </code></pre>
  
  <p>And some number:</p>
  
  <pre><code>tmp db 2   ; Holds the number 2
  </code></pre>
  
  <p>Let's say I want to print the element at index 4 in the array (so it would be <code>5</code>).</p>
  
  <p>I know I could do this:</p>
  
  <pre><code>mov EAX,4
  mov EBX,0
  mov ECX,ary          ; Put the array's address in ECX
  add ECX,4            ; Move address four bytes to the right
  add byte [ECX],'0'   ; The value at this address to ASCII
  mov EDX,1
  int 0x80
  </code></pre>
  
  <p>However, for whatever reasons, I decided that instead of writing the constant number <code>4</code>, I want to do it by multiplying my variable (which is <code>2</code>) by <code>2</code>.</p>
  
  <p>This is the updated code:</p>
  
  <pre><code>mov EAX,[tmp]   ; Put the number 2 in EAX
  mov ECX,ary     ; Put the array's address in ECX
  add ECX,EAX * 2 ; Move (2 * 2) = 4 bytes to the right
  add byte [ECX],'0' ; Decimal to ASCII
  mov EAX,4
  mov EBX,0
  mov EDX,1
  int 0x80
  </code></pre>
  
  <p>This doesn't work at <code>add ECX,EAX * 2</code>:</p>
  
  <pre><code>invalid operand type
  </code></pre>
  
  <p>But why? Doesn't <code>ECX</code> evaluate to <code>2</code>? Being equivalent to</p>
  
  <pre><code>add ECX,2 * 2
  </code></pre>
  
  <p>Curiously, these do work:</p>
  
  <pre><code>add ECX,EAX * 1   ; Moves by 2
  add ECX,EAX * 0   ; Moves by 0
  </code></pre>
  
  <p>The above suggests me that the answer is no. And the reason that multiplying by <code>1</code> or <code>0</code> works is because the assembler doesn't actually need to do any multiplication to know the answer in the first place.</p>
  
  <p>Does this mean that to achieve what I want, I do have to use the <code>mul</code> instruction?</p>