Program counter goes to the end of code and 8051 Restarts
I found out the reason. It is actually the problem of the compiler. The keil C51 v9.53 has a problem that sometimes it mixes the stack with data. They have proposed a new linker. But I upgraded to v 9.55 and the problem solved.