title: Recursively adding all the elements in an array in NASM Assembly
body: <p>Still on my way to learn recursion in NASM Assembly, 32-bit Ubuntu, I am now trying to add all the elements in an array recursively. The array's elements are all 4-bytes each.</p>
  
  <p>I came up with a solution that seems to work.</p>
  
  <p>Basically, to add the elements in an array, I somehow need to <strong>count</strong> them, right? So I have <code>ESI</code> as my counter. However, this register needs to be set to <code>0</code> at the beginning of the function - but I don't think there is any way I can tell whether the current function call is the first one, or the second or third... So to fix this, I have two functions: the <strong>initial call</strong>, and the <strong>recursive call</strong>. The first one sets <code>ESI</code> to <code>0</code> and then calls the recursive call. The elements are all added to <code>EAX</code>, which is also set to <code>0</code> in the initial call..</p>
  
  <p>But I'm concerned with it because it is somewhat different from two recursive functions I've done before:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/19217960/555690">Understanding recursive factorial function in NASM Assembly</a></li>
  <li><a href="https://stackoverflow.com/q/19220340/555690">NASM Assembly recursive fibonacci</a></li>
  </ul>
  
  <p>Because, first of all, I'm using two functions, one for the start, and another for the actual recursive part. Also, I am using a counter, which feels awfully a lot like an iterative solution.</p>
  
  <p>So my questions are: <strong>is there a solution that resembles better the two recursive functions I posted above? Can my current solution be considered recursive?</strong></p>
  
  <pre><code>; --------------------------------------------------------------------------
  ; Recursive function that adds all the elements in an array into EAX.
  ; The array's elements are 4-bytes each.
  ; --------------------------------------------------------------------------
  
  SECTION .data
      array:  dd  1,5,3,7,4,8,5,2
      size:   equ $-array
  
  SECTION .text
      global  main
      main:
  
      ; ------------------------------------------------------------------
      ; * Main
      ; ------------------------------------------------------------------
      call    addVector
      breakpoint:     ; Used for GDB to inspect the result later
  
      ; ------------------------------------------------------------------
      ; * Exit
      ; ------------------------------------------------------------------
      mov     EAX,0
      int     0x80
  
      ; ------------------------------------------------------------------
      ; * Initial function call, before doing the recursive calls
      ;   Sets ESI to 0, which will be used to count the array's elements
      ;   Also sets EAX to 0, for storing the result
      ; ------------------------------------------------------------------
      addVector:
      push    ESI
      mov     ESI,0
      mov     EAX,0
      call    recursiveCall
      pop     ESI
      ret
  
      ; ------------------------------------------------------------------
      ; * Recursive part of the function
      ;   Adds to EAX to current element, and increases the ESI counter by
      ;   4 (because the array's elements are 4-bytes each).
      ;   If the counter happens to be &gt;= the array's size, stop.
      ; ------------------------------------------------------------------
      recursiveCall:
      cmp     ESI,size
      jge     endRecursiveCall
      add     EAX,[array + ESI]
      add     ESI,4
      call    recursiveCall
      endRecursiveCall:
      ret
  </code></pre>