如何使用 C 的内联汇编器访问 ARM Cortex M3 的 r11 寄存器 [英] How to access r11 register of ARM Cortex M3 with inline assembler of C

查看:32
本文介绍了如何使用 C 的内联汇编器访问 ARM Cortex M3 的 r11 寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了下面的代码,但未能从 r11 中读取正确的值,以下参考 http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472f/Cihfhjhg.html

I tried below code, but failed to read the correct value from r11 following below reference of http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472f/Cihfhjhg.html

volatile int top_fp;
__asm
{
    mov top_fp, r11
}

r11 的值为 0x20009DCC top_fp 的值为 0x00000004

r11's value is 0x20009DCC top_fp's value is 0x00000004

[更新] 解决方案,我们必须使用嵌入式汇编器:

__asm int getRegisterR11()
{
    mov r0,r11
    BX LR //return, must not omitted!!!
}

推荐答案

您发布的链接仅指 lr(R13)、sp(R14)、pc(R5) 用于旧版 ARM ADS 代码的旧代码支持并且不适用于通用寄存器.

The link you posted refers only to lr(R13), sp(R14), pc(R5) for legacy code support of code for old versions of ARM ADS and does not apply to general-purpose registers.

在 ARM 的编译器中(也在 Keil 的 MDK-ARM 中使用):

In ARM's compiler (also used in Keil's MDK-ARM):

内联汇编器不提供对物理寄存器的直接访问ARM 处理器.如果将 ARM 寄存器名称用作内联汇编指令,它成为对变量的引用同名,而不是物理 ARM 寄存器.

The inline assembler provides no direct access to the physical registers of an ARM processor. If an ARM register name is used as an operand in an inline assembler instruction it becomes a reference to a variable of the same name, and not the physical ARM register.

(参考:内联汇编器并注册访问)

ARM 编译器中的内联汇编器会像内联它的 C 或 C++ 代码一样进行优化,因此编译器在任何情况下都可能生成与您编写的代码不同的代码.如果您希望完全按照您编写的方式生成汇编代码,您必须使用 嵌入式汇编器而不是内联汇编器

Inline assembler in ARM's compiler is subject to optimisation like the C or C++ code it is in-lined within, as such the compiler may generate code that differs from that you have written in any case. If you want assembler code to be generated exactly as you have written you must use embedded assembler rather the inline assembler

这篇关于如何使用 C 的内联汇编器访问 ARM Cortex M3 的 r11 寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆