> How to sort an array in assembly MSP430?

How to sort an array in assembly MSP430?

Posted at: 2014-12-18 
Like how would you compare a negative number? -5 would be (FB) but that's larger than +5, isn't it?

You can use the CMP instruction followed by some use of, or combination use of, the conditional jump instructions. Usually, the bulk of an ALU logic (all the logic required to perform ADD and SUB for example) doesn't care about signed or unsigned. It treats everything the same way -- unsigned. However, they usually add a tiny bit of extra logic to "help" in the case of signed numbers where you intend that type of interpretation.

For example, suppose you compare 0xFB (src) with 0x5 (dst). The ALU doesn't even know how to subtract, so instead it converts the 0xFB to its 2's complement and adds. (Read the docs, you'll see this mentioned.) This means it adds 0x5 to 0x5 to get 0xA, which happens to be the span or difference between those two numbers. The N bit will be cleared, Z will be cleared, C will be cleared (no carry adding those two), and V is the XOR of the carry outputs of the two most significant adder stages so in this case is set to 0 as the original addition was 0x05 + 0x05 and there were no carries in those upper adder stages.

For unsigned comparisons:

? ? ? ? src > dst ? ? ? ? ? ? ? C == 1 && Z != 1

? ? ? ? src ≥ dst ? ? ? ? ? ? ? C == 1

? ? ? ? src = dst ? ? ? ? ? ? ? Z == 1

? ? ? ? src ≤ dst ? ? ? ? ? ? ? C == 0 || Z == 1

? ? ? ? src < dst ? ? ? ? ? ? ? C == 0

For signed comparisons:

? ? ? ? src > dst ? ? ? ? ? ? ? (N ^ V) == 0 && Z != 1

? ? ? ? src ≥ dst ? ? ? ? ? ? ? (N ^ V) == 0

? ? ? ? src = dst ? ? ? ? ? ? ? Z == 1

? ? ? ? src ≤ dst ? ? ? ? ? ? ? (N ^ V) == 1 || Z == 1

? ? ? ? src < dst ? ? ? ? ? ? ? (N ^ V) == 1

If you now go to the conditional jump instructions you can figure out which to use in which combinations to achieve what you want.

I just whipped out the above. So I'd recommend that you read the docs and verify what I wrote above. But I think it's right, given a cursory moment.

You will notice that a signed comparison for "less than" is quite a bit different than an unsigned comparison. Luckily, they provide JL and JLO instructions. In some ways, learning about all this is a bit of a pain. But if you've ever designed an ALU yourself as I have, using logic gates, you will understand why it is often done this way. The TI people seem to imagine that the words "less" and "lower" will make you understand... but that's crazy. To most people, those things mean the same thing. But if you twist your mind up into knots, you can "get a feel" for their choice of words eventually. The bottom line is how the actual hardware was designed, though. And if you've ever done even so much as one ripple carry adder chain in your life, you'll understand much more about this.

Like how would you compare a negative number? -5 would be (FB) but that's larger than +5, isn't it?