作者:黃工 公眾號:strongerHuang 做底層軟件開發(fā)的,或多或少都會接觸一些匯編代碼,今天就來說說關(guān)于匯編在C中的定義和調(diào)用,以及舉例說明嵌套匯編代碼。 一、嵌套匯編說明有認(rèn)真研究,或者說細(xì)心一點的讀者應(yīng)該都知道:C中定義匯編代碼與編譯器有關(guān)。 比如:你在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,你還會發(fā)現(xiàn)有如下(通過預(yù)處理)嵌套匯編:
#pragma asm ; Assembler Code Here #pragma endasm 所以,你會發(fā)現(xiàn),不同的編譯器,匯編代碼還是有差異。當(dāng)然,這里主要是說C中嵌套匯編與編譯器有關(guān)。 二、C中嵌套匯編代碼常見兩種定義:
1.在C函數(shù)中定義一段匯編代碼;
2.在C文件中定義一個匯編函數(shù);
(當(dāng)然,兩個意思差不多,都是在C中嵌套匯編)
上面說了C中定義匯編代碼與編譯器有關(guān),換句話說:不同編譯器解析匯編代碼的方式不同。 這里還是拿core_cm3.c來舉例說明,定義一個__get_PSP函數(shù)。 在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)關(guān)鍵字用于調(diào)用內(nèi)聯(lián)匯編程序,并且可在 C 或 C++ 語句合法時出現(xiàn)。 三、更多舉例下面舉一些常見例子。 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.內(nèi)核復(fù)位 這是之前分享過的一篇文章《STM32復(fù)位來源》中的代碼:
__asm void NVIC_CoreReset_a(void){ LDR R0, =0xE000ED0C LDR R1, =0x05FA0001 STR R1, [R0]deadloop_Core B deadloop_Core} 舉了這些常見例子,沒有說透,但相信只要認(rèn)真理解了都能明白。
-
C語言
+關(guān)注
關(guān)注
180文章
7605瀏覽量
137020 -
編程
+關(guān)注
關(guān)注
88文章
3620瀏覽量
93785 -
代碼
+關(guān)注
關(guān)注
30文章
4793瀏覽量
68700
發(fā)布評論請先 登錄
相關(guān)推薦
TMS320C6000匯編語言工具v7.4用戶指南
![TMS320<b class='flag-5'>C</b>6000<b class='flag-5'>匯編</b>語言工具v7.4用戶指南](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
TMS320C6000匯編語言工具 v8.3.x
![TMS320<b class='flag-5'>C</b>6000<b class='flag-5'>匯編</b>語言工具 v8.3.x](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
TMS320LF240x DSP的C語言和匯編代碼快速入門
![TMS320LF240x DSP的<b class='flag-5'>C</b>語言和<b class='flag-5'>匯編</b><b class='flag-5'>代碼</b>快速入門](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
hex文件怎么能轉(zhuǎn)回去c語言
hex可以轉(zhuǎn)成源代碼么
hex文件如何查看原c語言代碼
RISC-V匯編語言
ModusToolbox 3.2在c代碼中包含c++代碼的正確步驟是什么?
AIGC系統(tǒng)中聊天小助手卡片小組件嵌套設(shè)計實踐
![AIGC系統(tǒng)<b class='flag-5'>中</b>聊天小助手卡片小組件<b class='flag-5'>嵌套</b>設(shè)計實踐](https://file1.elecfans.com/web2/M00/ED/CA/wKgaomZpE9CARJvuAACiKX5_taI674.jpg)
評論