I was just looking through the disassembly of some code I am working on and I have a bit of a conceptual question as to why the compiler generates the code the way it does. The first two lines of my code are pretty standard, I set DDRA because it's all outputs and then I clear port A to make sure they all start off low. This generates the following instructions:
DDRA |= 0xff; 0000002B IN R24,0x1A In from I/O location 0000002C SER R24 Set Register 0000002D OUT 0x1A,R24 Out to I/O location PORTA &= 0x00; 0000002E IN R24,0x1B In from I/O location 0000002F OUT 0x1B,R1 Out to I/O location
Looking at this I see a standard read, modify, write operation for the Data direction register. For the PORTA register though I'm confused as to the purpose of the read instruction at 0x2E. It's reading in the contents of PORTA register but then it is writing 0 out to it directly. Wouldn't the same results be achieved by eliminating the instruction at 0x2E? For grins I recompiled the code using the size optimization and wound up with the same (seemingly) wasted instruction. This has piqued my curiosity and I was just wondering if anyone could shed some light on this behavior. Thanks.