如何在代码里面读取CPU通用寄存器的值到变量

Technical Note

Architectures:

All

Component:

general

Updated:

2023/7/31 9:19

问题

在调试时,CPU通用寄存器可以在寄存器窗口里面查看。由于CPU通用寄存器不是地址映射的,在代码里面不能像读取地址映射的寄存器那样通过指针读取,需要使用对应的汇编语言来读取。

解决方法

可以通过内联汇编指令(inline assembler instructions)读取CPU通用寄存器到对应的变量:

static volatile uint32_t StackPointer;

__asm("mov %0, sp\n" : "=r"(StackPointer));

参考文献:

  1. IAR C/C++ Development Guide (Inline assembler)

 

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.