逐行读取字符串X86 64

时间:2014-09-28 15:12:06

标签: assembly comparison line

我正在UNIX中为作业重新创建diff函数。我只需检查线路是否已更改,未添加或删除。我们可以通过在代码本身中创建字符串来插入字符串(不是从.txt文件或从终端读取)。我被困在阅读已更改的行。我所取得的是确定哪个角色已经改变。如何让我的程序逐行比较字符串?

我现在的代码:

.text
    Waar: .asciz "Gelijk \n"
    NWaar: .asciz "Niet gelijk %d\n"
    String1:    .asciz "abcd\nefgh"
    String2:    .asciz "abcd\neagh"

.global main

    main:


        movq    $String1, %rsi
        movq    $String2, %rdi
        movq    $8, %rcx
        movq    $1, %r8
        cld
        loop:
            cmpsb
            jne     False
            incq    %r8
            jmp loop
        end:

        movq    $0, %rax
        movq    $Waar, %rdi
        call    printf
        call    exit

    False:
        movq    $NWaar, %rdi
        movq    %r8, %rsi
        movq    $0, %rax
        call    printf
        call    exit

    Exit:
        movq    $0, %rdi

1 个答案:

答案 0 :(得分:0)

您应首先使用更高级别的语言编写代码,然后转换为汇编语言。您可以从以下内容开始:

int lines_are_equal(char const *a, char const *b)
{
  while (*a&&*b)               //until zero-terminated...
    {
      if (*a != *b)            //or the two strings differ...
        {
          return 0;            //return false if different
        }
      if (*a == '\n')          //if newline is encountered
        {
          break;               //break to return true
        }
      a++;                     //loop until end of string or
      b++;                     //end of line
    }
 return 1;                     //if end of string or end of line is encountered
                               //before strings differ, return true.
}

您可能还需要一个跳到下一行开头的函数,以便在下一行调用lines_are_equal() ...