Detect stack overflows

The program stack in a program is used to store temporary information such as z.Bsp. Addresses or variables. This includes z.Bsp. also all internal Luna functions, which in turn of course also need to back up or store temporary data. is a stack grows from the end of the working memory of the controller back to the beginning. a stack is comparable to a sticky note picker. You put information from by plugging a note on the Spicker, then a more etc. If you want to zoom to the lowest note, you must first disconnect from Spicker all other notes. The stack value is, therefore, so to speak, a virtual border drawn „it may only xy notes on the Spicker, then connection“. By When specifying the stack size in a Luna program

avr.stack = 32

it specifies where it ends in the memory thus. This information is critical for a Luna program, since it limits the different memory areas are defined internally. If now exceeded the end of the stack by a programming error or a too small sized stack, the data is then written to the Luna dynamically managed memory area or even to the variables. This then leads to serious bugs and sporadic crashes. In such an event is called a stack overflow. Since you only can estimate in advance how many bytes are needed to stack, it is necessary to have accurate information about whether the stack is actually sufficiently dimensioned at its specified size. To this end, Luna offers an appropriate monitoring function, a StackOverflow-exception. If you want to test your program, you should examine all possible program states can adopt the program enabled StackOverflow-exception on the controller. The activation is done through implementation of the corresponding exception handler:

exception StackOverflow
  'Program for the information output
endexception

If there is no exception on the stack is sufficiently strong and you can be sure that no stack overflow occurs. Sample application, which provoked a stack overflow.

const F_CPU=20000000
avr.device = atmega328p
avr.clock  = F_CPU
avr.stack = 32
uart.baud = 19200
uart.recv.enable
uart.send.enable
print 12;"stack overflow example"
overflow()
do
loop
procedure overflow()
  print "overflow(), SP = ";str(avr.StackPointer)
  waitms 50
  overflow() 'calls itself again
endproc
exception StackOverflow
  print "*** StackOverFlowexception ***"
endexception