IAR EWAR 内联汇编 调用外部函数 Error[Og005], Error[Og006]

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}");

.
.
.
}
复制代码

 

posted @   IAmAProgrammer  阅读(1670)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2014-07-01 STM32 DFU -- Device Firmware Upgrade
点击右上角即可分享
微信分享提示