c - 混合 C 和 ASM,皮质 M4

标签 c assembly cortex-m

我正在尝试使用 cortex M4 处理器(我买了一 block Atmel SAM4C 板)混合 C 代码和 asm。

我正在尝试一个不起作用的简单代码(但它确实可以编译)。

我唯一的调试选项是使用 UART(我现在没有任何调试器)。

基本上,我只想写一个什么都不做的asm函数(直接返回C代码)。

这是我的 C 代码:

#include <asf.h>
extern uint32_t asmfunc(void);

int main (void){

    //some needed initialisation to use the board
    sysclk_init();
    board_init(); 

    uint32_t uart_s = 0, uart_r;

    //get_data and send_data get/send the 4 bytes of a 32 bits word
    //they work, I don't show the code here because we don't care

    get_data(&uart_r); //wait to receive data via uart
    send_data(uart_s); //send the value in uart_s

    asmfunc(); 

    send_data(uart_s);
}

汇编代码:

.global asmfunc

asmfunc:
MOV pc, lr

向 UART 发送一些数据后,我应该收到 2 倍的值“0”。但是,我只收到一次。我可以假设我的 asm 函数有问题,但我找不到问题。

我试图找到一些文档,我认为我做对了但是......

最佳答案

asmfunc:
MOV pc, lr

这是 ARM 程序集,不是 Thumb 程序集。您没有在 Thumb 模式下组装它,这导致生成的代码对于 Cortex-M CPU 无效。

将汇编程序设置为 Thumb 模式并使用适当的操作:

.thumb
.syntax unified

.global asmfunc
asmfunc:
    bx lr

关于c - 混合 C 和 ASM,皮质 M4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28504089/

相关文章:

c - 如何在 C 中表示内存中的 FLOAT 数

assembly - nasm:如何填充字符串的开头,使其在对齐边界处正确结束

c - 使用 Xcode 选项 Generate Output>Assembly 查看 C 文件的汇编代码

assembly - AVR 程序必须始终以相对跳转指令开头吗?

performance - 机器代码的精确副本运行速度比原始函数慢 50%

c++ - C 中是否有相当于 %* 或 Environment.CommandLine 的东西?

c - 如何删除C中的文本?

rust - 当值将在启动时写入一次然后仅读取时,嵌入式 Rust 中的 Mutex 是否有轻量级替代方案?

c - STM32 相同的 while 循环代码但编译为不同的汇编代码

c - 循环和模数