Professional Documents
Culture Documents
Industry Consultant
RomanB60156@yahoo.com
rev: 6/6/03
Microcontrollers Application Note #33 orig: 9/13/99
Stack overflow detection
The default selection for the NEC software simulator, emulator, and single chip
device is to not detect stack overflow conditions. However, the NEC software
simulator and emulator can be setup to check for this important condition.
To adjust the size of the stack, see Microcontroller Application Note #54, which
shows how to minimize RAM and ROM requirements. It also discusses how the
RAM can be allocated to provide more or less stack space.
The first approach is to setup the simulator and emulator to detect stack overflow
conditions.
AN_uc33.doc
Simulator and Emulator
Page 2 of 8
Detecting in a single chip device
However, this stack overflow routine will probably only be used for design
verification, and then will be turned off for the mask device. This section will
describe how this self checking code can be turned on or off, as desired, without
using extra ROM space on the mask device.
Note that if a stack overflow is detected with self checking, that does not mean
that it will necessarily occur in the actual device. For instance, the overhead for
running the function calls is 4 bytes (HL and AX) are pushed onto the stack.
Therefore, if the stack overflow is detected, then use caution to determine the
self check routine is causing the stack overflow.
However, if the self check routine does NOT find problems, then the software
should run fine in the mask device. If the self check routine can handle the 4
extra bytes of overhead (HL and AX) then the software should run fine in the
actual application.
Page 3 of 8
Page 4 of 8
Page 5 of 8
COMMENTS
ACTUAL CODE
#pragma sfr
#pragma asm
#pragma EI
#pragma rot
#pragma STOP
#pragma nop
*/
incw hl
mov [hl],a; 0FDFFh address
pop ax
pop hl
#endasm
#endif
Page 6 of 8
//If stack overflow then keep code otherwise not!
#if CheckStack <-- If stack
check is
void CheckStkOvrFlw() //Check stack overflow enabled then
{ this routine
#asm will be
push hl compiled.
push ax If not, then
ROM is not
movw hl,#0FDFDh; Stack should NOT be here used
mov a,[hl]; First address
cmp a,#0FFh;
bz $?LChk2nd; Everything okay so continue <-- Tags in ASM
br $?LStkErr; Oops, there was an error <-- code which
is embedded
?LChk2nd: in C must
incw hl start with
mov a,[hl]; Second address ?L
cmp a,#0FFh;
bz $?LChk3rd; Everything okay so continue
br $?LStkErr; Oops, there was an error
?LChk3rd:
incw hl
mov a,[hl]; Third address
cmp a,#0FFh;
bz $?LFinChk; Everything okay so done
?LFinChk:
pop ax
pop hl
#endasm
#endif
void main()
{
unsigned int temp;
unsigned int cnt;
unsigned int ExitFlag;
Page 7 of 8
//If stack overflow then keep code otherwise not!
#if CheckStack <-- If check is
SetStkOvrFlw(); off, then do
#endif not call
this
EI(); //Enable all interrupts routine.
while(1)
{ <-- Main loop
Roman Budek
Page 8 of 8