This is fascinating. The C compiler is ADDING A LOOP COUNTER, even though there was none in the source code, and none is needed! And a 16-bit loop counter at that! I wonder WTF it's thinking? (Also, let this be a warning against "optimizing" your C code without carefully inspecting the results!)
(It does the same thing if you convert the for loop to a do-while and try to retain the (shifted result == 0) conditional...)
void spiWrite(uint8_t data)
{
uint8_t bit;
for(bit = 0x80; bit; bit >>= 1) {
SPIPORT &= ~clkpinmask;
if(data & bit) SPIPORT |= mosipinmask;
else SPIPORT &= ~mosipinmask;
SPIPORT |= clkpinmask;
}
}
00000000 <spiWrite>:
0: 28 e0 ldi r18, 0x08 ; 8 Created loop counter in r19:r18
2: 30 e0 ldi r19, 0x00 ; 0
4: 90 e8 ldi r25, 0x80 ; 128
6: 2d 98 cbi 0x05, 5 ; 5
8: 49 2f mov r20, r25
a: 48 23 and r20, r24
c: 11 f0 breq .+4 ; 0x12
e: 2c 9a sbi 0x05, 4 ; 5
10: 01 c0 rjmp .+2 ; 0x14
12: 2c 98 cbi 0x05, 4 ; 5
14: 2d 9a sbi 0x05, 5 ; 5
16: 96 95 lsr r25
18: 21 50 subi r18, 0x01 ; 1 decrement the created loop counter (16bits!)
1a: 31 09 sbc r19, r1
1c: 21 15 cp r18, r1 ; compare loop counter with zero.
1e: 31 05 cpc r19, r1
20: 91 f7 brne .-28 ; 0x6
22: 08 95 ret