简体   繁体   English

msp430 中的汇编否定

[英]Assembly negation in msp430

I have the following versions of negating some integer value (in R12) in assembly in msp430:在 msp430 的汇编中,我有以下版本的否定一些 integer 值(在 R12 中):

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. INC dst使用ADD #1, dst进行模拟,因此前两个版本完全相同。

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.至于第三个版本:在二进制补码表示中,将所有位取反计算负一,因此您正在计算 (- x - 1) + 1 或 -( x + 1) + 1,这确实是相同的。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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