This is my assembly code to find and print the index of the letter that is different between two strings, but I keep getting segmentation faults. I tried the GDB and when my program prints the first index apparently it loses track and goes somewhere else in memory. Appreciate any help.
UPDATE: I changed the registers to those which are preserved across function, but now I am getting infinite prints of numbers. WTF is going on..
UPDATE1: This question is not answered, it is not a duplicate.
.text
       mystring:    .asciz  "Thisisacomparisontest."
       mystring1:   .asciz  "Thioisacomparusontosi."
       mystring2:   .asciz  "The strings are:"
       formatstr:   .asciz  "%d\n"
       formatstring:    .asciz  "%s\n"
.global main
main:
        movq    %rsp,%rbp
        movq    $mystring,%r12  #load the strings to registers
        movq    $mystring1,%r13     #>>
        movq    $0,%rbx             #initialise i
        call    loop
        jmp end 
loop:
        mov (%r12,%rbx),%al     #move each charatcter of the 1st string to the lowest memory 
        mov (%r13,%rbx),%bl     #>>          >>              2nd    >>            
        incq    %rbx            #increment pointer for the next letter
        cmp %al,%bl             #compare the two letters
        jne notequal    #if not equal then the two strings are different, so 
        cmp $0,%al          #compare 0 with al to realise the end of the string
        je  endofstring     #if end of string then the strings are printed
        jmp loop            #if not start with the second letter
notequal:
        movq    $formatstr,%rdi
        movq    %rbx,%rsi
        movq    $0,%rax
        call    printf
        jmp     loop
endofstring:
        movq    $formatstring,%rdi
        movq    $mystring2,%rsi
        movq    $0,%rax
        call    printf
        movq    $formatstring,%rdi
        movq    $mystring,%rsi
        movq    $0,%rax
        call    printf
         movq   $formatstring,%rdi
         movq   $mystring1,%rsi
         movq   $0,%rax
         call   printf
         ret
end:
     movq   %rbp,%rsp
     popq   %rbp
     movq   $0,%rdi
     call   exit