<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: S12X stack pointer in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146116#M3970</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unlike register variables, auto variables have the&amp;nbsp;address. I don't think compiler has the right to replace real address of variable with something meaningless. I don't mind allocating auto variables in registers, I don't mind if stk isn't allocated on stack, but compare expression has to&amp;nbsp;compare something meaningful, not random data because of too smart optimization.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even with volatile, it is still rubbish, because C doesn't require stack at all. Compiler can allocate stk not in the stack space, like it is done in C compilers for PIC (PIC16, PIC18).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;uint32_t doesn't help, because there are at least 3 16bits CPU registers, more than enough for uint32_t. Also optimizer may be too smart and ignore not used high order bits.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Sep 2010 16:21:03 GMT</pubDate>
    <dc:creator>kef</dc:creator>
    <dc:date>2010-09-10T16:21:03Z</dc:date>
    <item>
      <title>S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146112#M3966</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would like to read the current value of the stack pointer from within my C-code. Is this possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't find the SP register in the S12X manual.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;J&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 09 Sep 2010 17:09:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146112#M3966</guid>
      <dc:creator>JohnBarber</dc:creator>
      <dc:date>2010-09-09T17:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146113#M3967</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;foo()&amp;nbsp;&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; short stkptr;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm{&amp;nbsp; STS&amp;nbsp; stkptr }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want to check if SP isn't lower than some critical threshold, then just take address of function scope auto variable (on stack) and compare it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;foo()&amp;nbsp;&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;void *stk;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; stk = &amp;amp;stk;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if(stk &amp;lt; (void*)0x2000)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Sep 2010 02:00:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146113#M3967</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-09-10T02:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146114#M3968</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Sep 2010 12:35:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146114#M3968</guid>
      <dc:creator>JohnBarber</dc:creator>
      <dc:date>2010-09-10T12:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146115#M3969</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Isn't there a risk that the optimizer will put "stk" into an index register? I'm not quite sure what would happen in that case, but I guess that you'd get the current content of the index register in return, ie rubbish.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd declare that pointer as volatile just to be on the safe side.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I don't think C allows the cast from void** to void*. I would do like this instead:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;foo()&amp;nbsp;&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; volatile uint16_t stack;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; stack = (uint16_t) &amp;amp;stack;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; if(stack &amp;lt; 0x2000)&lt;BR /&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To be even more sure one can use a uint32_t as that will never fit into the D acc of the S12, and thereby can't be optimized.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Sep 2010 15:52:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146115#M3969</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-09-10T15:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146116#M3970</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unlike register variables, auto variables have the&amp;nbsp;address. I don't think compiler has the right to replace real address of variable with something meaningless. I don't mind allocating auto variables in registers, I don't mind if stk isn't allocated on stack, but compare expression has to&amp;nbsp;compare something meaningful, not random data because of too smart optimization.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even with volatile, it is still rubbish, because C doesn't require stack at all. Compiler can allocate stk not in the stack space, like it is done in C compilers for PIC (PIC16, PIC18).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;uint32_t doesn't help, because there are at least 3 16bits CPU registers, more than enough for uint32_t. Also optimizer may be too smart and ignore not used high order bits.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Sep 2010 16:21:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146116#M3970</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-09-10T16:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: S12X stack pointer</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146117#M3971</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Out of curiousity I disassembled the two examples. Both translate into the same code,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;; with void*&lt;/P&gt;&lt;P&gt;LEAX 2,-SP&lt;BR /&gt;STX 0,SP&lt;BR /&gt;CPX #8192&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;; with integer&lt;BR /&gt;LEAX 2,-SP&lt;BR /&gt;STX 0,SP&lt;BR /&gt;LDX 0,SP&lt;BR /&gt;CPX #8192&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After a &amp;nbsp;bit of research I also found out that the pointer cast is valid, just because it is a void pointer. Though to be picky, stk &amp;lt; (void*)0x2000) is undefined behavior in the C language, as the pointers don't point at the same aggregate.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Sep 2010 18:38:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/S12X-stack-pointer/m-p/146117#M3971</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-09-10T18:38:07Z</dc:date>
    </item>
  </channel>
</rss>

