博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IAR EWAR 内联汇编 调用外部函数 Error[Og005], Error[Og006]
阅读量:4966 次
发布时间:2019-06-12

本文共 2247 字,大约阅读时间需要 7 分钟。

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 changedbx 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 _SavePCpop {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}");...}

 

转载于:https://www.cnblogs.com/shangdawei/p/4612680.html

你可能感兴趣的文章
XHTML与HTML区别
查看>>
软考-程序设计语言基础(编译原理)
查看>>
2016峰会:项目管理与高级项目管理(广州站)
查看>>
用JAVA编写浏览器内核之实现javascript的document对象与内置方法
查看>>
linux 命令之top
查看>>
有关远程设置的问题
查看>>
BZOJ 1800: [Ahoi2009]fly 飞行棋
查看>>
2019,2月份第三个星期,js小突破了一波,笔记
查看>>
洛谷 [P3033] 牛的障碍
查看>>
枚举类
查看>>
js-20170804-Math对象
查看>>
算法笔记_226:填符号凑算式(Java)
查看>>
KONG 安装 (在 CentOS 7 中)
查看>>
jquery 对HTML标签的克隆、删除
查看>>
【Pandas】Pandas求某列字符串的长度,总结经验教训
查看>>
【转载】 Python动态生成变量
查看>>
WPF入门教程系列九——布局之DockPanel与ViewBox(四)
查看>>
用C写的俄罗斯方块游戏 By: hoodlum1980 编程论坛
查看>>
实现WMSservice的时候,出现边缘的点或icon被切断的情况
查看>>
使用ALAssetsLibrary读取所有照片
查看>>