單片機(jī)開發(fā)或多或少都會(huì)接觸一些匯編代碼,今天就來說說關(guān)于匯編在C中的定義和調(diào)用,以及舉例說明嵌套匯編代碼。
概述
有認(rèn)真研究,或者說細(xì)心一點(diǎn)的讀者應(yīng)該都知道:C中定義匯編代碼與編譯器有關(guān)。
比如:你在core_cm4.h文件會(huì)看到如下的代碼:
#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,你還會(huì)發(fā)現(xiàn)有如下(通過預(yù)處理)嵌套匯編:
#pragma asm ; Assembler Code Here #pragma endasm
所以,你會(huì)發(fā)現(xiàn),不同的編譯器,匯編代碼還是有差異。當(dāng)然,這里主要是說C中嵌套匯編與編譯器有關(guān)。
C中嵌套匯編代碼
常見兩種定義:
1.在C函數(shù)中定義一段匯編代碼;
2.在C文件中定義一個(gè)匯編函數(shù);
(當(dāng)然,兩個(gè)意思差不多,都是在C中嵌套匯編)
上面說了C中定義匯編代碼與編譯器有關(guān),換句話說:不同編譯器解析匯編代碼的方式不同。
這里還是拿core_cm3.c來舉例說明,定義一個(gè)__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++ 語句合法時(shí)出現(xiàn)。
看到這里,推薦閱讀我分享的一篇文章:單片機(jī)用匯編和C語言點(diǎn)燈程序的區(qū)別
舉例
下面舉一些常見例子。
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)真理解了都能明白。
來源:微信公眾號(hào) | 嵌入式專欄
作者 | strongerHuang
免責(zé)聲明:本文為轉(zhuǎn)載文章,轉(zhuǎn)載此文目的在于傳遞更多信息,版權(quán)歸原作者所有。本文所用視頻、圖片、文字如涉及作品版權(quán)問題,請(qǐng)聯(lián)系小編進(jìn)行處理
審核編輯 黃宇
-
單片機(jī)
+關(guān)注
關(guān)注
6043文章
44622瀏覽量
638533 -
匯編
+關(guān)注
關(guān)注
2文章
214瀏覽量
26005 -
C代碼
+關(guān)注
關(guān)注
1文章
89瀏覽量
14356 -
嵌套
+關(guān)注
關(guān)注
0文章
15瀏覽量
7946
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論