共2条
1/1 1 跳转至页
Aq430,wrappers,for,assembly,coded,functions Aq430 —— C wrappers for assembly cod
问
If you want to write a C callable function in assembly code, it is often most convenient to code it as a C function whose body is a block of in-line code. The benefit is that, if you declare all variables to be used in C, then the compiler will look after saving/restoring register on entry/exit and will look after assigning register numbers. Such a function would have a structure like this:
int
f( register int x, register int y )
{
register int a, b, c;
/$
....assembly code using x, y, a, b, c
$/
return c;
}
In this case, only the declarations and return statement are in C; everything else is in assembler. Note that the code uses the register keyword in declarations in order to force the compiler to put them into registers, so that they CAN be used as registers in the in-line code.
If, in inline assembly code, you refer to a register directly (by using the name of the register), then you must save/restore it on entry/exit to/from the inline assembly code. The best way to avoid such concerns is to always declare your registers as C variables and then refer to them using name in the inline assembly code. If you want to use a scratch register (r12-r15) which is not being used to hold an incoming function argument, then you should bind the C variable to the scratch register directly. For instance, to bind a variable q to register R15, you would write
register int q R[15];
In general, if the wrapper function does not contain any function calls, then arguments passed in registers will be used as is. Otherwise, they will be copied to other, non-scratch registers. When in doubt, look at the assembly code output file generated by the compiler, as it will contain compiler generated comments indicating which register it is using for each register variable.
int
f( register int x, register int y )
{
register int a, b, c;
/$
....assembly code using x, y, a, b, c
$/
return c;
}
In this case, only the declarations and return statement are in C; everything else is in assembler. Note that the code uses the register keyword in declarations in order to force the compiler to put them into registers, so that they CAN be used as registers in the in-line code.
If, in inline assembly code, you refer to a register directly (by using the name of the register), then you must save/restore it on entry/exit to/from the inline assembly code. The best way to avoid such concerns is to always declare your registers as C variables and then refer to them using name in the inline assembly code. If you want to use a scratch register (r12-r15) which is not being used to hold an incoming function argument, then you should bind the C variable to the scratch register directly. For instance, to bind a variable q to register R15, you would write
register int q R[15];
In general, if the wrapper function does not contain any function calls, then arguments passed in registers will be used as is. Otherwise, they will be copied to other, non-scratch registers. When in doubt, look at the assembly code output file generated by the compiler, as it will contain compiler generated comments indicating which register it is using for each register variable.
共2条
1/1 1 跳转至页
回复
| 有奖活动 | |
|---|---|
| 硬核工程师专属补给计划——填盲盒 | |
| “我踩过的那些坑”主题活动——第002期 | |
| 【EEPW电子工程师创研计划】技术变现通道已开启~ | |
| 发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
| 【EEPW在线】E起听工程师的声音! | |
| 高校联络员开始招募啦!有惊喜!! | |
| 【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
| 送您一块开发板,2025年“我要开发板活动”又开始了! | |
我要赚赏金打赏帖 |
|
|---|---|
| 在FireBeetle2ESP32-C5上实现温湿度检测和显示被打赏¥20元 | |
| 在FireBeetle2ESP32-C5上实现光照强度检测及显示被打赏¥21元 | |
| 以FireBeetle2ESP32-C5实现数据识读播报被打赏¥19元 | |
| 【STM32F103ZET6】14:实测STM32F1的串口输出任务的挂起与恢复功能被打赏¥27元 | |
| Chaos-nano在压力容器监控系统中的项目应用被打赏¥23元 | |
| 基于FireBeetle2ESP32-C5的WS2812B彩色灯带控制被打赏¥21元 | |
| SWM221CBT7显示开发板驱动TFT显示屏被打赏¥19元 | |
| SWM221CBT7显示开发板驱动OLED屏显示被打赏¥19元 | |
| SWM221CBT7显示开发板及其使用被打赏¥20元 | |
| 揭秘Chaos-nano:ArduinoProMini轻量级操作系统开发框架深度解析与实战应用被打赏¥19元 | |
我要赚赏金
