I am pretty new to ARM and am looking to compare a value in a register to a selection of known hex values. At the moment I am doing this by using individual CMP instructions so the code looks like this:
;stuff
CMP r2, #0x41
CMP r2, #0x45
CMP r2, #0x49
etc...
clearly this gets pretty cumbersome after a while and I'm thinking there must a way to compare the value in r2 to a number of hex values all at once.
Aplogies for the newbie question, thanks for the help in advance.
Generally, you look at the valid value range.
Step 2,3 are only needed if some values in the range are not valid and the array should be fairly densely pack, which for your two digit hex should be the case.
Obviously, your
key_array
must be changed to be valid with your use case. The condition codes will change slightly for signed and unsigned values.If
key_array
is all ones for the binary case, then steps two and three are justreturn 1;
. If thelow_val
is zero, then you don't need to subtract (or do a low range check). Thekey_array
should be of sizehi_val - low_val + 1
or your range check logic can change.This mechanism is used for many 'ctype' functions such as
ispunct
, but you may store up to eight values in a byte and use a bit mask to get the one you are interested in. It is also easy to implement in 'C' and use a compiler. It is possible to use only one register (R0 is EABI compatible function) but then the error handling is also intermixed and less clear.