From the CPU's point of view, how does it distinguish an assembly code is an ARM code or a Thumb code?
Is there some signal bit represent the code is ARM or Thumb code, this is just my guess.
Opcodes, whether ARM or Thumb, are just bit patterns in memory. It's a question of how those bit patterns are interpreted, which is controlled by the CPSR.T bit (1=Thumb, 0=ARM).
That is, if you execute from an address with T=1 the contents of the address will be treated as Thumb. Execute the same address with T=0, and it will be treated as ARM.
If a person looks at a region of memory, it's usually obvisous whether it contains ARM or Thumb. As one will give you sensible looking code, and the other garbage.
The T bit usually gets updated during branches, such as function calls/returns or exception entry/exit. Sticking with function call/returns, "BL <label>" will branch to function in the same instruction set. While "BLX <label>" will branch to a function in the other instruction set. (You usually don't have to worry which to use, and can leave it to the linker). On the return side, when you write an absolute address into the PC (e.g. "BX lr" or "POP {..., PC}"), bit 0 is used to update the T bit.
Thanks very much for your answer.
And I want to ask one more question is that the CPU how to distinguish thumb and thumb-2 code?
Because thumb-2 code has 16-bit length and 32-bit length code.
A little bit terminology first. Thumb-2 isn't an instruction set, but an extension to Thumb. So in that sense, it doesn't need to distinguish between Thumb and Thumb-2 instructions. The processor either support Thumb-2, or it doesn't.
Whether a Thumb instruction is 16-bit or 32-bit is part of the encoding, specifically the upper bits of the first 16-bit chunk.
Take a look at sections A6.2 and A6.3 of the ARMv7-A/R Architecture Reference Manual.
ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition