我正在做一個小專案,我必須從 lib C 重新編碼一個函式。實際上我正在做strcasecmp:
BITS 64
%include "minilib.inc"
section .text
my_strcasecmp:
init:
mov r10b, [rdi]
mov r11b, [rsi]
jmp while
while:
cmp r10b, 0
je end
cmp r11b, 0
je end
cmp r10b, 90
jle checkfirstup
cmp r11b, 90
jle checksecondup
jmp strcasecmp
strcasecmp:
cmp r10b, r11b
jne end
inc rdi
inc rsi
jmp init
checkfirstup:
cmp r10b, 65
jge r10btolowcase
jmp checksecondup
r10btolowcase:
add r10b, 32
jmp while
checksecondup:
cmp r11b, 65
jge r11btolowcase
jmp strcasecmp
r11btolowcase:
add r11b, 32
jmp while
end:
movzx rax, r10b
movzx rbx, r11b
sub rax, rbx
ret
這是 c 中的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int my_strcasecmp(const char *string1, const char *string2); /* Prototype */
int main(void)
{
char *str1 = "S";
char *str2 = "S";
int result;
result = strcasecmp(str1, str2);
if (result == 0)
printf("Strings compared equal.\n");
else if (result < 0)
printf("\"%s\" is less than \"%s\".\n", str1, str2);
else
printf("\"%s\" is greater than \"%s\".\n", str1, str2);
return 0;}
當我嘗試我的 strcasecmp 時,我總是有“nanana 大于 nanana”,我不明白為什么。
我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
問題是,當第一個字符不在 [A,Z] 中時,您會立即跳轉到checksecondup,在那里您只檢查第二個字符的下限。當時cmp r11b, 90從未執行過!(它會觸發不需要的add r11b, 32.)
解決方案是獨立地對兩個字符進行 LCase:
...
cmp r10b, 65
jb Next
cmp r10b, 90
ja Next
add r10b, 32
Next:
cmp r11b, 65
jb Next_
cmp r11b, 90
ja Next_
add r11b, 32
Next_:
cmp r10b, r11b
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434837.html
下一篇:` `和` =`運算子的行為不同
