如何将参数从 C 传递给汇编函数?

问题描述

我正在尝试编写一些汇编代码以在 ubuntu 32 位机器上使用 write syscall 打印字符串。 到目前为止,我的工作是:

main.c 文件

extern int my_write();


int main(){
    my_write();
    return 0;
}

my_write.s 文件

.globl my_write
.type my_write,@function


.section .data
    str: .ascii "this is my string\n"
    len: .quad . - str

.section .text
my_write:

    mov $0x4,%eax # use the write syscall
    mov $0x1,%ebx # write to standard output
    mov $str,%ecx
    mov $len,%edx
    int $0x80      #trap

    mov $0x1,%eax #exit syscall
    mov $0x0,%ebx #exit status of 0
    int $0x80 #trap

我使用以下命令编译文件gcc -Wall main.c my_write.s -o main

它确实工作并打印“这是我的字符串”

但是字符串在汇编中是硬编码的。 我的问题是 - 如何从 C 文件中传递我自己的字符串? 类似的东西:

extern int my_write(char *str,int len);


int main(){
    char str[] = "this is my string\n";
    my_write(str,18);
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)