How do I call a C function in another module from inline assembler in IAR EWARM?

I have a bit of assembly in a hard fault handler.

The assembly is basically meant to pass the current stack pointer as a parameter (in R0). It looks like so...

  1. __asm(" mov r0, sp\n"
  2. " bl SavePC\n"
  3. " bx lr");

This works fine when SavePC is in the same c file.

However, when SavePC is placed in another c file I have no luck.

I have tried to IMPORT the function like so...

  1. __asm("IMPORT SavePC\n"
  2. " mov r0, sp\n"
  3. " bl SavePC\n"
  4. " bx lr");

... but I must be doing something incorrect. The compiler reports the following...

  1. Error[Og005]: Unknown symbol in inline assembly: "IMPORT"
  2. Error[Og005]: Unknown symbol in inline assembly: "SavePC"
  3. Error[Og006]: Syntax error in inline assembly: "Error[54]: Expression can not be forward"
  4. Error[Og005]: Unknown symbol in inline assembly: "SavePC"
  5. Error while running C/C++ Compiler

The c file with the assembly includes the header file with the SavePC prototype...

  1. extern void SavePC(unsigned long);

Suggestions?

Your code won't work even with a correct call.

  1. bl _SavePC // LR be changed
  2. bx lr // while (1){}

What do you think will be the value in the LR register in the bx lr instruction?

The address of the instruction itself!

The bl instruction has put it there. This is effectively a while (1);

with a bx instruction.

A nested function call looks more like this:

  1. push {lr}
  2. bl _SavePC
  3. pop {pc}

To get the stack register one uses the corresponding CMSIS functions:

  • __get_MSP() for the Main Stack Pointer (MSP)
  • __get_PSP() for the Process Stack Pointer (PSP)

Using extern is a bad habit since it is prone to errors. C-99 standard provides an safe alternative for extern. You should write the function prototype in the header file without extern keyword. Then include the header file in both C files. The linker is then responsible for linking the function in different files.

Example:

File : custom_header.h

  1. void SavePC(unsigned long);

File : source_c_file.c

  1. #include "custom_header.h"
  2.  
  3. void SavePC(unsigned long)
  4. {
  5. ....
  6. ....
  7.  
  8. ....
  9.  
  10. }

File : user_c_file.c

  1. #include "custom_header.h"
  2.  
  3. void someFunction(void)
  4. {
  5. .
  6. .
  7. .
  8.  
  9. __asm("mov r0, sp\n"
  10. " push {lr}
  11. " bl SavePC\n"
  12. " pop {pc}");
  13.  
  14. .
  15. .
  16. .
  17. }

IAR EWAR 内联汇编 调用外部函数 Error[Og005], Error[Og006]的更多相关文章

  1. IAR EWAR 内联汇编 Error[Og010], Error [Og005], Error [Og006]

    Error [Og005] + [Og006] when using inline assembler EW targets: 430, ARM, AVR EW component: C/C++ co ...

  2. gcc 在c代码中内嵌汇编调用c函数: 只是证明曾经我来过

    我怕我不写下来,将来我都不记得我还在 c 中嵌套过汇编语言,用汇编代码调用一个c函数的过程. 折腾了一下午,在网上查看相关的资料,然后照葫芦画瓢地在c代码中嵌套汇编,希望解决我所遇到的问题,可最后发现 ...

  3. x64内联汇编调用API(需intel编译器,vc不支持x64内联汇编)

    #include "stdafx.h" #include <windows.h> STARTUPINFOW StartInfo  = {0}; PROCESS_INFO ...

  4. 在Visual C++中使用内联汇编

    一.内联汇编的优缺点 因为在Visual C++中使用内联汇编不需要额外的编译器和联接器,且可以处理Visual C++中不能处理的一些事情,而且可以使用在C/C++中的变量,所以非常方便.内联汇编主 ...

  5. GNU C内联汇编(AT&amp;T语法)

    转:http://www.linuxso.com/linuxbiancheng/40050.html 内联汇编提供了可以在C或C++代码中创建汇编语言代码,不必连接额外的库或程序.这种方法对最终程序在 ...

  6. C语言中递归什么时候能够省略return引发的思考:通过内联汇编解读C语言函数return的本质

    事情的经过是这种,博主在用C写一个简单的业务时使用递归,因为粗心而忘了写return.结果发现返回的结果依旧是正确的.经过半小时的反汇编调试.证明了我的猜想,如今在博客里分享.也是对C语言编译原理的一 ...

  7. 通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制

    通过调用C语言的库函数与在C代码中使用内联汇编两种方式来使用同一个系统调用来分析系统调用的工作机制 前言说明 本篇为网易云课堂Linux内核分析课程的第四周作业,我将通过调用C语言的库函数与在C代码中 ...

  8. VC++线程函数内怎么调用外部函数

    VC++线程函数内怎么调用外部函数 1.把外部函数做成静态函数,就可以直接调用了.2.把外部函数所在的对象通过线程函数参数传到线程里面来,这样线程里可以使用此对象及其函数了.

  9. 最牛X的GCC 内联汇编

    导读 正如大家知道的,在C语言中插入汇编语言,其是Linux中使用的基本汇编程序语法.本文将讲解 GCC 提供的内联汇编特性的用途和用法.对于阅读这篇文章,这里只有两个前提要求,很明显,就是 x86 ...

随机推荐

  1. LCA 算法(一)ST表

    介绍一种解决最近公共祖先的在线算法,st表,它是建立在线性中的rmq问题之上.   代码:   //LCA: DFS+ST(RMQ) #include<cstdio> #include&l ...

  2. HDU 1869 六度分离 最短路

    解题报告: 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人, ...

  3. python运行execjs解密js

    [转]http://www.knowsky.com/1041161.html python 记一次计算qzonetoken经历 之前用python写了个发表说说的爬虫,但最近发现在post数据时返回不 ...

  4. asp.net动态增加服务器端控件并提交表单

    为什么要用原生的呢? 1.目的 原生出现浏览器兼容性问题 极少,不用测试多浏览兼容性 .需要考虑到市面上的其他垃圾浏览器. 2.性能不好 如果不考虑第一条 你可以换一种方式 直接上代码 .aspx页面 ...

  5. springboot配置mybatis的mapper路径

    1.在src/main/resources/目录下新建mybatis文件夹,将xxx.xml文件放入该文件夹内 2.在application.yml文件中配置: mybatis: configurat ...

  6. 过滤掉文本中的javascript标签代码

    2014年1月21日 11:51:19 php代码: $content = preg_replace('#<\s*[script].*>#', '', $a);//有些攻击可以在scrip ...

  7. window BIOS设置硬盘启动模式

      bios如何设置硬盘启动模式?BIOSD硬盘模试主是要针对IDE接口的硬盘和SATA接口的硬盘来设置的.以前的主板只支持一种类型.现在的智能笔记本主板支持:IDE Mode.AHCI Mode.下 ...

  8. .NetCore SkyWalking APM实现服务器监控环境安装及基础使用

    下载Java 8 SDK wget  --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...

  9. Net WebAPI2

    SwaggerUI ASP.Net WebAPI2   目前在用ASP.NET的 WebAPI2来做后台接口开发,在与前台做测试的时候,总是需要发送一个demo给他,但是这样很麻烦的,他还有可能记不住 ...

  10. 【LOJ】#2076. 「JSOI2016」炸弹攻击

    题解 我冷静一下,话说如果去掉建筑和R的限制好像是模拟退火吧 然后开始写模拟退火了,起始点就随机一个敌人作为起始点 没对着数据写了一下获得了70pts,感到美滋滋 然后对着数据卡了很久--发现有个数据 ...