NASM奇怪的行为与al

时间:2014-03-12 16:42:35

标签: assembly nasm strchr

我尝试运行以下程序:

C代码:

int  main()
    {
     char  *s1 = "hello";

     printf("string : %s\n", strchr(s1, 'l'));
    }

汇编代码:

global strchr

    section .text

strchr:
    push rbp
    mov rbp, rsp

strchr_loop:
    mov al, byte [rsi]       ; My bug come from here
    cmp byte [rdi], al       ; and from here
    je strchr_end
    cmp byte[rdi], 0
    jz strchr_nul
    inc rdi
    jmp strchr_loop

strchr_end:
    mov rax, rdi
    mov rsp, rbp
    pop rbp

    ret

strchr_nul:
    mov rax, 0
    mov rsp, rbp
    pop rbp

    ret

当我执行此操作时,我遇到了分段错误。

但是当我用值'l'替换bug行而不是使用al时,程序正在运行

cmp byte [rdi], 'l'       ; It's working

0 个答案:

没有答案