I'm trying to implement a function int string_cmp(const char* str1, char* str2)
that takes in 2 strings and I'm supposed to return 1 if str1 is greater than str2 alphabetically, 0 if they are the same and -1 if str1 is smaller.
I'm a newbie in assembly, so I have almost no clue on how can I implement this function correctly, here is what I have so far:
int string_cmp(const char* str1, char* str2) {
int result;
asm volatile (
"mov %[src], %%rsi\n"
"mov %[dst], %%rdi\n"
);
}
I just want to clarify that the reason I have a very very tiny portion of a far from completed code is because I genuinely don't know how to proceed from this point onwards, so if anyone could give me an explanation or show me a sample code that would work as per the requirements that I have mentioned above, it would be really helpful to me and others struggling to learn as well.
Thank you.
Edit: Here is what I have so far:
int string_cmp(const char* str1, char* str2) {
int result;
asm volatile (
"cld\n" // Clear the direction flag to move forward
"1:\n"
"lodsb\n"
"scasb\n" // Compare the byte in al with the byte at [rdi] and increment rdi
"jne 2f\n"
"test al, al\n"
"jne 1b\n"
"xor eax, eax\n"
"jmp 3f\n"
"2:\n"
"sbb eax, eax\n" // Set eax to -1 if the strings are not equal
"3:\n"
: [src] "+r" (str1),
"=a" (result) // Output: store the result in the 'result' variable
: [dst] "D" (str2) // Input: str2 is in the rdi register
: "cc" // Clobbered flags
);
return result;
}