我正在尝试将列表中第一个结构的第一个字符与汇编中标记的第一个字符进行比较。但是,由于某些原因,两个字符永远不会相等。我知道第一个学生中的第一个字符与令牌中的第一个字符相同。
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].name
和token
。我如何将结构char数组与令牌char *进行比较?
两个主要问题:首先,cmpq
比较前8个字符,而不仅仅是一个。其次,当比较失败时,您将返回指针list
,而不是任何类型的哨兵。
[此外,您正在破坏一堆寄存器,但不通知GCC。在上一个:
之后,添加"rbx", "rcx", "rsi", "rdi", "cc", "memory"
。