8 beware of stack corruption – Texas Instruments MSP50C6xx User Manual

Page 353

Advertising
background image

Beware of Stack Corruption

5-57

Code Development Tools

5.8

Beware of Stack Corruption

MSP50C614/MSP50P614 stack (pointed by R7 register) can easily get
corrupted if care is not taken. Notice the following table read code:

SUBB R7, 4

MOV A0, *R7––

ADD A0, address

MOV A0, *A0

ADD A0, *R7––

MOV A0, *A0

RET

This code will work perfectly well if no interrupts happen before SUBB and
MOV instruction. If interrupts do happen between SUBB and MOV
instructions, the parameter in the stack is corrupted by the return address
pushed by the hardware. This problem may not be easily observed in the
system level. But once it happens, it is very difficult to debug. Use the following
method to modify stack pointer instead:

MOV A0, *R7 + –2 * 2

ADD A0, address

MOV A0, *A0

ADD A0, *R7 + –2 * 1

MOV A0, *A0

RET

This method will not have the stack corruption problem since the MOV instruc-
tion performs the entire operation either before or after an interrupt.

Advertising