作者:黃工 公眾號: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 section. */ 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} 舉了這些常見例子,沒有說透,但相信只要認真理解了都能明白。
-
C語言
+關注
關注
180文章
7628瀏覽量
139696 -
編程
+關注
關注
88文章
3673瀏覽量
94692 -
代碼
+關注
關注
30文章
4880瀏覽量
69999
發布評論請先 登錄
相關推薦
技術干貨驛站 ▏深入理解C語言:嵌套循環與循環控制的底層原理

C語言如何處理函數的返回值
EE-134:為SHARC系列編寫C兼容匯編代碼中斷處理程序

EE-132:使用VisualDSP將C代碼和數據模塊放入SHARC存儲器中

TMS320LF240x DSP的C語言和匯編代碼快速入門

hex文件怎么能轉回去c語言
hex文件如何查看原c語言代碼
ModusToolbox 3.2在c代碼中包含c++代碼的正確步驟是什么?
AIGC系統中聊天小助手卡片小組件嵌套設計實踐

評論