Continue to Site

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.

  • 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.

How to create stack underflow exception ?

Status
Not open for further replies.

jani12

Member
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?
 
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...
 
"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.
 
call ASM" what will this do? What would be the exact syntax in my environment?
No idea... Check the manual, I would just decrement Register A10...
"and decrease the stack pointer" ? Our code is written in embedded 'C'. How to write assembly language code to decrease the stack pointer.
No idea... Check the manual. I know how to do it on the chip I use.. There is always an "inline asm tool"

"BUT!! Its not catchable, the program will go haywire.." I don't understand!
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.
There are Two cores. Each core has a stack.
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..
 
Just looking ahead here!!! You seem to have asked this question all over the web

If I add 0x1000 to current stack pointer(Register A10)
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...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top