Arduino Due vs. embedded C - ARM reversing
arduino-cli
: Command Line Interface for Arduino- ARM cross compiler:
sudo apt install gcc-arm-none-eabi
Sample program
We want to create the most simple program which goal is to light on the built-in LED, located at port PB27 on the Arduino Due.
Arduino
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
}
Embedded C
int main()
{
PIOB->PIO_PER = 1<<27; /* Enable port PB27 */
PIOB->PIO_OER = 1<<27; /* Configure PB27 as output */
PIOB->PIO_ODSR = 0xFFFFFFFF; /* Write 1 in all PORTB ports */
return 0;
}
Codes and compilation
$ ls -lR
./led-arduino:
total 4
-rw-r--r-- 1 pascal pascal 104 mai 1 12:58 led-arduino.ino
./led-embedded:
total 4
-rw-r--r-- 1 pascal pascal 81 mai 1 13:00 led-embedded.ino
$ arduino-cli compile --fqbn arduino:sam:arduino_due_x --output-dir led-arduino led-arduino
$ arduino-cli compile --fqbn arduino:sam:arduino_due_x --output-dir led-embedded led-embedded
Binary comparison
Arduino | Embedded C | |
---|---|---|
Storage use | 10660 bytes | 2544 bytes |
The embedded C code is 5 times smaller than the Arduino one which is a bit “weird” as both codes do the same thing! Let’s find why by reversing binaries and analyzing assembly codes.