title: Assembly(Intel x86) function to find the length of a string, why am I getting extra characters?
body: <p>I am a beginner in assembly and I have this homework where I have to create a <code>strlen</code> function to find the length of any string.</p>
  
  <p>I tried subtracting 4 from <code>edx</code> because I am seeing 4 extra characters at the end, but that did not fix anything. They are still there.</p>
  
  <pre><code>section .data   
  text: db "Hello world, trying to find length of string using function."     ;our string to be outputted
  
  section .text
  global _start   ;declared for linker
  
  _start:     
      mov eax, 4      ;system call number (sys write)
      mov ebx, 1      ;file descriptor to write-only
      mov ecx, text   ;message to output
      call strlen
      mov edx, len    ;length of string to print
      int 80h         ;interrupt
  
  exit:       
      mov eax, 1  ;system call number (sys exit)
      mov ebx, 0  ;file descriptor to read-only
      int 80h     ;interrupt
  
  strlen: 
      push ebp        ;prologue, save base pointer
      mov ebp, esp    ;copy esp to ebp
      push edi        ;push edi for use
  
                      ;body
      mov edi, text   ;save text to edi, and i think when i do that edi expands? if text = 5 bytes, and edi was originally 4, then edi becomes 5?
      sub edi, esp    ;subtract edi starting point by the esp starting point to get len. ex: edi = 100, esp = 95
      mov [len], edi  ;copy value of edi onto len
  
      pop edi         ;epilogue, pop edi out of stack
      mov esp, ebp    ;return esp back to top of stack
      pop ebp         ;pop ebp back to original
      ret             ;return address
  
  
  
  section .bss    
  len: resb 4 ;4 byte to integer
  </code></pre>
  
  <p>Let say I have the follow code in the <code>.data</code> section:</p>
  
  <pre><code>section .data   
  text: db "Hello world, trying to find length of string using function."
  </code></pre>
  
  <p>The expected output should be <em>"Hello world, trying to find length of string using function."</em>, however I am getting <em>"Hello world, trying to find length of string using function.####"</em> where <code>#</code> is any random character. </p>
  
  <p>This is the terminal output :<br>
  <a href="https://i.gyazo.com/2f1fc69caa08373252d5b5d0a8e03680.png" rel="nofollow noreferrer"><img src="https://i.gyazo.com/2f1fc69caa08373252d5b5d0a8e03680.png" alt=""></a></p>
  
  <p>Thank you.</p>