So here I have the following code (ARMv6 assembly):
wait$:
ldr r2,[r0,#24] //read 32 bits from addr + 24 in r0 and store in r2
tst r2,#0x80000000 //????
bne wait$
I understand all of the lines other than the tst instruction. I have done some research online and the best definition I could find was:
Test if a register is zero or minus. Perform a logical AND between a register and itself.
I had some trouble understanding what it meant so I then tried to make the C equivalent to an tst instruction and this is what I got:
if(valRead & 0x80000000 != 0){}
The code above does not seems to be working. 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:
On a CPU with a "status register", conditional code execution is done in two steps:
a-b).In the so-called "status register" the CPU stores some "relevant" information (e.g. the sign) about the result.
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":... however, the result of the operation (
cin the example) is not needed.The
TSTandCMPinstructionsTo perform an
if(...)operation, two operations are often needed:it is needed for
==,<,>,<=,>=and!=.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 (
CMPandTST) that perform an operation (CMPperforms a subtraction andTSTperforms an AND operation) but discard the result:The
TSToperation 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 operationa & 0xF000was zero or not, but you are not interested in the actual result ofa & 0xF000.You need brackets:
Otherwise the compiler understands:
... which is the same as: