Keil C51 8051汇编函数参数和返回值放在哪里,C语言调用汇编函数时汇编函数如何处理参数?

Keil C51 8051汇编函数参数和返回值放在哪里,C语言调用汇编函数时汇编函数如何处理参数?

作者 将狼才鲸
创建日期 2024-07-21

  • 参考网址:

  • Keil C51 C语言中调用汇编函数时,最多使用三个参数,默认第一个参数从R7开始放,第二个参数从R5开始放,第三个参数从R3开始放,如果是2字节的int,那么遵循8051的大端模式,高字节放在R2、R4、R6,低字节放在R3、R5、R7;如果是指针参数,无论是第一第二第三个参数,都放在R1~R3,存储类型是R3,指针值是0xR2R1(注意此时是小端模式存储);超过三个的参数请用外部RAM来实现;

  • 汇编函数将返回值返回给C语言时,返回值需要放在R7开始的位置,char就放在R7,int是0xR6R7,long和float是0xR4R5R6R7,指针是类型在R3,指针值0xR2R1;

  • 汇编里的函数如果要给C语言用,那么函数名(标号)前要叫下划线,例如 _LOOP: NOP; RET;,C语言调用时去掉下划线,如:LOOP();

    • 其实加下划线是代表有参数调用,但是无参数的汇编函数你也这么加并没有问题;
  • 想找原文的,在Keil安装目录的C:\Keil_v5\C51\Hlp\c51.chm,在里面搜索Parameter Passing或者Passing in Registers,原文摘抄如下:

Passing in Registers

C functions may pass parameters in registers and fixed memory locations. A maximum of 3 parameters may be passed in registers. All other parameters are passed using fixed memory locations. The following tables define which registers are used for passing parameters.

Arg Number char, 1-byte ptr int, 2-byte ptr long, float generic ptr
1 R7 R6 & R7 (MSB in R6,LSB in R7) R4—R7 R1—R3 (Mem type in R3, MSB in R2, LSB in R1)
2 R5 R4 & R5 (MSB in R4,LSB in R5) R4—R7 R1—R3 (Mem type in R3, MSB in R2, LSB in R1)
3 R3 R2 & R3 (MSB in R2,LSB in R3) R1—R3 (Mem type in R3, MSB in R2, LSB in R1)

The following examples clarify how registers are selected for parameter passing.

Declaration Description
func1 ( int a) The first and only argument, a, is passed in registers R6 and R7.
func2 ( int b, int c, int *d) The first argument, b, is passed in registers R6 and R7. The second argument, c, is passed in registers R4 and R5. The third argument, d, is passed in registers R1, R2, and R3.
func3 ( long e, long f) The first argument, e, is passed in registers R4, R5, R6, and R7. The second argument, f, cannot be located in registers since those available for a second parameter with a type of long are already used by the first argument. This parameter is passed using fixed memory locations.
func4 ( float g, char h) The first argument, g, passed in registers R4, R5, R6, and R7. The second parameter, h, cannot be passed in registers and is passed in fixed memory locations.

Copyright © Keil, An ARM Company. All rights reserved.

相关推荐

  1. win32 汇编调用C标准库函数

    2024-07-21 15:00:02       48 阅读
  2. 数学函数参数返回探秘

    2024-07-21 15:00:02       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 15:00:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 15:00:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 15:00:02       45 阅读
  4. Python语言-面向对象

    2024-07-21 15:00:02       55 阅读

热门阅读

  1. Try ubuntu core (by quqi99)

    2024-07-21 15:00:02       21 阅读
  2. 独孤思维:副业赚钱,易如反掌

    2024-07-21 15:00:02       16 阅读
  3. Composition API对比Options API

    2024-07-21 15:00:02       15 阅读
  4. C# 删除DataTable里符合条件的行

    2024-07-21 15:00:02       17 阅读
  5. centos7更换yum源

    2024-07-21 15:00:02       16 阅读
  6. c++应用网络编程之五Windows常用的网络IO模型

    2024-07-21 15:00:02       17 阅读
  7. opencv—常用函数学习_“干货“_12

    2024-07-21 15:00:02       17 阅读
  8. AI文章特点详细分析

    2024-07-21 15:00:02       18 阅读
  9. ubuntu24无法网络无法连接的问题

    2024-07-21 15:00:02       16 阅读
  10. mqtt协议有哪些机制

    2024-07-21 15:00:02       17 阅读