使用引用参数从 c 调用汇编函数

标签 c assembly x86 nasm

我正在实现一个汇编函数来反转字符串。此函数在 C 中以签名 void strrev(char *str) ; 在函数中似乎一切正常,使用 gdb 进行调试,但在调用 后打印字符串的内容>strrev() 只打印空字符串。

getlast:
        cmp     byte [edi], 0
        je      reverse_string
        inc     edi
        inc     ecx
        jmp     getlast

reverse_string:
        cmp     esi, edi
        jae     reversed
        mov     al, [esi]
        mov     bl, [edi]
        mov     [esi], bl
        mov     [edi], al
        inc     esi
        dec     edi

        jmp     reverse_string

主.c

void strrev(char *str) ;

int main() {
   char *str1;

   str1 = strdup("Hello World") ;
   strrev(str1) ;
   printf("str1 = \"%s\"\n", str1) ;
}

最佳答案

这个

    cmp     byte [edi], 0
    je      reverse_string

会导致这个

    mov     bl, [edi]
    mov     [esi], bl

在字符串的开头写0。

您需要从字符串终止符后退。

关于使用引用参数从 c 调用汇编函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16016829/

相关文章:

c - 函数终止时出现段错误

linux - x86 指令格式 : "ba 0e 00 00 00" . .. "mov $0xe,%edx"

c - LP线程 : Pointer to a structure or address of a structure?

assembly - 如何在x86中仅使用2条连续的leal指令将寄存器乘以37?

c++ - Lambdas 作为环境的闭包。 RIP寄存器的关键作用

gcc - 如何在 NASM 中添加两个数字,整数和 float ?

linux - x86 汇编 linux 中如何解释系统调用

c - getint() 函数乘以 10?

c - 文件读取返回重复两次

c - 如何在C中序列化数据