共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 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】智能可穿戴设备AR/VR如何引领科技新潮流! | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
【有奖活动】震撼来袭!这场直播将直击工程师的心灵! | |
“我想要一部加热台”活动,不花钱赢取开发好工具!活动开始了哦 | |
【有奖活动】伙伴们:您的“需求”解决了吗? |
打赏帖 | |
---|---|
【换取手持数字示波器】N32G430点亮数码管与串口打印、ADC被打赏40分 | |
老胖子聊电路--分析一个反馈电路被打赏50分 | |
【换取手持数字示波器】+点亮WS2812RGB灯被打赏50分 | |
【换取手持数字示波器】+线性调整器理论知识分享被打赏40分 | |
放大↔震荡被打赏50分 | |
【换取手持数字示波器】RTduino驱动ssd1306+sht31温湿度计被打赏40分 | |
【换取手持数字示波器】称植国产开源TobudOS到雅特力AT32F425被打赏40分 | |
【分享评测,赢取加热台】+Telink-泰凌微电子B91点灯篇被打赏20分 | |
【换取手持数字示波器】+精英板驱动LCD屏被打赏50分 | |
【换取手持数字示波器】+DFR0216全彩灯带WS2812示例分享被打赏50分 |