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...

__asm("    mov     r0, sp\n"
" bl SavePC\n"
" 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...

__asm("IMPORT SavePC\n"
" mov r0, sp\n"
" bl SavePC\n"
" bx lr");

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

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

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

extern void SavePC(unsigned long);

Suggestions?

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

bl _SavePC             // LR be changed
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:

push {lr}
bl _SavePC
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

void SavePC(unsigned long);

File : source_c_file.c

#include "custom_header.h"

void SavePC(unsigned long)
{
....
.... .... }

File : user_c_file.c

#include "custom_header.h"

void someFunction(void)
{
.
.
. __asm("mov r0, sp\n"
" push {lr}
" bl SavePC\n"
" pop {pc}"); .
.
.
}

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. Zookeeper笔记之命令行操作

    $ZOOKEEPER_HOME/bin下的zkCli.sh进入命令行界面,使用help可查看支持的所有命令: 一.节点相关操作 create [-s] [-e] path data acl creat ...

  2. Oracle PLSql配置

    1.安装Oracle客户端或者服务端 2.配置环境变量 <1>.一般如果安装了Oracle客户端或者服务端的话,在环境变种的Path中有Oracle的安装路径(计算机-属性-高级系统设置- ...

  3. 20155303 实验三 敏捷开发与XP实践

    20155303 实验三 敏捷开发与XP实践 目录 一.编码标准 任务一:在IDEA中使用工具(Code->Reformate Code)格式化代码,并学习Code菜单的功能 二.敏捷开发与XP ...

  4. linux笔记_day08

    1.文件权限管理 1)chown:改变文件属主(只有管理员才能使用这个命令)(change owner) chown USERNAME file... -R :修改目录以及内部文件的属主 --refe ...

  5. 【CTF WEB】XSS-https://alf.nu/alert1

    XSS练习平台 https://alf.nu/alert1 Warmup 1");alert(1)// Adobe 1");alert(1)// JSON </script& ...

  6. 浅谈tomcat中间件的优化【转】

    今天来总结一下tomcat的一些优化的方案,由于本人才疏学浅,写的不好,勿喷! tomcat对于大多数从事开发工作的童鞋应该不会很陌生,通常做为默认的开发环境来为大家服务,不过tomcat默认的一些配 ...

  7. Windows 8.1/10配置VS 2013 + MPI开发环境

    关于win上安装MPI开发环境,网上能搜到的教程貌似都很老,还需要在管理员账户开发,感觉很诡异,于是自己摸索了下,不需要管理员账户,总结如下. 1. 准备 VS 2013(VS 2010及以上都行) ...

  8. centos7环境配置haproxy实现mysql数据库和redis代理服务器

    centos7环境配置haproxy实现mysql数据库代理 我们通常会碰到这样的业务场景: b主机和c数据库在同一个内网,a主机不能直接访问c数据库,我们可以通过在b主机上搭建代理让a访问c数据库, ...

  9. [转]如何取得当前正在执行的shell脚本的绝对路径?

    来源:http://sexywp.com/bash-how-to-get-the-basepath-of-current-running-script.htm 如题,一般我们写Shell脚本的时候,都 ...

  10. pycharm+PyQt5+python最新开发环境配置

    Python 3.6https://www.python.org/downloads/windows/========================================PyQt5 pip ...