.s文件在C项目中的作用是什么? [英] What is the role of .s files in a C project?

查看:474
本文介绍了.s文件在C项目中的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ARM Cortex M3芯片(STM32F2),ST提供了标准外围设备库。它具有一些有用的.c和.h文件。它也有.s文件。

I am working with an ARM Cortex M3 chip (STM32F2) and ST provides a "standard peripheral library". It has some useful .c and .h files. It also has .s files.

在C项目的上下文中,这些.s文件的目的是什么?如何获得我的编译器/链接器/?

What is the purpose of these .s files in the context of a C project? How do I get my compiler/linker/? to take them into account?

推荐答案

.s扩展名是GNU和许多其他工具链用于汇编程序文件的约定。

The .s extension is the convention used by GNU and many other tool-chains for assembler files.

最后,我看到STM32标准外设库本身不包含汇编程序文件,但是CMSIS库包含用于STM32各个部件的启动代码,例如startup_stm32f2xx.s是所有STM32F2xx系列设备的启动代码。不同的工具链有不同的实现。您需要构建和链接与您的特定零件和工具链关联的文件。如果您使用的是一个示例项目,该项目可以生成并运行,或者是一个IDE为您创建零件特定的项目,那么这可能已经完成了-如果您有运行它的代码,当然可以。

Last I looked the STM32 Standard Peripheral Library itself contains no assembler files, however the CMSIS library contains start-up code for various STM32 parts, for example startup_stm32f2xx.s is start-up code for all STM32F2xx series devices. There are different implementations for different tool-chains; you need to build and link the file associated with your specific part and tool-chain. If you are using an example project that builds and runs or an IDE that creates part-specific projects for you, this will probably already have been done - if you have code that runs it certainly has.

如何构建和链接代码将取决于您所使用的工具链。大多数基于IDE的工具会自动识别该扩展名,并调用汇编程序以生成一个目标文件,该目标文件将像其他文件一样被链接。确切的内容在工具链版本之间略有不同,但是主要是创建C运行时环境(堆栈和堆),初始化处理器,定义初始中断/异常向量表,初始化静态数据并跳转到main()。

How you build and link the code will depend on what tool-chain you are using. Most IDE based tools will automatically recognise the extension and invoke the assembler to generate an object file that will be linked like any other. The exact content differs slightly between tool-chain versions, but primarily creates the C runtime environment (stack and heap), initialises the processor, defines an initial interrupt/exception vector table, initialises static data and jumps to main().

例如,Keil / ARM RealView版本的文件核心如下:

The core of the file for the Keil/ARM RealView version for example looks like this:

; Reset handler
Reset_Handler    PROC
                 EXPORT  Reset_Handler             [WEAK]
        IMPORT  SystemInit
        IMPORT  __main
                 LDR     R0, =SystemInit
                 BLX     R0
                 LDR     R0, =__main
                 BX      R0
                 ENDP

Reset_Handler 是在处理器重置后将地址设置为程序计数器(PC)寄存器。

Reset_Handler is the address Program Counter (PC) register will be set to after a processor reset.

SystemInit 是一个外部C代码函数,它完成大部分初始化工作-这可能需要针对您的硬件进行自定义。 Cortex-M与众不同之处在于,它可以在复位后立即开始运行C代码,因为向量表同时包含复位地址和初始堆栈指针地址,该地址在复位时会自动加载到SP寄存器中。因此,您不需要太多的汇编程序知识即可开始运行。

SystemInit is an external C code function that does the bulk of the initialisation - this may need customisation for your hardware. Cortex-M is unusual in that it can start running C code immediately after reset because the vector table includes both the reset address and the initial stack pointer address, which is automatically loaded to the SP register on reset. As a result you do not need much assembler knowledge to get one running.

__ main()是提供的编译器您的C代码的入口点。它不是您编写的main()函数,而是在调用`main()'函数之前对标准库,静态数据和堆执行初始化。

__main() is the compiler supplied entry point for your C code. It is not the main() function you write, but performs initialisation for the standard library, static data, the heap before calling your `main()' function.

GCC版本涉及更多,因为它完成了Keil / ARM RealView版本中 __ main()所做的许多工作,但实际上它执行的功能相同。

The GCC version is somewhat more involved since it does much of the work done by __main() in the Keil/ARM RealView version, but essentially it performs the same function.

请注意,在CMSIS中, SystemInit()是在system_stm32f2xx.c中定义的,可能需要针对您的电路板进行自定义(正确的晶体频率,PLL设置,外部SRAM配置等)。因为这是C代码,并且经过了很好的注释,所以您可能会更满意。

Note that in the CMSIS SystemInit() is defined in system_stm32f2xx.c, and may need customisation for your board (correct crystal frequency, PLL setup, external SRAM configuration etc.). Because this is C code, and well commented, you will probably be more comfortable with it.

这篇关于.s文件在C项目中的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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