title: Outputting integers in assembly on Linux
body: <p>This needs to be done in pure assembly (ie. no libraries or calls to C).</p>
  
  <p>I understand the essence of the problem: one needs to divide the integer by 10, convert the one-digit remainder to ASCII, output that and then repeat the process with the quotient.</p>
  
  <p>But for some reason, it's just not working. I'm using NASM on x86.</p>
  
  <p>Here's what I have up to now (doesn't output anything, but doesn't throw any assembler errors either):</p>
  
  <pre><code>; integer to output is stored in eax
  mov ecx, 10   ; for base 10
  
  loop:
  div ecx  ;EAX contains the quotient, EDX the remainder
  
  ; Do something to EDX to convert it to ASCII, not sure if this is correct
  add edx, '0'
  
  push eax    ;We'll be playing with EAX to output EDX, save EAX to the stack
  
  mov eax, 4              ; sys_write
  mov ebx, 1              ; to STDOUT
  mov ecx, edx
  mov edx, 1
  int 0x80
  
  pop eax  ;restore EAX
  
  cmp eax, 0   ;If EAX is 0, our job is done
  jnz loop
  </code></pre>
  
  <p>There are a number of questions similar to this one (namely, <a href="https://stackoverflow.com/questions/4117422/more-efficient-way-to-output-an-integer-in-pure-assembly">this</a> and <a href="https://stackoverflow.com/questions/6892805/how-to-get-integer-input-with-assembly">this</a>), but I'm lost in the implementation. <a href="https://stackoverflow.com/questions/4244624/print-integer-to-console-in-x86-assembly">This question</a> (for DOS) was also helpful, but I'm still confused.</p>
  
  <p>I must be missing something here. Thoughts?</p>