I am interested in finding out the memory usage of my code running on an ATMega328P (16 MHz) using the avr-size utility bundled with WinAVR 20100110. Using the Makefile included in the distribution I obtained the following memory usage for the code below:
Here is the date sheet of atmega328p
#include <avr/io.h>
#include <stdint.h>
int main(void)
{
while(1)
{ }
return 0;
}
The console output is:
make all
———— begin ————
avr-gcc (WinAVR 20100110) 4.3.3
Copyright © 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Compiling C: src/main.c
avr-gcc -c -mmcu=atmega328p -I. -gdwarf-2 -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=./src/main.lst -std=gnu99 -MMD -MP -MF .dep/main.o.d src/main.c -o src/main.o
Linking: src/main.elf
avr-gcc -mmcu=atmega328p -I. -gdwarf-2 -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=src/main.o -std=gnu99 -MMD -MP -MF .dep/main.elf.d src/main.o —output src/main.elf -Wl,-Map=./src/main.map,—cref -lm
Creating load file for Flash: src/main.hex
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature src/main.elf src/main.hex
Creating load file for EEPROM: src/main.eep
avr-objcopy -j .eeprom —set-section-flags=.eeprom=“alloc,load” \
—change-section-lma .eeprom=0 —no-change-warnings -O ihex src/main.elf src/main.eep || exit 0
Creating Extended Listing: src/main.lss
avr-objdump -h -S -z src/main.elf > src/main.lss
Creating Symbol Table: src/main.sym
avr-nm -n src/main.elf > src/main.sym
Size after:
AVR Memory Usage
————————
Device: atmega328p
Program: 134 bytes (0.4% Full)
(.text + .data + .bootloader)
Data: 0 bytes (0.0% Full)
(.data + .bss + .noinit)
———— end ————
When I run the same code with the while() construct removed the program size is reported to be 138 bytes. Same happens if I comment out both while() and the return statement. How can the program size increase when I remove a loop construct? The Makefile uses the -S optimizer for gcc. Does it have something to do with it?