title: NASM - Integer to String
body: <p>have a question regarding integer to string conversion in NASM.</p>
  
  <p>My question is how do I concatenate the digits such that the output contains all the digits when the loop is terminated, rather than the last calculated digit?</p>
  
  <p>Eg. 1234 -> 1234 div 10 = remainder 4 => Buffer = "4"
      123 -> 123 div 10 = remainder 3 => Buffer = "3"
      etc...</p>
  
  <p>My program just stores the last digit calculated ('1') and prints that.</p>
  
  <p>Here are my code files:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      int i;
      char *str;
      printf("Enter a number: ");
      scanf ("%d", &amp;i);
      str=int2string(i);
      printf("Number as string is: %s\n", str);
  
      return 0;
  }
  
  %include "asm_io.inc"
  
  segment .data
  
  segment .bss
  buffer db 20    ; buffer of size 8 bits (in reference to c file)
  
  segment .text
      global int2string
  int2string:
          enter   0,0             ; setup routine
          pusha
      mov esi, 0      ; set sign to 0
      mov ebx, 0      ; set current remainder to 0
      mov edi, 0      ; set the current digit in eax
  
          mov eax, [ebp+8]        ; eax contains input value of int2string
  
      jmp placeValues
  
  placeValues:
      mov edx, 0      ; set remainder to 0
      mov eax, eax        ; redundancy ensures dividend is eax
      mov ebx, 10     ; sets the divisor to value of 10
      div ebx         ; eax = eax / ebx   
  
      mov ebx, 48     ; set ebx to 48 for ASCII conversion
      add ebx, edx        ; add remainder to ebx for ASCII value
      add dword[buffer], ebx  ; store the number in the buffer
  
      cmp eax, 0
      jz return   
      jmp placeValues
  
  return:
      popa
      mov eax, buffer     ; move buffer value to eax
          leave                     
          ret
  </code></pre>