Find end of program

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are there a way a program can be aware of it's size?
one (ugly) way would be to use FLASHEND and then read down to first non FF, and hope that last instruction aren't FF.
Are there a nice way?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most compilers will have a label for the last location.
Read the MAP file.

Quite honestly, it is as easy to follow your strategy than to read through a MAP file.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The linker defines symbols from the linker script. The end of text is _etext and the end of text+data is __data_load_end.

You can declare those symbols as extern and then use the & address of operator to get their values.

Something along the lines of:

#include 
#include 

char text[] = "Hello world";
extern int _etext;
extern int __data_load_end;
char buff[40];

int main(void) {
	sprintf(buff, "etext=%04X data_end=%04X",(uint16_t) &_etext, (uint16_t)&__data_load_end);
	while(1) {
		asm("nop");
	}
}

When I ran this in the simulator etext was 0658 and data_load_end was 067E.

At the same time the .map file said:

 *(.fini0)
                0x00000658                _etext = .

and

                0x0000067e                __data_load_end = (__data_load_start + SIZEOF (.data))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks it match the number printed in the build window.