What is an easier to understand definition of tst
and what is the equivalent to it in C?
Some background
There is no equivalent in C because higher-level languages work differently than a CPU with "status registers" (such as ARM or x86):
In high-level languages like C or C++, conditional code execution can be done directly:
if(a < b) ...
On a CPU with a "status register", conditional code execution is done in two steps:
- In the first step, you perform some calculation (such as
a-b
).
In the so-called "status register" the CPU stores some "relevant" information (e.g. the sign) about the result.
- In the second step, the actual conditional code execution is done.
This can only be done depending on the information in the "status register".
A simplified example:
The operation if(a < b) ...
could be performed the following way on a CPU with a "status register":
/* Subtract b from a */
c = a - b;
/* (a < b) means that (a - b) is negative */
if(status.last_result_was_negative) ...
... however, the result of the operation (c
in the example) is not needed.
The TST
and CMP
instructions
To perform an if(...)
operation, two operations are often needed:
- A subtraction:
it is needed for ==
, <
, >
, <=
, >=
and !=
.
- An AND operation:
it is needed to check if some bit(s) in a value is (are) set:
if(a & 0x8000) ...
... and in both cases, the result of the operation (the difference or the result of the AND operation) is not needed.
For this reason, there are two instructions (CMP
and TST
) that perform an operation (CMP
performs a subtraction and TST
performs an AND operation) but discard the result:
The TST
operation performs an AND operation, sets the information in the "status register" according to the result but it discards the actual result.
This makes sense in lines like if(a & 0xF000) ...
where you are only interested in that the "status register" holds the information if the result of the operation a & 0xF000
was zero or not, but you are not interested in the actual result of a & 0xF000
.
if(valRead & 0x80000000 != 0){}
You need brackets:
if((valRead & 0x80000000) != 0){}
Otherwise the compiler understands:
if(valRead & (0x80000000 != 0)){}
... which is the same as:
if((valRead & 1) != 0){}