Program 11: All Positivity
Revisiting the N flag #
You learned briefly that the N flag is set if the number, interpreted as signed,
used to set the CPSR is a negative number. You may be surprised to know, that
really this flag is a mirror of the most significant bit. No, seriously. It’s that
simple. Imagine the ARM processor had 8-bit registers as opposed to 32-bit.
Number | 8-bit register | N set? |
---|---|---|
22 | 0b00010110 | N |
-22 | 0b11101010 | Y |
200 | 0b11101010 | Y |
The number 200 again illustrates that the processor itself inherently does not know if it is working with positive or negative numbers, and it’s a construct the user determines.
LDRSB/STRSB/LDRSH/STRSH operations #
Unfortunately(?), the processor DOES have 32-bit registers. This means that if you are working with numbers that are less than 32-bits, you need to be aware or you could make some negative numbers positive very quickly.
Imagine you have these two numbers stored in single bytes: 6, -7
If you used the STRB operation to move 6 from memory to a register, there won’t be a problem.
memory: 00000000 6
ldrb:
result: 00000000 00000000 00000000 00000110 6
But if you tried doing this with -7, you all of a sudden get a positive number.
memory: 11111001 -7
ldrb
result: 00000000 00000000 00000000 11111001 249
To accommodate for this, the architects implemented signed load and store instructions for byte and half word operations. 1 Essentially it just takes the bit in the most significant position of the loaded data and fills that to the rest of the data space.
memory: 11111001 -7
lrdsb
result: 11111111 11111111 11111111 11111001 -7
-
No need to do this for word operations because the load/str will never add empty bytes to the register. ↩︎