<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>What's ironic about this is that for switch statements that are
"dense enough" (consecutive case values) gcc will create exactly
this kind of "computed goto" for you if the right optimization
level is selected (but probably limited to some set of target
machines).<br>
</p>
<p>-Pete</p>
<div class="moz-cite-prefix">On 6/30/20 8:57 PM, Jon Wolfe via
TriEmbed wrote:<br>
</div>
<blockquote type="cite" cite="mid:md5:tsvMZdoDl7okKEwjp6Jp2g==">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
<div class="WordSection1">
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">There is a trick you can use with gcc that
is a non-standard C construct where you can use ‘goto’ and
give it a variable containing the address of a label. You then
create an array of label address and you can then dynamically
index that array to jump to various locations. I’ve seen it
used as an optimization technique, and you can also have more
control over the program flow, though it is using the infamous
keyword, Essentially though it end up looking pretty much like
a switch-case. </p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">That is really odd about that gcc bug.
It’s not like I’ve never seen them, but 99.9% of the time
when I thought I had found a compiler bug in C/C++, it turns
out to be something else. (hafl the time one of those things
that disappears with a “clean/rebuild all”) I remember the
Arduino /AVR/gcc linker used to have a bug related to 8-bit
AVR chips that had more than 64KB of flash memory, such as the
ATMega 1284. Those chips address by 16 bit words not bytes, so
128kb of flash is accessible without trick likes far pointers,
but the linker would mess up the address calculations
sometimes I think for interrupt handlers or functions called
by interrupt handlers that crossed the 64kb boundary. </p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div
style="mso-element:para-border-div;border:none;border-top:solid
#E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From:
</b><a href="mailto:triembed@triembed.org"
moz-do-not-send="true">Huan Truong via TriEmbed</a><br>
<b>Sent: </b>Tuesday, June 30, 2020 12:48 PM<br>
<b>To: </b><a href="mailto:triembed@undecidedgames.net"
moz-do-not-send="true">Brian</a><br>
<b>Cc: </b><a href="mailto:triembed@triembed.org"
moz-do-not-send="true">Triangle Embedded Computing
Discussion</a><br>
<b>Subject: </b>Re: [TriEmbed] Hacking a fake vintage radio
(with Arduino + Pi 0)</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
Oh yeah, that explains my issue. I definitely ran into that issue<br>
where I have checked and had no reason to believe I was doing<br>
something wrong, yet, when I evacuated each switch to a function,
the<br>
switch worked correctly. But neither scoping with an anonymous
scope<br>
nor renaming the variables work.<br>
<br>
The reason I used the switch was that I read on stackoverflow at
one<br>
point and someone said that we should use switches instead of
elseifs<br>
when we have a lot of cases. Then, using switches, the compiler
will<br>
be able to (at some point) create a lookup table for you so it's<br>
faster. I doubt that was what happening at least on the Arduino
case.<br>
You'll need a giant lookup table which the uCs don't have memory
for.<br>
I suspect that in a lot of cases, using switches is probably just
as<br>
slow as using elseifs. Now as I see that it is so buggy, I
probably<br>
will not use switches, at least on Arduino.<br>
<br>
Cheers,<br>
- Huan.<br>
<br>
On Tue, Jun 30, 2020 at 9:23 AM Brian via TriEmbed<br>
<a class="moz-txt-link-rfc2396E" href="mailto:triembed@triembed.org"><triembed@triembed.org></a> wrote:<br>
><br>
> Side note:<br>
><br>
> The arduino compiler has bugs in how it handles switch
statements. I've<br>
> run into situations lately where the order of the case
statements matter<br>
> (which it never should); cases are completely ignored, etc.<br>
><br>
> I believe it may be tied to the use of local scoping within a
case, e.g.:<br>
><br>
> switch(thing) {<br>
> case 1:<br>
> {<br>
> // stuff with case-local scope<br>
> }<br>
> break;<br>
> }<br>
><br>
> Syntactically- and semantically-correct code has proven to
generate<br>
> incorrect runtime results.<br>
><br>
> I haven't had time/motivation to submit a bug report, but I
should do<br>
> that. At any rate, a potential workaround is to reorder your
cases.<br>
><br>
> -B<br>
><br>
> On 6/24/20 9:51 PM, Huan Truong via TriEmbed wrote:<br>
> > Thanks Pete!<br>
> ><br>
> > I feel like there was something really mysterious about
the switch statement. Even if I pasted the whole blocks of code of
each function I would have called to the {} inside a case, the
code still wouldn’t work. That baffled me by a mile.<br>
> ><br>
> > But yeah, I spent way too much time on the project that
I’m comfortable with the idea of not understanding some of it now.
The watchdog timer code was baffling too.<br>
> ><br>
> > Please excuse my typos, sent from phone.<br>
> ><br>
> > On Jun 24, 2020, at 10:14 AM, Pete Soper via TriEmbed
<a class="moz-txt-link-rfc2396E" href="mailto:triembed@triembed.org"><triembed@triembed.org></a> wrote:<br>
> ><br>
> > What a beautifully presented adventure. Loved reading
it. And when you say a problem "could be bad" you make your point.
:-) (meant as a "find Waldo" exercise for alert readers)<br>
> ><br>
> > Hadn't heard of "kev" or any other Arduino emulator for
that matter. That aspect was interesting too.<br>
> ><br>
> > The other issue with redeclaration of the vars local to
the switch statement is that they literally don't exist outside
it, so communicating their values outside the block would be
difficult. :-) In general, every {} defines a local scope in C/C++
and you can declare variables inside that scope but they cease to
be defined outside the scope. The scope outside any {} (aka
"global") or vars declared "static" can avoid this issue but not
the redefine issue.<br>
> ><br>
> > Thanks for sharing this!<br>
> ><br>
> > Pete<br>
> ><br>
> ><br>
> >> On 6/24/20 12:43 PM, Huan Truong via TriEmbed wrote:<br>
> >> This has taken me way more time than I thought, but
finishing this<br>
> >> retrofit is a big achievement for me. It's really
silly and serves<br>
> >> exactly no purpose other than RE'ing something no
one cares about. So<br>
> >> I just want to share for some shits and giggles.<br>
> >><br>
> >>
<a class="moz-txt-link-freetext" href="http://www.tnhh.net/posts/adventures-hacking-fake-vivitar-vintage-radio.html">http://www.tnhh.net/posts/adventures-hacking-fake-vivitar-vintage-radio.html</a><br>
> >><br>
> >><br>
> >><br>
> >><br>
> >><br>
> ><br>
> > _______________________________________________<br>
> > Triangle, NC Embedded Computing mailing list<br>
> ><br>
> > To post message: <a class="moz-txt-link-abbreviated" href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a><br>
> > List info:
<a class="moz-txt-link-freetext" href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
> > TriEmbed web site: <a class="moz-txt-link-freetext" href="http://TriEmbed.org">http://TriEmbed.org</a><br>
> > To unsubscribe, click link and send a blank message:
<a class="moz-txt-link-freetext" href="mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe">mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe</a><br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > Triangle, NC Embedded Computing mailing list<br>
> ><br>
> > To post message: <a class="moz-txt-link-abbreviated" href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a><br>
> > List info:
<a class="moz-txt-link-freetext" href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
> > TriEmbed web site: <a class="moz-txt-link-freetext" href="http://TriEmbed.org">http://TriEmbed.org</a><br>
> > To unsubscribe, click link and send a blank message:
<a class="moz-txt-link-freetext" href="mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe">mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe</a><br>
> ><br>
><br>
><br>
> _______________________________________________<br>
> Triangle, NC Embedded Computing mailing list<br>
><br>
> To post message: <a class="moz-txt-link-abbreviated" href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a><br>
> List info:
<a class="moz-txt-link-freetext" href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
> TriEmbed web site: <a class="moz-txt-link-freetext" href="http://TriEmbed.org">http://TriEmbed.org</a><br>
> To unsubscribe, click link and send a blank message:
<a class="moz-txt-link-freetext" href="mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe">mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe</a><br>
><br>
<br>
<br>
-- <br>
<br>
Huan Truong<br>
www tnhh.net / twitter @huant<br>
<br>
_______________________________________________<br>
Triangle, NC Embedded Computing mailing list<br>
<br>
To post message: <a class="moz-txt-link-abbreviated" href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a><br>
List info:
<a class="moz-txt-link-freetext" href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a><br>
TriEmbed web site: <a class="moz-txt-link-freetext" href="http://TriEmbed.org">http://TriEmbed.org</a><br>
To unsubscribe, click link and send a blank message:
<a class="moz-txt-link-freetext" href="mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe">mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe</a><br>
<br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
Triangle, NC Embedded Computing mailing list
To post message: <a class="moz-txt-link-abbreviated" href="mailto:TriEmbed@triembed.org">TriEmbed@triembed.org</a>
List info: <a class="moz-txt-link-freetext" href="http://mail.triembed.org/mailman/listinfo/triembed_triembed.org">http://mail.triembed.org/mailman/listinfo/triembed_triembed.org</a>
TriEmbed web site: <a class="moz-txt-link-freetext" href="http://TriEmbed.org">http://TriEmbed.org</a>
To unsubscribe, click link and send a blank message: <a class="moz-txt-link-freetext" href="mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe">mailto:unsubscribe-TriEmbed@bitser.net?subject=unsubscribe</a>
</pre>
</blockquote>
</body>
</html>