linux - 比较字符与 Intel x86_64 汇编

标签 linux assembly nasm x86-64

我是汇编 (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/

相关文章:

assembly - NASM cmp 错误 : invalid combination of opcode and operands

linux - 将一行中的第一个单词替换为上一行中的单词

linux - 系统重启后 Linux 中的服务自动启动

assembly - 二进制减法,进位位未设置?

c++ - 在循环条件 : why is strlen() called every time, 中,但 vector.size() 只被调用一次?

c - 无法在 Mac OSX 上的程序集 (x86-64) 和 C 文件中链接 printf 和 scanf

c++ - 如何在大型 Linux 应用程序中以编程方式设置 "yank the network cable"?

linux - 如何从源代码安装 nginx; : Got SSL_OP_MSIE_SSLV2_RSA_PADDING error

linux nasm 命令行 args 作为整数

linux - execve shellcode linux段错误