@Graynomad
Yes, the code is very naive at best and indeed when the magic number is used as either an offset in the code or as operand the code will fail to deliver.
I had a look at the AVR instruction set - http://www.atmel.com/images/doc0856.pdf - to see how complex it would be to write a "disassembler" that would be smart enough to catch the operand scenario and that seems doable. 9508 as operand can only occur with the 32 bit instructions and there are less than 10 of those. E.g. CALL is one of them (CALL calls another function). Recognizing these will catch the operand scenario.
A far more difficult scenario to tackle is compiled functions with multiple RET in it. It is easy to imagine that the assembler code for a function like below (but more elaborate) has three places it returns. Did not encounter this yet.
uint32_t example(uint32_t n)
{
if (n== 0) return 0;
// ...lines of code
if (n%2) return 3*n + 1;
return n/2;
}
@kowalski, westfw
avr-nm is a great tool, and far superior to what I have written (no doubt), but it cannot be called on the UNO itself.
The reason why that is important for me is as follows:
I am often optimizing code both for fun and more serious needs. When I try different approaches I want to compare the effects on speed and space as quickly as possible. So if I can do it runtime and generate performance and footprint in the output of the sketch it would be optimal. Then I do not need to swap between different tools, parsing and merging their output.
With the getBytesUntilReturn function I can (within its limits) do :
start = micros();
func(); // or a loop
duration =micros() - start;
size = getBytesUntilReturn((char*) func);
Serial.print("func\t");
Serial.print(duration);
Serial.print("\t");
Serial.print(size);
Serial.println();
This done for multiple functions will generate output like :
name time size
f1 960 234
f2 1024 324
f3 876 236
f4 400 1200
f5 404 896
in which I can see the effects of a new algorithm/variation directly and copy paste the numbers into a spreadsheet to graph them.
And yes, knowing the limits of getBytesUntilReturn() I must not accept the numbers blindly. (you never should
And yes, it should have a better name too, but at least now it just does what it says it does.
Thanks for the feedback,