title: Read and print user input with x86 assembly (GNU/Linux)
body: <p>I'm learning x86 assembly on GNU/Linux, and I'm trying to write a program that reads user input from stdin and prints it on stdout.</p>
  
  <p>The following code does work, but it prints extra characters if the size of the user-entered string is less than 100 bytes.</p>
  
  <pre><code>section .data
      str: db 100    ; Allocate buffer of 100 bytes
  
  section .bss
  
  section .text
  
  global _start
  
  _start:
      mov eax, 3          ; Read user input into str 
      mov ebx, 0          ; |
      mov ecx, str        ; | &lt;- destination
      mov edx, 100        ; | &lt;- length
      int 80h             ; \
  
      mov eax, 4          ; Print 100 bytes starting from str
      mov ebx, 1          ; |
      mov ecx, str        ; | &lt;- source
      mov edx, 100        ; | &lt;- length
      int 80h             ; \ 
  
      mov eax, 1          ; Return
      mov ebx, 0          ; | &lt;- return code
      int 80h             ; \
  </code></pre>
  
  <p>How can I reliably calculate the length of the user-entered string? </p>
  
  <p>How can I avoid printing extra characters?</p>