title: Assembly: without informing edi of the word size, why does my program jump ahead one number too far?
body: <p>Take a look at this sample program from “programming from the ground up”. It cycles through a list of numbers and ends up putting the largest one in the status register:</p>
  
  <pre><code>.section .data
  data_items: .long 3, 67, 34, 222, 45, 75, 54, 34, 44, 33, 22, 11, 66, 0
  
  .section .text
  .globl _start
  _start:
      movl $0, %edi
      movl data_items (,%edi,4), %eax
      movl %eax, %ebx
      start_loop:
      cmpl $0, %eax
      je loop_exit
      incl %edi
      movl data_items (,%edi,4), %eax
      cmpl %ebx, %eax
      jle start_loop
      movl %eax, %ebx
      jmp start_loop
  loop_exit:
      movl $1, %eax
      int $0x80
  </code></pre>
  
  <p>This is what the program is supposed to look like and I have no issues with it.</p>
  
  <p>However, I’ve been experimenting a lot with removing parts of the program to see what they do more in depth. I have been experimenting with removing <code>(,%edi,4)</code> from the first part of the program, and when you do that by itself the program functions the same, and the largest number still gets left as the status code at the end of the program.</p>
  
  <p>However, if you start the index further along the list (in this case the third number), I’ve noticed that getting rid of <code>(,%edi,4)</code> causes it to skip ahead one number in the <code>data_items</code> list. For example, running the program like this, it ignores <code>222</code> and says that the largest number in the program is <code>75</code>:</p>
  
  <pre><code>.section .data
  data_items: .long 3, 67, 34, 222, 45, 75, 54, 34, 44, 33, 22, 11, 66, 0
  
  .section .text
  .globl _start
  _start:
      movl $3, %edi
      movl data_items, %eax
      movl %eax, %ebx
  start_loop:
      cmpl $0, %eax
      je loop_exit
      incl %edi
      movl data_items (,%edi,4), %eax
      cmpl %ebx, %eax
      jle start_loop
      movl %eax, %ebx
      jmp start_loop
  loop_exit:
      movl $1, %eax
      int $0x80
  </code></pre>
  
  <p>I know that you need to use  <code>(,%edi,4)</code>  to inform the processor of the word size in <code>data_items</code>, but what I wanted to know is why getting rid of <code>(,%edi,4)</code> causes our program to behave in this particular way. </p>