0

i'm stuck with a NASM code, i'm trying to reproduce the strchr function in NASM and i can't figure out two things : -Why when i try to print the result i got a warning on wrong format with a printf %s even if i'm trying to return a pointer on a string, -And also why i keep getting SIGSEV Signal, but one problem is related to the other i think.

Here is my code, if someone could help, it will be greatly appreciated :

[BITS 64]
        global strchr_asm


strchr_asm:
        push rbp    
        mov rbp, rsp

        mov r9, -1
        mov rax, rsi
        mov r8b, BYTE[rdi]

loop:
        inc r9
        cmp BYTE[rax+r9], 0
        je  endF
        cmp BYTE[rax+r9], r8b
        je endV
        jmp loop

endF:
        add rax, r9
        jmp last

endV:
        add rax, r9

last:
        mov rsp, rbp
        pop rbp
        ret
Hakeem El Bakka-lee
  • 756
  • 1
  • 7
  • 23
  • The only thing that strikes me as odd is starting out with `push r` and `mov rb rsp` and ending with `mov rsp,rbp` and `pop rbp` at the end. That moves the stack's base into the stack pointer, _then_ returns, I believe (it's been at least ten years since I've written assembly code), which could easily be causing your crash. And if you're over-popping the stack, it may explain your output, too. – John C Mar 27 '14 at 19:24
  • The code in your question doesn't even assemble (it looks like it's missing some characters). Please edit your question so that it contains _the exact_ code that you're assembling. – Michael Mar 27 '14 at 20:53
  • Sorry about the miss editing, i didn't see it. I found the solution, it is because i was using BYTE[rdi] and not directly the sub register sil. now and don't get segfaults anymore ;). – Hakeem El Bakka-lee Mar 29 '14 at 18:08

0 Answers0