1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

How to create stack underflow exception ?

Discussion in 'Microcontrollers' started by jani12, Aug 7, 2017.

  1. jani12

    jani12 Member

    Joined:
    Oct 16, 2014
    Messages:
    54
    Likes:
    0
    I'm programming Infineon TC29x Aurix Tricore Microcontroller based embedded system in 'C' language.
    Consider stack starting at address 0x5000 0000. The stack size is 0x1000.

    If I add 0x1000 to current stack pointer(Register A10) and store result in A10, then I should get underflow error.

    What might be the code in assembly to change stack pointer? something like following:

    stw a10, 0x1000(a10) # I'm not sure if stw is correct opcode ?

    what are other options to create stack undeflow exception?
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Write a function... Call it from main... Whilst you are in the function call ASM and decrease the stack pointer, then a return to main will cause an underflow, BUT!! Its not catchable, the program will go haywire..

    If the MPU has multiple cores, then you may have multithreading to create a supervisor thread..

    The way you are describing is a stack overflow.. An underflow is on the way back down...
     
  3. jani12

    jani12 Member

    Joined:
    Oct 16, 2014
    Messages:
    54
    Likes:
    0
    "call ASM" what will this do? What would be the exact syntax in my environment?

    "and decrease the stack pointer" ? Our code is written in embedded 'C'. How to write assembly language code to decrease the stack pointer.

    I didn't quite grasp. How returning to main will cause underflow.

    "BUT!! Its not catchable, the program will go haywire.." I don't understand!

    There are Two cores. Each core has a stack.

    OK. I meant to describe underflow.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    No idea... Check the manual, I would just decrement Register A10...
    No idea... Check the manual. I know how to do it on the chip I use.. There is always an "inline asm tool"

    To create an underflow you need to pop more than you pushed!! So! to create a stack underflow you need to use ASM as there is no way of doing this in C... C should'nt allow the compilation of such a problem.
    Then it is catchable as I said... One core needs to run a supervisor program to monitor the other


    Why do you need to do this.... If you get a stack underflow then your compiler isn't very good..
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Just looking ahead here!!! You seem to have asked this question all over the web

    This means you have your stacks messed up..

    Register A10 is the hardware stack pointer.... In C the software stack is used!! There is no manipulation of the hardware stack at all!!

    A C stack pointer is created by the compiler and is normally held in high ram... When using ASM its usually up to the programmer to keep his eye on the hardware stack... But us lowly C worshipers, the stack is abundant... as you see from your text The C stack is 0x1000 bytes ( 4096 ).. This can be made bigger but there is no need...
     
    • Agree Agree x 1
  7. jani12

    jani12 Member

    Joined:
    Oct 16, 2014
    Messages:
    54
    Likes:
    0
    I'm doing this to test stack underflow trap.
     

Share This Page