What's the difference between the CMP and TST instructions in ARM?

Viewed 11036

I'm having trouble understanding the difference between these two instructions in ARM. From what I understand,

CMP R1, R2

Would perform the action R1-R2, but not store the result. It would then set flags in the status register such as N (negative, if R1

What I don't get is what TST does differently. In my notes it says that TST doesn't change the values of its operands.. but I thought CMP didn't either. How do the two instructions differ?

How is

CMP R1, R2

different to

TST R1, R2

? Thanks in advance for any help!

1 Answers

TST R1, R2 computes the bitwise AND of R1 and R2 and then discards the result while CMP R1, R2 subtracts the two. TST is mainly useful on ARM for finding out if a given bit is set in a number. For example, to check if R1 is odd, you might do:

TST R1, #1    @ is R1 odd?

This sets the Z flags to 0 iff R1 is odd.

Related