sterretje:
And that is exactly what I like to know; what does that code look like (in relation to other parts of the code). Because it basically means that the buttonState is not initialised in loop() but beforehand (contrary to earlier replies saying "the first time loop() runs"). And it must be initialised after the pinMode is set.
Bummer. Looks like a hidden flag is used rather than crt0...
void loop()
{
Serial.println("Entering loop");
14a: 6f e0 ldi r22, 0x0F ; 15
14c: 71 e0 ldi r23, 0x01 ; 1
14e: 80 e5 ldi r24, 0x50 ; 80
150: 91 e0 ldi r25, 0x01 ; 1
152: 0e 94 5e 04 call 0x8bc ; 0x8bc <_ZN5Print7printlnEPKc>
static byte buttonState = digitalRead(2);
156: 80 91 3f 01 lds r24, 0x013F
15a: 81 11 cpse r24, r1
15c: 08 c0 rjmp .+16 ; 0x16e <loop+0x24>
15e: 82 e0 ldi r24, 0x02 ; 2
160: 0e 94 09 02 call 0x412 ; 0x412 <digitalRead>
164: 80 93 3e 01 sts 0x013E, r24
168: 81 e0 ldi r24, 0x01 ; 1
16a: 80 93 3f 01 sts 0x013F, r24
Serial.println(buttonState);
16e: 4a e0 ldi r20, 0x0A ; 10
170: 50 e0 ldi r21, 0x00 ; 0
172: 60 91 3e 01 lds r22, 0x013E
176: 80 e5 ldi r24, 0x50 ; 80
178: 91 e0 ldi r25, 0x01 ; 1
17a: 0e 94 cc 04 call 0x998 ; 0x998 <_ZN5Print7printlnEhi>
delay(500);
17e: 64 ef ldi r22, 0xF4 ; 244
180: 71 e0 ldi r23, 0x01 ; 1
182: 80 e0 ldi r24, 0x00 ; 0
184: 90 e0 ldi r25, 0x00 ; 0
186: 0c 94 32 01 jmp 0x264 ; 0x264 <delay>
Register r24 = whatever is stored at address 0x013F. 0x013F was initialized to zero by crt0 so the first time here r24 = 0.
156: 80 91 3f 01 lds r24, 0x013F
If r24 is zero skip the next instruction (the rjmp).
15a: 81 11 cpse r24, r1
Skip the next five instructions.
15c: 08 c0 rjmp .+16 ; 0x16e <loop+0x24>
Call digitalRead then store the result at address 0x013E (buttonState).
15e: 82 e0 ldi r24, 0x02 ; 2
160: 0e 94 09 02 call 0x412 ; 0x412 <digitalRead>
164: 80 93 3e 01 sts 0x013E, r24
Store a 1 at address 0x013F so the next time here the code above is skipped.
168: 81 e0 ldi r24, 0x01 ; 1
16a: 80 93 3f 01 sts 0x013F, r24