Built-in output screen

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!
Is the following code valid?

#include 
int main(void)
{
int a;
a=4;
}

If it is then I would like to see the value of "a" in the built-in output screen of the Atmelstudio6.1.

What will be the instruction if I want to see the value of a in the built-in output screen?

Thanks in advance!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm also curious to know how a value could be displayed in atmel studio .

When I need to see a value or an array at a point I use an LCD display from the micro or I write the code in Codeblocks IDE in "c" and use printf

Yes , the above code is valid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The optimiser will simply discard your 'a' variable so there's no chance of watching it in the simulator /debugger. Read this to understand why:

https://www.avrfreaks.net/index.p...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff... if one moves the int a; outside of main, does it still get thrown away? It is being written to in main at least.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
if one moves the int a; outside of main, does it still get thrown away?

Actually, no. I just tested it (with -Os).

Here is (parts of) the LSS:

00000056 
: int a; int main(void) { a=4; 56: 84 e0 ldi r24, 0x04 ; 4 58: 90 e0 ldi r25, 0x00 ; 0 5a: 90 93 01 01 sts 0x0101, r25 5e: 80 93 00 01 sts 0x0100, r24 62: 80 e0 ldi r24, 0x00 ; 0 64: 90 e0 ldi r25, 0x00 ; 0 66: 08 95 ret

And (parts of) the MAP file:

.bss            0x00800100        0x2
                0x00800100                PROVIDE (__bss_start, .)
 *(.bss)
 *(.bss*)
 *(COMMON)
 COMMON         0x00800100        0x2 BobOptimization.o
                0x00800100                a
                0x00800102                PROVIDE (__bss_end, .)

So there is still room for improvement.. :D

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You also need a "super loop" for embedded C, to keep the code from retURNING when there's no OS. For example, this works if you put as your last line:

 while(1);

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Actually, no. I just tested it

There was little doubt. Once it's global it could clearly be accessed by something in another compilation unit so the compiler has no option but to create the access code.

But the point here really is that pointless testing of the compiler will likely be found out and thrown away by the optimiser. You need to do something concrete with an input or an output for code to be generated to access it - the optimiser is very good at spotting pointless code and eating it.

If you want to do the local a=4 test actually do it on a PC using an i386 compiler and make sure it's set to "Debug" (which likely also means "no optimisation").

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Once it's global it could clearly be accessed by something in another compilation unit so the compiler has no option but to create the access code.

[facepalm] Of course.. [/facepalm]

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, if I copy a to a port pin, will it get thrown away?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
OK, if I copy a to a port pin, will it get thrown away?

Assuming we are back to a local variable:

Yes, the variable will be optimized away
Yes, there will be code generated to put the value 4 onto the port

#include 

int main(void)
{
   uint8_t a;
   a=4;
   PORTB = a;
}
00000046 
: int main(void) { uint8_t a; a=4; PORTB = a; 46: 84 e0 ldi r24, 0x04 ; 4 48: 85 b9 out 0x05, r24 ; 5 4a: 80 e0 ldi r24, 0x00 ; 0 4c: 90 e0 ldi r25, 0x00 ; 0 4e: 08 95 ret

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And the global variable will stay in bss?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

may be a stupid answer but I would hit the line with a non stopping break point ant add the message a = {a}

nigel

EDIT Yes I was correct it was a stupid answer!
I will take my foot out of my mouth now!!

the above doesn't work.

Last Edited: Tue. Aug 20, 2013 - 12:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's either/or. Either you have 'a' as an automatic stack based local OR it's a global in .bss (or I suppose it could be a local in .bss if you made it 'static').

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At first, declaring a as a local and not using it made it get deleted. I asked if declaring it not local and using it would keep it from getting deleted. Trying to form a model of what gets thrown out and when and why.
Note that this example doesnt involve interrupts, so the volatility hint doesnt really apply?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

And the global variable will stay in bss?

Well, that would depend on your toolchain and how you use the variable, I guess.

If I have a global variable that is unused, with my toolchain it would be "tossed" unless I would force it to be kept in some manner.

Consider also the case of a good-old 8-bit unsigned integer loop counter; ala Fortran we'll call it "i". Make i a global variable, that is only used as a loop counter for a single loop (and not referenced as the loop completes).

A good compiler's code generator will just use (say) R21 for the loop counter and never LDS/STS to the global variable. Thus "i" has no references, and b-bye...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Local variable: Gets thrown out if not used, or if used in a way where it is an intermediate (e.g. as in assigning it a literal value, and then using the variable for outputting to a port - the intermediate variable can be thrown out and a simple LDI, OUT sequence which has the same semantics is generated instead).

Global (non-static) variable: Must be generated since the compiler can not know if any other compilation unit will reference it. Thus it has to be generated so that the linker can resolve any references to it.

Global static variable: Untested, but I would not be surprised if it is optimized out. If you're curious re this case then why not test it yourself, Bob?

All the above for avr-gcc which (unless actively switched to do otherwise) exercizes separate compilation

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I would not be surprised if it is optimized out.

I was surprised too:

static int n;

int main(void) {
	n = 12345;
  76:	89 e3       	ldi	r24, 0x39	; 57
  78:	90 e3       	ldi	r25, 0x30	; 48
  7a:	90 93 61 00 	sts	0x0061, r25
  7e:	80 93 60 00 	sts	0x0060, r24

That looks like a missed optimisation to me.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, so now we have to think hard: Is optimizing out a "global" static variable not possible for some other reason? Or is this actually a missed optimization?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As "n" wasn't actually used for anything sensible in my test.c. (I just set it to 12345 then did nothing else with it) then, because it is "static" the compiler must surely realise it is not accessible outside this compilation unit and there's no valid use for it inside so I'd argue it should never exist.

Interestingly, if I remove the "n = 12345" then the .map file reveals that it does not exist. So it's the pointless assignment of 12345 to it that makes it exist.

Even with -fdata-sections and -gc-sections I cannot get rid of it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Trying to form a model of what gets thrown out and when and why.

In general, the model is of-course: Anything can be thrown out as long as the generated code still adheres to the semantics of the source code, interpreted using the C standard. (This is where C is closest to taking on the coat of a high-level language rather than a "super-macro-cross-platfom-assembler-language").

Nothing else is guaranteed across compilers, or across versions of a specific compiler, or across settings of a specific compiler of a specific version.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My head has just exploded....

But a use full lesson has been learnt. test or check ideas or suppositions before posting.

Nigel

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

My head has just exploded....

:mrgreen:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]