打印

如何在汇编代码中调用C语言函数?

[复制链接]
3369|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
george3023|  楼主 | 2007-9-24 20:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
小弟用的是TMS320LF2407,编译器是CCS2.2。小弟尝试在汇编的中断服务子程序中调用C语言函数,发现程序运行不正常。于是将相关C文件生成汇编代码,发现只要是在C中使用了右移或除法指令,在相应生成的汇编代码中就会出现RSXM这条指令,屏蔽除法和右移指令就不会产生RSXM这条指令,并且程序可以正常运行。不知道哪位大哥在汇编代码中调用过c语言函数,请不吝赐教!

相关帖子

沙发
george3023|  楼主 | 2007-9-26 18:54 | 只看该作者

TI的回复

以下是TI给我的回复:
Regarding your inquiry, To call a C subroutine from assembly you have to follow below mentioned steps:
           
1) Declare and define the C subroutine in a .c file
                                      
2) To access a C function or object from assembly language, declare the C object with     
.global or .ref. This creates an undeclared external reference that the linker resolves.  
                                                                                         
3)When you call a C function from assembly language, push any arguments onto the stack in 
reverse order. Pop them off after calling the function.
                                  
4) The compiler adds an underscore ( _ ) to the beginning of all identifiers(that is,     
labels). In assembly language modules, you must use an underscore prefix for all objects  
that are to be accessible from C. For example, a C object named x is called _x in         
assembly language. So if you have to call the C subroutine "func_c" in assembly then      
declare the subroutine as .global _func_c and then call the subroutine.
                  
5)Functions must return values in the accumulator. 16-bit integer values and pointers     
must be loaded in the accumulator with proper sign extension.

使用特权

评论回复
板凳
george3023|  楼主 | 2007-9-26 18:56 | 只看该作者

我的实际经验

我在调试过程中发现按如下方法可以完成在汇编代码中调用C语言函数:

/*C file  Begin */

void func_a();

void main()
{
    func_a();
}
/*C file  End */

/*ASM file Begin*/

     ref _func_a
....
....
....        
    mar *,ar1
    mar *+
    sst #1,*+
    sst #0,*+
    call _func_a
    mar *-
    lst #0,*-
    lst #1,*- 
....
....
....
     
/*ASM file  End */

使用特权

评论回复
地板
george3023|  楼主 | 2007-9-26 18:58 | 只看该作者

请高手批评指正

上面的做法如有问题,还请高手指正!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

68

帖子

1

粉丝