作者:黃工 公眾號(hào):strongerHuang 做底層軟件開(kāi)發(fā)的,或多或少都會(huì)接觸一些匯編代碼,今天就來(lái)說(shuō)說(shuō)關(guān)于匯編在C中的定義和調(diào)用,以及舉例說(shuō)明嵌套匯編代碼。 一、嵌套匯編說(shuō)明有認(rèn)真研究,或者說(shuō)細(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?
如果你寫(xiě)過(guò)Keil C51,你還會(huì)發(fā)現(xiàn)有如下(通過(guò)預(yù)處理)嵌套匯編:
#pragma asm ; Assembler Code Here #pragma endasm 所以,你會(huì)發(fā)現(xiàn),不同的編譯器,匯編代碼還是有差異。當(dāng)然,這里主要是說(shuō)C中嵌套匯編與編譯器有關(guān)。 二、C中嵌套匯編代碼常見(jiàn)兩種定義:
1.在C函數(shù)中定義一段匯編代碼;
2.在C文件中定義一個(gè)匯編函數(shù);
(當(dāng)然,兩個(gè)意思差不多,都是在C中嵌套匯編)
上面說(shuō)了C中定義匯編代碼與編譯器有關(guān),換句話(huà)說(shuō):不同編譯器解析匯編代碼的方式不同。 這里還是拿core_cm3.c來(lái)舉例說(shuō)明,定義一個(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++ 語(yǔ)句合法時(shí)出現(xiàn)。 三、更多舉例下面舉一些常見(jià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ù)位 這是之前分享過(guò)的一篇文章《STM32復(fù)位來(lái)源》中的代碼:
__asm void NVIC_CoreReset_a(void){ LDR R0, =0xE000ED0C LDR R1, =0x05FA0001 STR R1, [R0]deadloop_Core B deadloop_Core} 舉了這些常見(jiàn)例子,沒(méi)有說(shuō)透,但相信只要認(rèn)真理解了都能明白。
-
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7631瀏覽量
141228 -
編程
+關(guān)注
關(guān)注
88文章
3687瀏覽量
95112 - 代碼
發(fā)布評(píng)論請(qǐng)先 登錄
技術(shù)干貨驛站 ▏深入理解C語(yǔ)言:嵌套循環(huán)與循環(huán)控制的底層原理

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

EE-132:使用VisualDSP將C代碼和數(shù)據(jù)模塊放入SHARC存儲(chǔ)器中

EE-128:C語(yǔ)言中的DSP:從C調(diào)用匯編類(lèi)成員函數(shù)

TMS320C6000匯編語(yǔ)言工具v7.4用戶(hù)指南

TMS320C6000匯編語(yǔ)言工具 v8.3.x

TMS320C28x匯編語(yǔ)言工具

TMS320LF240x DSP的C語(yǔ)言和匯編代碼快速入門(mén)

hex文件怎么能轉(zhuǎn)回去c語(yǔ)言
hex可以轉(zhuǎn)成源代碼么
hex文件如何查看原c語(yǔ)言代碼
RISC-V匯編語(yǔ)言

評(píng)論