title: Get the last line separator
body: <p>I think this is a pretty common task and there should be some fast and concise solution. I have a quadword and I want to get the lowest byte position which is equal to 0x0A (line break in Linux). I wrote the following simple program:</p>
  
  <pre><code>SYS_exit equ 0x3C
  
  section .text
      global _start
  
  _start:
      mov rax, 0x0A
      mov rbx, [dt]
      mov rcx, 0x07
      loop:
          mov r13, rbx
          and r13, 0xFF
          cmp r13, 0x0A
          jz ok
          shr rbx, 8
          dec rcx
          jnz loop
          jmp fail
      ok:
          mov rax, SYS_exit
          mov rdi, 8
          sub rdi, rcx
          syscall
      fail:
          mov rax, SYS_exit
          mov rdi, -1
          syscall
  
  
  section .data
      dt: dq 0xAB97450A8733AA1F
  </code></pre>
  
  <p>And it works pretty much fine. <code>strace ./bin</code> prints </p>
  
  <pre><code>execve("./bin", ["./bin"], [/* 69 vars */]) = 0
  exit(5)                                 = ?
  +++ exited with 5 +++
  </code></pre>
  
  <p>But the program looks ugly and actually I'm looking for a way to make this  as fast as possible. Can you give some optimization advice?</p>