代码之家  ›  专栏  ›  技术社区  ›  Joao Pereira

x64程序集反向字符串函数[重复]

  •  0
  • Joao Pereira  · 技术社区  · 2 年前

    我正在学习组装,并面临以下问题:

    在函数中

    asm_func4:
            push rbp                ; preserve rbp
            call _strlen            ; get rdi strlen
            mov rdx, rax            ; copy strlen to reverse counter 
            sub rdx, 2              ; remove \n and 0h 
            xor rcx, rcx            ; clear counter        
    
    _reverse_str:
            mov al, [rdi+rdx]       ; copy last char to al
            mov byte [rsi+rcx], al  ; copy al to rsi 
            inc rcx                 ; increment destination counter
            dec rdx                 ; decrement reverse counter 
            jnz  _reverse_str       ; not zero? do it again
    
    _reverse_end:
            mov byte [rsi+rcx], 0h  ; null-terminate the string
            mov rax,rsi             ; copy to rax to return the func
            pop rbp                 ; must be preserved
            ret
    

    我得到了一个 SIGSEGV 同时执行行`mov byte[rsi+rcx],al;将al复制到rsi。 我在这里做错了什么?

    这是“gdb”信息:

    0x0000555555555317 in _reverse_str ()
    1: (char *) $rdi = 0x555555556086 "Hello from asm_func4\n"
    2: (char *) $rsi = 0x55555555609b ""
    3: (char *) $rax = 0x34 <error: Cannot access memory at address 0x34>
    4: (char) $al = 52 '4'
    5: (int) $rcx = 0
    6: (int) $rdx = 19
    (gdb) x/i $pc
    => 0x555555555317 <_reverse_str+3>: mov    %al,(%rsi,%rcx,1)
    (gdb) stepi
    
    Program received signal SIGSEGV, Segmentation fault.
    
    

    函数是从C:

    #include <stdio.h>
    #include "prog.h"
    
    
    int main(void)
    {
    
        printf("%s", asm_func4("Hello from asm_func4\n"));
    
        return 0;
    }
    

    程序h是:

    #pragma once
     char* asm_func4(char *);
    
    0 回复  |  直到 2 年前
    推荐文章