title: How to increment value of global variable in assembly in linux
body: <p>I am going through the assembly implementation of AES algorithm to understand, how sbox and lookup table are implemented/accessed. 
  Theoretically I know, AES uses 10 rounds when key length=128bits. </p>
  
  <p>There is one part where one loop of 200 lines of code (representation of AES rounds) is executed.</p>
  
  <pre><code>_x86_64_AES_decrypt_compact:
  
  
  .Ldec_loop_compact:
  
  xorl    0(%r15),%eax
  xorl    4(%r15),%ebx
  xorl    8(%r15),%ecx
  xorl    12(%r15),%edx
  leaq    16(%r15),%r15
  .
  .
  .
  xorl    %r12d,%ecx
  movq    256(%r14),%r13
  xorl    %r8d,%ebx
  xorl    %r11d,%edx
  
  jmp .Ldec_loop_compact
  </code></pre>
  
  <p>I want to increase a global variable's (i.e. round_var) value by 1 every time one round of AES is completed.
  So at the end of AES operation on 1 block (16bytes), the variable's value will be 10 and 
  at the end of AES operation on 2 block (2x16bytes), the variable's value will be 20.</p>
  
  <pre><code>.globl round_var 
  round_var:
  .long 0
  
  _x86_64_AES_decrypt_compact:
  
  
  .Ldec_loop_compact:
  .
  .
  .
  ;increase round_var by 1 
  .
  .
  .
  jmp .Ldec_loop_compact
  </code></pre>
  
  <p>To increase round_var by 1 , I tried following approaches </p>
  
  <pre><code>64bit :
          movq    round_var(%rip), %rax
          addq    $1, %rax
          movq    %rax, round_var(%rip)
  
  32bit :
          movl    round_var(%rip), %eax
          addl    $1, %eax
          movl    %eax, round_var(%rip)
  </code></pre>
  
  <p>As all of eax,ebx,ecx,edx are used inside that loop, I just added those lines before the updation of %eax (or %rax) register so it won't effect AES operation. </p>
  
  <p>After addition of those lines, I am successfully able to execute following commands to build customized openssl. It means the lines are added at correct place during compile/assemble time.</p>
  
  <pre><code>make
  sudo make install
  </code></pre>
  
  <p>However, I got segmentation fault while trying to perform AES decryption operation. This indicates something went wrong at those lines codes at runtime.</p>
  
  <p>I suspect <strong>%rip</strong> of <code>movl    round_var(%rip), %eax</code> is causing this segmentation fault.</p>
  
  <p>It will be great if anyone help me to resolve the above issue or to increment the global variable round_var by 1 using assembly instruction ?</p>
  
  <p>I am using Ubuntu-16.04 , Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz and Openssl-1.0.2n</p>
  
  <p>Thanks in advance.</p>