查看: 2282|回复: 0
收起左侧

risc-v 汇编函数与C语言函数相互调用

[复制链接]

  离线 

  • TA的每日心情
    奋斗
    2021-3-3 12:32
  • 签到天数: 10 天

    [LV.3]

    发表于 2020-8-24 11:44:20 | 显示全部楼层 |阅读模式

    有人预言,RISC-V或将是继Intel和Arm之后的第三大主流处理器体系。欢迎访问全球首家只专注于RISC-V单片机行业应用的中文网站

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    本帖最后由 皋陶 于 2020-8-27 14:08 编辑

    硬件:RISC-V 开发板


    国内芯片技术交流-risc-v 汇编函数与C语言函数相互调用risc-v单片机中文社区(1)



    软件环境:Linux X220 5.0.0-37-generic #40~18.04.1-Ubuntu SMP Thu
    Nov 14 12:06:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    IDE:VSCode1.41.1 + platformio IDE+ gd32v


    汇编文件:Add.S


    (注意是大写S) 不然在VSCode里会编译错误:


    1. riscv64-unknown-elf-as: unrecognized option '-x'        
    复制代码
    1. .file Add.S
    2. .section .text
    3. .globl Add
    4. .type Add,@function ;可选
    5. Add:
    6. add a0,a0,a1                                         ;// a0对应参数1,a1对应参数2
    7. ret                                                             ;且a0带返回值
    复制代码
    1.                                                                                                 图1-1
    复制代码


    上述的汇编为叶文件(不调其它函数的文件)汇编函数格式,如果需要调用其它函数需要按 图2-1


    在c/c++文件里调用汇编语言编写的函数


    1. //先声明函数原型
    2. int Add(int i,int j);

    3. int main(void)
    4. {
    5.     //...other codes
    6.     int rtn=Add(100,200); // a0对应参数1,a1对应参数2,...a7对应参数8(这里寄存器为接口名称:ABI Name)
    7.     printf("%d",rtn);
    8.     //...other codes
    9.     return 0;
    10. }
    复制代码
    1.                                                                                                 图1-2
    复制代码


    汇编调用汇编函数 calculate(int x,int y,int z) #计算x-(y+z)


    文件:Sub.S

    1. .section .text
    2. .globl Sub
    3. Sub:
    4. sub a0,a0,a1
    5. ret
    复制代码


    文件:calculate.S #;函数调用模板

    1. .section .text         #";函数调用模板"
    2. .equ framesize,16 #"frameszie=ra+a0+a1+...an总字符数"
    3. .globl calculate #计算x-(y+z)
    4. .type calculate,@function
    5. calculate:
    6. #";calculate(a0,a1,a2)//这里3个参数 framesize=(3+1)*4  #假定每个参数4个字节"
    7. addi sp,sp,-framesize                              #";堆指针上浮增加堆空间"
    8. sw ra,framesize-4(sp)
    9. sw s0,framesize-8(sp)
    10. sw  s1,framesize-12(sp)
    11. sw  s2,framesize-16(sp)
    12. mv s2,a0                                    #"Add 参数1 x"
    13. mv s1,a1                                    #"Add 参数2 y"
    14. mv s0,a2                                    #"Add 参数3 z"
    15. #-----开始操作---------
    16. add a1,s0,s1                            #(y+z)
    17. mv a0,s2                                   # x
    18. jal Sub                                       #x-(y+z)
    19. #------结束操作--------
    20. lw ra,framesize-4(sp)                                       ;"取回返回地址"
    21. lw s0,framesize-8(sp)
    22. lw s1,framesize-12(sp)
    23. lw  s2,framesize-16(sp)
    24. addi sp,sp,framesize                                       ;"返还堆空间"
    25. ret
    复制代码


    calculatePrint.S
    汇编调用汇编Sub函数及c/c++函数printf("%d-(%d+%d)=%d",x,y,z,r)

    1. .section .text
    2. .equ framesize,16 #"frameszie=ra+a0+a2+...an总字符数"
    3. .globl calculatePrint #"计算x-(y+z)"
    4. .type calculatePrint,@function
    5. calculatePrint:
    6. #";calculate(a0,a1,a2)//这里3个参数 framesize=(3+1)*4  #假定每个参数4个字节"
    7. #";printf('%d-(%d+%d)=%d',a0,a1,a2,a3);"
    8. addi sp,sp,-framesize
    9. sw ra,framesize-4(sp)
    10. sw s0,framesize-8(sp)
    11. sw  s1,framesize-12(sp)
    12. sw  s2,framesize-16(sp)
    13. mv s2,a0                                    #"Add 参数1 x"
    14. mv s1,a1                                    #"Add 参数2 y"
    15. mv s0,a2                                    #"Add 参数3 z"
    16. #-----开始操作---------
    17. add a1,s0,s1                            #(y+z)
    18. mv a0,s2                                   # x
    19. jal Sub                                       #x-(y+z)
    20. #"printf('%d-(%d+%d)=%d',x,y,z,r)"
    21. mv a4,a0                                #"返回值"
    22. mv a3,s0                                #x
    23. mv a2,s1                                #y
    24. mv a1,s2                                #z
    25. la  a0,format                       #"format"
    26. jal printf
    27. #------结束操作--------
    28. lw ra,framesize-4(sp)
    29. lw s0,framesize-8(sp)
    30. lw s1,framesize-12(sp)
    31. lw  s2,framesize-16(sp)
    32. addi sp,sp,framesize
    33. ret
    34. .section  .rodata
    35. format:
    36.     .string "%d-(%d+%d)=%d"
    复制代码
    本篇完,感谢关注:RISC-V单片机中文网




    上一篇:Rocket - spec - RISC-V规范整理
    下一篇:RISC-V 使能、禁止、恢复全局中断
    RISCV作者优文
    全球首家只专注于RISC-V单片机行业应用的中文网站
    回复

    使用道具 举报

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    关闭

    RISC-V单片机中文网上一条 /2 下一条



    版权及免责声明|RISC-V单片机中文网 |网站地图

    GMT+8, 2024-4-19 23:57 , Processed in 1.013806 second(s), 48 queries .

    快速回复 返回顶部 返回列表