在汇编中比较struct char数组和char指针

Comparing struct char array with char pointer in assembly

我正在尝试将列表中第一个结构的第一个字符与程序集中标记的第一个字符进行比较。但是,由于某种原因,这两个字符永远不相等。我知道第一个学生中的第一个字符与令牌中的第一个字符相同。

struct student
{
  long    ID;          /* 8 bytes in 64-bit */
  char    name[24];
};

/*
* list - the starting address of the list of structures to be searched
* count - total number of names in list
* token - name to be searched in the list
*/
long search_by_name (char* list, long count, char* token)
{
    long index = 0;

asm
(
    "movq %1, %%rax;" // list to rax       
    "movq %2, %%rbx;" // token to rbx
    "movq %3, %%rcx;" // count to rcx 

    "movq 8(%%rax), %%rsi;" // Offset rax by 8 to get to first char in struct
    "movq (%%rbx), %%rdi;" // Move memory address rbx into rdi
    "cmpq %%rdi, %%rsi;" // Compare the two chars
    "je equals;"         // Two chars are never equal for some reason

    "jmp finish;"

"equals:"
    "movq 0(%%rax), %%rax;"
    "jmp finish;"

"finish:"
      : "=a" (index)
     : "r" (list), "r" (token), "r" (count)
     : 
     );
     return index;
}
What it's supposed to do in C:

struct student *clist = (struct student*)list;
for (index = 0; index < count; index++)  
    if ( strcasecmp( clist[index].name, token ) == 0)
        return clist[index].ID;
return 0;

C 代码应该是最终结果,但我仍在尝试弄清楚如何比较 clist[index].nametoken。 我如何将 struct char 数组与标记 char* 进行比较?

编辑: 将其更改为后比较有效:

movq 8(%%rax), %%r10; 
movq (%%rbx), %%r11; 
cmpb %%r10b, %%r11b;

两个主要问题:首先,cmpq比较的是前8个字符,而不是一个。其次,当比较失败时,你 return 指针 list,而不是任何类型的哨兵。

此外,您破坏了一堆寄存器但不让 GCC 知道。在最后一个 : 之后,添加 "rbx", "rcx", "rsi", "rdi", "cc", "memory".