<?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: HCS08- Stack Control in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219988#M19144</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, it's been a year since this thread was active, but maybe you are still "around"...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inline assembler for reading the SP to a c-variable;&amp;nbsp;what would this look like. Totally inexperienced in assembler coding...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 25 Nov 2011 22:30:12 GMT</pubDate>
    <dc:creator>klasen</dc:creator>
    <dc:date>2011-11-25T22:30:12Z</dc:date>
    <item>
      <title>HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219984#M19140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I read the stack pointer address in a C language code? I am using the MC9S08LC60 MCU.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to have some control of my stack pointer where-abouts. I'm afraid it will overflow when I least expect it, specially since I have only estimated it, but am not sure at all how close to overflowing I actually am.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a timer interrupt, and I believe that's the most critical point, since it may be triggered when the main thread is in it's most nested condition (where stack is at it's lowest), so if I can detect the lowest stack value in that condition, I'll have a good estimate if my stack is well dimentioned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have other stack control ideas, I'll be happy to hear them.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Aug 2010 18:09:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219984#M19140</guid>
      <dc:creator>rdazcal</dc:creator>
      <dc:date>2010-08-30T18:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219985#M19141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The easiest and cleanest way is to use inline assembler: then you can read the SP right away. In pure C language you would have to use some sort of dirty hack like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;interrupt void isr (void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; volatile uint32 debug_sp = 0; &amp;nbsp;/* volatile so that it wont be optimized away, uint32 so that it is guaranteed to end up on the stack and not in an accumulator. */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if( (uint16)&amp;amp;debug_sp - STACK_START &amp;gt; some_size)&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; do_something();&lt;/P&gt;&lt;P&gt;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another trick is to download the program with the BDM, then in the debugger set the whole stack area to some unlikely value like 0xAA. Let the program run for a while, halt it and check the stack, to see at which address 0xAA still exists without having been overwritten. If there is no 0xAA anywhere, the stack has overflowed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Aug 2010 20:01:17 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219985#M19141</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-08-30T20:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219986#M19142</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;UL&gt;&lt;LI&gt;&amp;nbsp;have a timer interrupt, and I believe that's the most critical point, since it may be triggered when the main thread is in it's most nested condition (where stack is at it's lowest), so if I can detect the lowest stack value in that condition, I'll have a good estimate if my stack is well dimentioned.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not&amp;nbsp;at all. Timer interrupt is often synchronized with main routine. If for example main thread is not executing most nested routines until timer interrupt signals something, then your most critical point can be synchronized and happen always somewhere between two adjacent timer interrupt events.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could clear&amp;nbsp;stack space or initialize it with some constant&amp;nbsp;before enabling interrupts and jumping to main(). Then you could periodically scan stack space upwards from the bottom to find first occurence of modified byte. Or, better, to make&amp;nbsp;checks faster, just check if safe minimum of N bytes was not modified.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 30 Aug 2010 20:11:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219986#M19142</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2010-08-30T20:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219987#M19143</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lundin, Kef&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Filling the RAM with 0xAA in BDM worked beautifully! I can now see clearly how deep the stack goes!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both for your answers!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 31 Aug 2010 02:32:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219987#M19143</guid>
      <dc:creator>rdazcal</dc:creator>
      <dc:date>2010-08-31T02:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219988#M19144</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, it's been a year since this thread was active, but maybe you are still "around"...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inline assembler for reading the SP to a c-variable;&amp;nbsp;what would this look like. Totally inexperienced in assembler coding...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Nov 2011 22:30:12 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219988#M19144</guid>
      <dc:creator>klasen</dc:creator>
      <dc:date>2011-11-25T22:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219989#M19145</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello, and welcome to the forum.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe the following for HCS08&amp;nbsp;-&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;unsigned int readSP( void){   unsigned int temp;   __asm  tsx;      // Transfer stack pointer+1 to H:X   __asm  aix  #-1;   __asm  sthx temp;   return temp;}&lt;/PRE&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 26 Nov 2011 14:20:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219989#M19145</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2011-11-26T14:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219990#M19146</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks! Exactly what I was looking for!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Nov 2011 21:35:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219990#M19146</guid>
      <dc:creator>klasen</dc:creator>
      <dc:date>2011-11-28T21:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08- Stack Control</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219991#M19147</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps I should clarify one aspect, which I had previously over-looked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the&amp;nbsp;previous&amp;nbsp;code snippet described a separate function, in which a 16-bit local variable was also&amp;nbsp;defined, the stack pointer address returned by the function will be that within the function.&amp;nbsp; However, if you require the stack pointer address present just prior to the function call, the adjustment should be&amp;nbsp; &lt;FONT face="courier new,courier"&gt;__asm&amp;nbsp; aix&amp;nbsp; #3&lt;/FONT&gt;, rather than &lt;FONT face="courier new,courier"&gt;__asm aix #-1&lt;FONT face="arial,helvetica,sans-serif"&gt;, to allow for both&amp;nbsp;the local variable and the return address.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;Mac&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 Dec 2011 20:25:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Stack-Control/m-p/219991#M19147</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2011-12-01T20:25:01Z</dc:date>
    </item>
  </channel>
</rss>

