Arkitek
Arkitek

Reputation: 3

Comparing struct char array with char pointer in assembly

I'm trying to compare the first character of the first struct in list with the first character of token in assembly. However, the two characters are never equal for some reason. I know the first char in the first student is the same as the first char in token.

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;

The C code is what the end result is supposed to be, but I'm still trying to figure out how to compare clist[index].name and token. How would I compare the struct char array with the token char*?

Edit: Comparison worked after changing it to:

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

Upvotes: 0

Views: 460

Answers (1)

Two main problems: First, cmpq compares the first 8 characters, not just one. Second, when the comparison fails, you return the pointer list, rather than any sort of sentinel.

Also, you're clobbering a bunch of registers but not letting GCC know. After your last :, add "rbx", "rcx", "rsi", "rdi", "cc", "memory".

Upvotes: 1

Related Questions