我是汇编 (Intel x86_64) 的新手,我正在尝试重新编码 C 库中的一些函数。我在 64 位 Linux 上使用 NASM 进行编译。
我在使用 strchr 函数时遇到错误,我找不到解决方案...
提醒一下,这里摘自 strchr 的手册页:
char *strchr(const char *s, int c);
The strchr() function returns a pointer to the first occurrence of the character c in the string s.
这是我尝试过的:
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
这会在字符串的 ned 上返回一个指针,但找不到该字符...
我认为这是行不通的:
cmp [r12], r13
我用这个测试过,它有效:
cmp [r12], 97 ; 97 = 'a' in ASCII
例子:
char *s;
s = strchr("blah", 'a');
printf("%s\n", s);
返回:
ah
但我不能让它与寄存器比较一起工作。我做错了什么,我该如何解决?
最佳答案
首先,感谢您的帮助!我想我对自己在做什么有了更好的理解。
我遇到了接收 8 位参数而不是 64 位参数的问题 rdi...但是一位 friend 告诉我第一个 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
请随时告诉我是否有改进的方法,再次感谢!
关于linux - 比较字符与 Intel x86_64 汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998521/