2

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
  • 3
    For one thing, `%rdx` is not preserved across function calls (https://stackoverflow.com/q/18024672/) – Nemo Dec 03 '17 at 01:58
  • r10 is also not preserved. – prl Dec 03 '17 at 04:22
  • Not related to your question, but if the strings are not the same length, it won't stop at the end of the string. – prl Dec 03 '17 at 04:26
  • You have at least one other bug: like @prl pointed out you forgot to check for a terminating `0` in positions where the bytes don't match. But the one you're asking about is because your function call clobbers the regs you're using. You should be able to track down the rest with GDB. – Peter Cordes Dec 03 '17 at 06:10
  • Hey, thanks for the responses. What do you mean by saying my function calls clobbers. And also , the comparison with zero must happen and when they are not equal? I mean they are both zero, when its the end of the string so they are equal and hence it will check it when they are equal. Maybe i m wrong, but I m new in assembly. – Dr Victor V. Doom Dec 03 '17 at 14:52
  • Read the linked duplicate re: call-preserved vs. call-clobbered registers. (And/or read more about calling conventions in general). – Peter Cordes Dec 03 '17 at 20:40
  • re: checking when not equal: What if the two strings aren't the same length? The shorter one will have a terminating 0 which doesn't match the byte in the other string. – Peter Cordes Dec 03 '17 at 20:41
  • Yeah now I know what is clobbered. And I also compared with zero inside my notequal subroutine.But again, this isnt the problem. I modified my program and now I can get the letters that are different between the two strings but I cant get their position. – Dr Victor V. Doom Dec 04 '17 at 01:16
  • I use %rbx for the pointer, and when I put this register as an argument in %rsi in order to be printed, it prints the ascii number of the different letters that it found between the two strings. So if I change the formatstr to %c it prints the mismatching letters. – Dr Victor V. Doom Dec 04 '17 at 01:21

0 Answers0