在C中嵌套汇编代码难吗?

作者:黄工

公众号:strongerHuang

做底层软件开发的,或多或少都会接触一些汇编代码,今天就来说说关于汇编在C中的定义和调用,以及举例说明嵌套汇编代码。

一、嵌套汇编说明

有认真研究,或者说细心一点的读者应该都知道:C中定义汇编代码与编译器有关

比如:你在core_cm4.h文件会看到如下的代码:

#if   defined ( __CC_ARM )
  #define __ASM            __asm                                      /*!< asm keyword for ARM Compiler          */
  #define __INLINE         __inline                                   /*!< inline keyword for ARM Compiler       */
  #define __STATIC_INLINE  static __inline

#elif defined ( __GNUC__ )
  #define __ASM            __asm                                      /*!< asm keyword for GNU Compiler          */
  #define __INLINE         inline                                     /*!< inline keyword for GNU Compiler       */
  #define __STATIC_INLINE  static inline

#elif defined ( __ICCARM__ )
  #define __ASM            __asm                                      /*!< asm keyword for IAR Compiler          */
  #define __INLINE         inline                                     /*!< inline keyword for IAR Compiler. Only available in High optimization mode! */
  #define __STATIC_INLINE  static inline

#elif defined ( __TMS470__ )
  #define __ASM            __asm                                      /*!< asm keyword for TI CCS Compiler       */
  #define __STATIC_INLINE  static inline

#elif defined ( __TASKING__ )
  #define __ASM            __asm                                      /*!< asm keyword for TASKING Compiler      */
  #define __INLINE         inline                                     /*!< inline keyword for TASKING Compiler   */
  #define __STATIC_INLINE  static inline

#elif defined ( __CSMC__ )
  #define __packed
  #define __ASM            _asm                                      /*!< asm keyword for COSMIC Compiler      */
  #define __INLINE         inline                                    /*use -pc99 on compile line !< inline keyword for COSMIC Compiler   */
  #define __STATIC_INLINE  static inline
#endif

如果你写过Keil C51,你还会发现有如下(通过预处理)嵌套汇编:

#pragma asm

; Assembler Code Here

#pragma endasm

所以,你会发现,不同的编译器,汇编代码还是有差异。当然,这里主要是说C中嵌套汇编与编译器有关。

二、C中嵌套汇编代码

常见两种定义:

1.在C函数中定义一段汇编代码;

2.在C文件中定义一个汇编函数;

(当然,两个意思差不多,都是在C中嵌套汇编)

上面说了C中定义汇编代码与编译器有关,换句话说:不同编译器解析汇编代码的方式不同。

这里还是拿core_cm3.c来举例说明,定义一个__get_PSP函数。

在Keil MDK中定义:

__ASM uint32_t __get_PSP(void)
{
  mrs r0, psp
  bx lr
}

在IAR EWARM中定义:

uint32_t __get_PSP(void)
{
  __ASM("mrs r0, psp");
  __ASM("bx lr");
}

__asm(__ASM)关键字用于调用内联汇编程序,并且可在 C 或 C++ 语句合法时出现。

三、更多举例

下面举一些常见例子。

1.FreeRTOS中portmacro.h文件下源代码:

static portFORCE_INLINE void vPortRaiseBASEPRI( void )
{
uint32_t ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;

  __asm
  {
    /* Set BASEPRI to the max syscall priority to effect a critical p. */
    msr basepri, ulNewBASEPRI
    dsb
    isb
  }
}

2.FreeRTOS中port.c文件下源代码:

__asm void xPortPendSVHandler( void )
{
  extern uxCriticalNesting;
  extern pxCurrentTCB;
  extern vTaskSwitchContext;

  PRESERVE8

  mrs r0, psp
  isb

  ldr  r3, =pxCurrentTCB    /* Get the location of the current TCB. */
  ldr  r2, [r3]

  stmdb r0!, {r4-r11}      /* Save the remaining registers. */
  str r0, [r2]        /* Save the new top of stack into the first member of the TCB. */

  stmdb sp!, {r3, r14}
  mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
  msr basepri, r0
  dsb
  isb
  bl vTaskSwitchContext
  mov r0, #0
  msr basepri, r0
  ldmia sp!, {r3, r14}

  ldr r1, [r3]
  ldr r0, [r1]        /* The first item in pxCurrentTCB is the task top of stack. */
  ldmia r0!, {r4-r11}      /* Pop the registers and the critical nesting count. */
  msr psp, r0
  isb
  bx r14
  nop
}

3.内核复位

这是之前分享过的一篇文章《STM32复位来源》中的代码:

__asm void NVIC_CoreReset_a(void)
{
  LDR R0, =0xE000ED0C
  LDR R1, =0x05FA0001
  STR R1, [R0]
deadloop_Core
  B deadloop_Core
}

举了这些常见例子,没有说透,但相信只要认真理解了都能明白。

1.RISC-V 处理器嵌入式开发网课上线!

2.刚刚!官方发布:中美达成第一阶段贸易协议,美分阶段取消关税!

3.一幅趣味漫画,让你秒懂Linux内核!

4.NXP推出了新一款8位MCU,看看适合你的应用不?

5.手机CPU与电脑CPU性能究竟相差多少?

6.FPGA老大把研发5年的软件平台开源了!

免责声明:本文系网络转载,版权归原作者所有。如涉及作品版权问题,请与我们联系,我们将根据您提供的版权证明材料确认版权并支付稿酬或者删除内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值