title: Understanding these assembly instructions
body: <p>I'm learning assembly by writing C programs and viewing the assembly output. I've included the C program at the bottom for the page to make it easier. I'm struggling to understand one line of assembly:  </p>
  
  <p>cdqe  </p>
  
  <p>movzx   eax, BYTE PTR [rbp-32+rax] &lt;--- what is this doing?  </p>
  
  <p>movsx   eax, al</p>
  
  <p>So I think cdqe extends eax into rax (64 bits). Its clear that the string I want to print fits into the al register but I don't understand what is happening deep down with rbp-32+rax. Can someone explain for me?</p>
  
  <pre><code>.file   "string_manip.c"
      .intel_syntax noprefix
      .section    .rodata
  .LC0:
      .string "Hello"
      .string ""
      .zero   3
      .text
      .globl  main
      .type   main, @function
  main:
  .LFB0:
      .cfi_startproc
      push    rbp
      .cfi_def_cfa_offset 16
      .cfi_offset 6, -16
      mov rbp, rsp
      .cfi_def_cfa_register 6
      sub rsp, 48
      mov rax, QWORD PTR fs:40
      mov QWORD PTR [rbp-8], rax
      xor eax, eax
      mov DWORD PTR [rbp-36], 0
      mov eax, DWORD PTR .LC0[rip]
      mov DWORD PTR [rbp-32], eax
      movzx   eax, WORD PTR .LC0[rip+4]
      mov WORD PTR [rbp-28], ax
      movzx   eax, BYTE PTR .LC0[rip+6]
      mov BYTE PTR [rbp-26], al
      mov WORD PTR [rbp-25], 0
      mov BYTE PTR [rbp-23], 0
      mov DWORD PTR [rbp-36], 0
      jmp .L2
  .L3:
      mov eax, DWORD PTR [rbp-36]
      cdqe
      movzx   eax, BYTE PTR [rbp-32+rax] &lt;--- what is this doing?
      movsx   eax, al
      mov edi, eax
      call    putchar
      add DWORD PTR [rbp-36], 1
  .L2:
      cmp DWORD PTR [rbp-36], 5
      jle .L3
      mov edi, 10
      call    putchar
      mov eax, 0
      mov rdx, QWORD PTR [rbp-8]
      xor rdx, QWORD PTR fs:40
      je  .L5
      call    __stack_chk_fail
  .L5:
      leave
      .cfi_def_cfa 7, 8
      ret
      .cfi_endproc
  .LFE0:
      .size   main, .-main
      .ident  "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4"
      .section    .note.GNU-stack,"",@progbits
  
  
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  int main()
  {
      int i = 0;
      char array[10] = "Hello\0";
      for(i=0; i&lt;6; i++)
          printf("%c", array[i]);
      printf("\n");
      return 0;
  }
  </code></pre>