简体   繁体   中英

Assembly negation in msp430

I have the following versions of negating some integer value (in R12) in assembly in msp430:

inv R12
inc R12

this is according to the manual and I think this will work the same?

inv R12
add #1, R12

But will this work and why not? :

sub #1, R12
inv R12

Still new to this and thank you for any help!

INC dst is emulated with ADD #1, dst , so the first two versions are exactly the same.

As for the third version: In the two's complement representation, inverting all bits computes the negative minus one, so you are computing (− x − 1) + 1 or −( x + 1) + 1, which is indeed the same.

And if you want a more practical demonstration, just use brute force:

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

int main()
{
    for (uint32_t i = 0; i < 0x10000; i++) {
        uint16_t input = i;
        uint16_t output1 = (~input) + 1;
        uint16_t output2 = ~(input - 1);
        assert(output1 == output2);
    }
    puts("it works!");
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM