比较字符和Intel x86_64程序集

我是新assembly(英特尔x86_64),我试图从C库重新编码一些function。 我在一个64位的Linux上编译NASM

我有一个strchr函数的错误,我找不到解决scheme…

作为strchr手册页的一个提示,

char * strchr(const char * s,int c);

strchr()函数返回一个指向strings中第一个出现的字符c的指针。

这是我试过的:

strchr: push rpb mov rbp, rsp mov r12, rdi ; get first argument mov r13, rsi ; get second argument call strchr_loop strchr_loop: cmp [r12], r13 ; **DON'T WORK !** check if current character is equal to character given in parameter... je strchr_end ; go to end cmp [r12], 0h ; test end of string je strchr_end ; go to end inc r12 ; move to next character of the string jmp strchr_loop ; loop strchr_end mov rax, r12 ; set function return mov rsp, rbp pop rbp 

这将返回一个指向string的指针,而不会find字符…

我认为这是行不通的:

  cmp [r12], r13 

我testing了这个,它工作:

  cmp [r12], 97 ; 97 = 'a' in ASCII 

这个例子 :

 char *s; s = strchr("blah", 'a'); printf("%s\n", s); 

回 :

 ah 

但是我不能使它与寄存器比较工作。 我做错了什么,我该如何解决?

首先,感谢您的帮助! 我想我对自己在做什么有了更好的理解。

我遇到了接收8位参数而不是64位rdi的问题 …但是一个朋友告诉我,第一个8位参数也在sil寄存器中。

所以这是我的工作代码:

 strchr: push rpb mov rbp, rsp call strchr_loop strchr_loop: cmp byte [rdi], sil ; check if current character is equal to character given in parameter je strchr_end ; go to end cmp byte [rdi], 0h ; test end of string je strchr_end ; go to end inc rdi ; move to next character of the string jmp strchr_loop ; loop strchr_end mov rax, rdi ; set function return mov rsp, rbp pop rbp 

请随时告诉我,如果有一种方法来改善它,再次感谢!