linux – 将字符与Intel x86_64程序集进行比较
发布时间:2020-12-13 23:19:54 所属栏目:Linux 来源:网络整理
导读:我是汇编(Intel x86_64)的新手,我正在尝试从C库中重新编写一些函数.我在64位 Linux上并使用NASM进行编译. 我的strchr函数有错误,我找不到解决方案…… 这里提醒一下strchr手册的摘录: char *strchr(const char *s,int c); The strchr() function returns a
我是汇编(Intel x86_64)的新手,我正在尝试从C库中重新编写一些函数.我在64位
Linux上并使用NASM进行编译.
我的strchr函数有错误,我找不到解决方案…… 这里提醒一下strchr手册的摘录:
这是我尝试过的: 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("%sn",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 请随时告诉我是否有办法改进它并再次感谢! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |