Hi All,
I was trying to grasp the concept of variable scope and I was looking at the lss file.
so i have this.
#includeuint8_t fcn(void) { uint8_t foobar = 0; foobar++; return foobar; } int main(void) { while(1) { uint8_t foo = 0; static uint8_t bar = 0; bar++; foo = fcn(); //TODO:: Please write your application code } }
which generates this.
#includeuint8_t fcn(void) { 84: cf 93 push r28 86: df 93 push r29 88: 1f 92 push r1 8a: cd b7 in r28, 0x3d ; 61 8c: de b7 in r29, 0x3e ; 62 uint8_t foobar = 0; 8e: 19 82 std Y+1, r1 ; 0x01 foobar++; 90: 89 81 ldd r24, Y+1 ; 0x01 92: 8f 5f subi r24, 0xFF ; 255 94: 89 83 std Y+1, r24 ; 0x01 return foobar; 96: 89 81 ldd r24, Y+1 ; 0x01 } 98: 0f 90 pop r0 9a: df 91 pop r29 9c: cf 91 pop r28 9e: 08 95 ret 000000a0 : int main(void) { a0: cf 93 push r28 a2: df 93 push r29 a4: 1f 92 push r1 a6: cd b7 in r28, 0x3d ; 61 a8: de b7 in r29, 0x3e ; 62 while(1) { uint8_t foo = 0; aa: 19 82 std Y+1, r1 ; 0x01 static uint8_t bar = 0; bar++; ac: 80 91 00 01 lds r24, 0x0100 b0: 8f 5f subi r24, 0xFF ; 255 b2: 80 93 00 01 sts 0x0100, r24 foo = fcn(); b6: 0e 94 42 00 call 0x84 ; 0x84 ba: 89 83 std Y+1, r24 ; 0x01 //TODO:: Please write your application code } bc: f6 cf rjmp .-20 ; 0xaa
I have a few questions about this.
1. why subi? why not inc?
2. I know that everytime I enter a function, the non-static variables are "re-created" and they vanish when I exit the function. But looking at the lss, this is also true for loops? foo was re-initialized to zero.
3. The static bar was given a fixed address e.g. 0x100. Is this in RAM?
4. What about the non-static variables? They are referenced as Y+1. Where is this? Instruction set says that Y=R29:R28. Does this mean that foo is in 0x3E3D+1? why there?
5. Also, r1 is always zero?