内嵌汇编
(仅做了解)
__asm volatile (
汇编语句模板:
输出部分:
输入部分:
破坏描述部分
); 内嵌汇编允许我们在C或C++代码中插入汇编代码,并且不需要我们安装额外的汇编语言编译器
用法
实现c/c++无法实现的操作,比如FreeRTOS的上下文切换
void vPortSVCHandler( void )
{
__asm volatile (
" ldr r3, pxCurrentTCBConst2 \n"/* Restore the context. */
" ldr r1, [r3] \n"/* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
" ldr r0, [r1] \n"/* The first item in pxCurrentTCB is the task top of stack. */
" ldmia r0!, {r4-r11} \n"/* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
" msr psp, r0 \n"/* Restore the task stack pointer. */
" isb \n"
" mov r0, #0 \n"
" msr basepri, r0 \n"
" orr r14, #0xd \n"
" bx r14 \n"
" \n"
" .align 4 \n"
"pxCurrentTCBConst2: .word pxCurrentTCB \n"
);
}
或者运行发生致命错误,C语言的运行环境可能已经不存在,需要恢复运行现场
void HardFault_Handler(void) {
__asm volatile(
" tst lr, #4 \n"
" ite eq \n"
" mrseq r0, msp \n"
" mrsne r0, psp \n"
" ldr r1, [r0, #24] \n"
" ldr r2, handler2_address_const \n"
" bx r2 \n"
" handler2_address_const: .word GetRegistersFromStack \n");
}