William Chang

How startup_stm32f107xc.s works

Blog Post created by William Chang on May 13, 2018

1. 

2.

   PRESERVE8:  the PRESERVE8 directives specify that the current file requires 8-byte alignment of the stack.

      8 byte stack alignment is a requirement of ARM Architecture Procedure Call Standard[AAPCS]. This specifies that    functions must maintain an 8 byte aligned stack address.

   THUMB: the THUMB directive instructs the assembler to interpret subsequent instructions as Thumb instructions.

 

3.

build a RESET section. RESET section is an indivisible chunks of read only data that are manipulated by the linker.

 

4.

build a .text section. .text section is an indivisible chunks of code (execution only) that are manipulated by the linker.

including all Handles Function.

ALIGN: ensures following instruction are word aligned.

5.

 

Let make a real one in keil.

For simplest, I make a real simple example.

 

That's it. This program only consume 12 bytes in flash!

Let's go into debug mode.

When MCU power up. It first load the MSP at address 0x800 0000. The load the value of address 0x800 0004 to the PC. This is done automatically.

Then

PC, the program counter, I would prefer to say program pointer.

PC have the value 0x800 0009. What would CPU do.

CPU will load the value store in the address 0x800 0008. the value is 0x002AF04F. Then CPU decode the value. He found it means " MOV R0, 42". Then he execute instruction.

After execution. The R0 is 42! Then the PC automatically increase and point to next address in the flash.

What would CPU do?

Load the PC value -> Decode -> execute -> increase PC 

over and over again.

 

 

 

Anyway, the startup file is also a program, but written in assembly.

It isn't difficult to read if you understand the assembly language.

Also, you need to know:

When MCU power up. It automatically load the 32bit value in the first address of the memory(0x0800 0000), then load the next 32bit value of the address (0x0800 0004) to the PC. This address is also known as Reset_Handler.

 

If you are wondering how the MCU go to main() function.

Please refer to main and __main 

 

 

Outcomes