比较两个字符串并在& t语法

时间:2017-12-03 01:54:06

标签: assembly x86-64 att

这是我的汇编代码,用于查找和打印两个字符串之间不同的字母索引,但我不断收到分段错误。我尝试了GDB,当我的程序打印出第一个索引时,显然它会丢失跟踪并转到内存中的其他位置。感谢任何帮助。

更新: 我将寄存器更改为跨函数保留的寄存器,但现在我得到无限的数字打印。 WTF正在进行..

UPDATE1: 这个问题没有得到回答,也没有重复。

.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

0 个答案:

没有答案