Common command functions in startup.s files in STM32 development

Due to the popularity of C and the development of compilers, more and more software engineers seldom have access to assembly language in programming. In the development of ARM, we will inevitably encounter the compilation of startup file. In KEIL environment, startup. s file is generally used as startup code.Many engineers have a headache when they see this file. Here I will briefly introduce some common assembly instructions and pseudo-instructions. I hope it will be helpful to you. Next time I see the. s file, I will not find it so difficult to accept.

  Remind you, in assembly code is not case-sensitive, which is not the same as C language, so the instructions below are often lowercase. But my personal opinion is that the compilation is preferably all uppercase, so that it is easy to read.

  There are many basic knowledge in this note, which is suitable for some junior engineers. Many instructions and syntax are assembled in general, if you patiently read all, I believe it will be helpful to your development work.

 

 1.   REQUIRE8And PRESERVE8

When defining stacks, we often see REQUIRE8 and RESERVE8 pseudoinstructions, which tell the compiler to ensure that 8 bytes are aligned. Because the ARM compiler defaults to 4-byte alignment, LDRD and ST are often used in stack operationsRD double word transmission instructions, so the memory here requires 8 byte alignment.

In practice, REQUIRE8 and RESERVE8 don’t do alignment directly, they just change the compiler’s compiler properties, and the real alignment is done by ALIGN.

 

 2.   AREA

AREA Pseudo-instructions are used to define a memory area, which is generally called a segment. The syntax is as follows:

AREA Section name property 1, attribute 2,…

Note that if the segment name begins with a number or a special character, the segment name needs to be enclosed in ||. For example, |3_Code| or |.code|

The attribute section is used to represent the related properties of the memory segment, and the commas are used to separate the multiple attributes. The common attributes are as follows:

– NOINIT : Non initialization

– CODE   :For defining code segments, the default attribute of READONLY is defined.

– DATA   : For defining data segments, the default attribute of READWRITE is defined.

– READWRITE/READONLY : Used to indicate whether the paragraph is read-only or readable.

– ALIGN  : The alignment of memory access is generally defined as ALIGN = N, where N represents the number of words aligned, for example:

ALIGN = 2Indicates that the alignment is the 2 power of 2, that is, 4 byte alignment.

ALIGN = 3 Mark alignment is the 3 power of 2, that is, 8 byte alignment.

– COMMON : This attribute is used to define a general storage area, without any code and data. The COMMON area defined in different source files share a storage area.

 

 3.   THUMB(CODE16)、ARM(CODE32)

The two pseudo directives notify the compiler, followed by 16 bit Thumb instructions or 32 bit ARM instructions. Because the ARM7 kernel supports both instruction modes and allows switching between them in the same code, the ARM and Thumb instructions are mixed togetherIn programming code, use these two pseudo instructions to switch.

In the Cortex-M series kernel, there is no instruction mode for switching ARM because of the Thumb2 instruction set.

 

 4.   EXPORT,IMPORT,EXTERN

These two pseudo directives are used to declare or reference a global variable (function) label. In mixed programming code of assembly and C, variable (function) labels can be passed through these two pseudo instructions. EXPORT can also be replaced by GLOBAL.

It should be noted that although assembly language is case-insensitive, variables (functions) are labeled case-insensitive.

IMPORTThe usage is similar to that of EXTERN. The main difference between them is that if the external file does not define this label, whether it is added to the symbolic table of the source file, for the general programmer does not care about this difference.

If there is a parameter [WEAK] after EXPORT and IMPORT, it indicates that the label declared in this directive has the lowest priority if it encounters a label of the same name elsewhere.

 

 5.   SPACE

SPACEIt is used to retain a specified size storage space. This statement is relatively simple, for example:

SPACE 100

Represents a space of 100 bytes size at the address at the beginning of this instruction.

 

 6.   SPACE

SPACEIt is used to retain a specified size storage space. This statement is relatively simple, for example:

SPACE 100

Represents a space of 100 bytes in the address at the beginning of this instruction.

 

 7.   ENTRY

ENTRYThe entry point for specifying assemblers is a bit like the main function in C language. A complete assembly application requires at least one ENTRY.

 

 8.   INCBIN

Sometimes we run into the problem of having a picture or data compiled into a BIN file, so how do I embed it into my program? The INCBIN pseudo-instruction gives us the benefit of including a compiled BIN file in the current sourceThe files contained are not altered.

For example:

We need to add a compiled string to the source file.

    EXPORT string_1 ; Declare the label of this string to facilitate other program calls.

String_1

INCBIN string_1.bin   ; The bin file that loads the character.

In the *.c file, we can call the String_1[] array directly.

 

 9.   GET, INCLUDE

GETSimilar to INCLUDE, they are used to include a source file into the current source file and assemble the included source file at the current location. This is similar to the “#include” in C language.

 

 10.  END

    ENDThe pseudo instruction is used to notify the compiler to the end of the source program. He corresponds to ENTRY, and an application should have only one END.

The above is from Triton.Zhang.

Leave a Reply

Your email address will not be published. Required fields are marked *