> William "Chops" Westfield wrote: > >> On Jun 23, 2011, at 8:29 PM, Harold Hallikainen wrote: >> >>> Is it possible to read the current stack pointer in C? If so, what >>> would the instruction be? >> >> Exactly or approximately? >> >> void *returnsp() >> { >> int a; >> return &a; >> } >> >> Should be pretty close (on almost any processor that uses stack frames >> for local variables.) > > That's probably close, but at the caller's location almost certainly off > by an int, possibly by an int and a return address and possibly some > stack frame overhead (if the function didn't get inlined). > >> Otherwise you get to look at inline assembler and/or assembler >> subroutines. > > And the compiler docs and generated code for how the compiler uses the > stack. > Thanks for the obvious solution that I did not think of? I kept digging through documentation on MIPS assembly, trying to figure this out. I messed around with locals a bit and did come up with a method of matching the pointer to the SP as reported in the processor registers window. What I'm doing now is writing a known pattern to the space between the heap and the stack, running code for a while, then looking back at that space. I can't figure out any other method on a PIC32 to determine whether the stack and the heap have collided. The PIC24 has a stack limit register, but apparently not the PIC32. I assume malloc returns a null pointer when it runs out of space allocated for the heap in the linker, but that does not keep the stack from growing down into the heap space. I think this is what's causing my occasional bus exceptions. Again, thanks for the solution! Harold --=20 FCC Rules Updated Daily at http://www.hallikainen.com - Advertising opportunities available! --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .