Core Functions

Does anyone know where the core function defs are located within the arduino software? By "core" I mean the ones ported over from C/C++, like while(), for(), if(), etc. I don't want to change anything, I'm just interested in seeing the code.

Thanks! baum

Those are not functions, they are language constructs. Other common C functions like malloc are in avr-libc.

ok. one more question:

when i was looking around the arduino code, I found main.cpp:

#include <WProgram.h>

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

why for(;:wink: and not while(1)?

baum

The for( ; ; ) construct is the "most efficient". http://www.atmel.com/dyn/resources/prod_documents/doc1497.pdf , p19. Normally, I would say it doesn't matter. But in that loop it's probably the one place that DOES matter. The writer of that code has no idea what loop() will do, so he doesn't know if an extra couple instructions will be important.

The for( ; ; ) construct is the "most efficient". http://www.atmel.com/dyn/resources/prod_documents/doc1497.pdf ,

I really hate it hate it when superstitious nonsense (or at best "compiler and/or compiler version dependent observations) make their way into official documents. With the C compiler used by Arduino, a "while (1)" loop compiles to a single jump instruction at the end of the loop (and it's not possible to do better than that.) I'd be really shocked if all "eternal loops" weren't equivalently optimized in any compiler sold or built in the last 5 years. (the app note is from 2004...)

westfw, I wish there was a "Like" button on the forum here. I couldn't have said it better.

Maybe you could give him some Positive Karma 8)

Is that a joke, or is there really a function in this forum software to do that?

Hah, indeed. That did always strike me as weird, but I never tried it until now. Sure enough, rjmp to the top.

Karma was an inside joke - when this "new" forum replaced the "old" forum, there was a feature called karma where you showed pleasure (?)/displeasure with a poster with no explanation but giving positive or negative karma - it finally got to the point of being silly and was taken down.

Back to the OP:
If you want to see what your code compiles to, and hence see how the different constructs work, run “objdump” on your sketch’s “.elf” file.
To find where your “.elf” file is, hold down the shift key when you hit the compile button.

It can be quite informative - I don’t know AVR assembler that well, and was surprised (and amused) to see that the “++i” in a “for (byte i = 0; i < 8; ++i)” loop compiled to “subi r24, 0xff”; in other words, “subtract immediate from r24, minus 1” !

was surprised (and amused) ... compiled to "subi r24, 0xff"

No choice. The processor lacks an "add immediate" instruction! :astonished: Kind of sad that our favourite little processor does not know how to add. =(

RISC philosophy, you know. Why should you waste two opcodes (and internal paths) when you can always get the same results with a single opcode (and MAYBE some smarts in the assembler/compiler.)

I'm not sure it is strictly a RISC thing; the distinctly non-RISC Z80 had no "clear accumulator" instruction (use "XOR A,A"), and the even earlier PDP-8 had no "load immediate", just TAD (two's complement add) and CLR.

Ok! thanks everybody