On the MSP430, what happens when I cast a null pointer?

I know the dereferencing of a null pointer is undefined - but I would like to know what happens for a specific purpose - MSP430.

I don't have a board to download this in front of me to check it out right now.

What happens if I do this (or the like)?

int * foo = NULL; (*foo)++; //Crash? 

The 0x0 location is in the SFR range and is reserved.

Will it generate PUC / POR? Or does he silently "work"?

Generated assembly

 ;int * foo = NULL; clr.w R15 ;(*foo)++; inc.w R15 

Thus, the location 0x0 literally increases by 1.

When I run this in the simulator, I see that the value at 0x0 goes from 0 to 1. I do not receive any warnings in the debug log, and the program exits normally.

I am using the IAR EW430 compiler / assembler / simulator.

+5
source share
1 answer

Not only writing and reading the 0x0 address will not lead to a crash or reboot, it is actually a completely legitimate operation that is often used by MSP430 applications.

The initial portion or memory card of the MSP430 is reserved for I / O ports and control registers: http://en.wikipedia.org/wiki/TI_MSP430#MSP430_address_space

In particular, the control registers at 0x0 and the following addresses:

  #define IE1_ 0x0000 /* Interrupt Enable 1 */ #define IE2_ 0x0001 /* Interrupt Enable 2 */ #define IFG1_ 0x0002 /* Interrupt Flag 1 */ #define IFG2_ 0x0003 /* Interrupt Flag 2 */ 

So, for example, writing zero to the address of this memory by dereferencing the uint8_t * or uint16_t * will disable interrupts. By writing zero by dereferencing uint32_t * , it will also clear the flags. Increasing the value of these registers does not make much sense, but should be completely legal.

At least this applies to the msp430 Series 1, Series 2, and Series 4 protocols. While checking the header files, I could not find anything matching 0x0 on Series 5 (interrupt control registers are displayed in the area starting with 0x0100).

So, if you want to catch places in the code where the NULL pointer is dereferenced, you are completely at your discretion.

+6
source

Source: https://habr.com/ru/post/1209975/


All Articles