The build process for an embedded system that contains source files written in C involves many steps, starting from preprocessing to generating the binaries.
One important step in the build process is the compilation stage, which is carried out by the compiler. In this step, the source file which was written in C gets converted into the architecture-specific assembly instructions.
The sequence of this blog is as follows:
- Writing an assembly code to perform bubble sorting on 10 elements stored in SRAM.
- Writing a C code to perform the same operation and comparing the Assembly code generated by the ARM compiler to the assembly code written by me.
Example:
Example:
Example:
Example:
Compare Instruction doesn't change the operands, the result affects the flag. The carry (C) flag is set when an operation results in a carry, or when a subtraction results in no borrow, so cc will be set if R1 > R2 in the above example.
B InstructionTo loop conditional branch instructions are used
Some conditional branch statements
Note: Assembly Code was written by hand first, before writing the C code.
Some information before writing the Assembly code:
- Microcontroller Used: STM32F429ZI
- Address of SRAM1: 0x2000 0000
- The elements to be sorted are of 1 byte each stored from the start of the SRAM Address, there are 10 elements in total
- The numbers can be loaded directly into memory through memory browser interface provided by the IDE
- Bubble sort is used for sorting, with the smallest element sorted first in ascending order
- A pointer (register) is used to store the start of the SRAM Address (*ptr)
- Initialize the outer loop index element to zero ( I = 0 )
- Initialize the inner loop index element to outer index + 1 ( j = I + 1 )
- Load the memory element corresponding to the address of the sum of SRAM Addr & outer loop index element *(ptr + i) into a register
- Load the memory element corresponding to the address of the sum of SRAM Addr & inner loop index element *(ptr + j) into a register
- Compare the two registers and swap (memory elements) if the data in the lower memory address is greater than the data in the higher memory address
- Update the inner and outer loop index elements and continue the loop if the condition satisfies
Initial Data access through memory browser:
Outer Loop Iterations:
The last iteration output shows the final sorted array of elements.
C CodeThe algorithm used is the same.
Now let's look at the assembly code generated by the compiler.
Assembly Code generated by CompilerADDRESS OPCODE MNEMONIC
The ARM compiler is designed smart enough to generate the assembly code in an optimized way, as you might realize after seeing the code written by hand and the one generated by the compiler is almost the same.
This really surprised me for the first time I compiled the C code & looking at the assembly after writing the assembly code by hand, as I expected some difference (more instructions when compiled from C code).
The conversion of high-level C code to low-level assembly is one of the most crucial steps in embedded systems' build process.
I hope this blog was an informative one, I shall list out all the references used to write this blog.
References
Comments