【问题标题】:Assembly negation in msp430msp430 中的汇编否定
【发布时间】:2020-02-03 04:06:30
【问题描述】:

我在 msp430 的汇编中有以下版本的否定某些整数值(在 R12 中):

inv R12
inc R12

这是根据手册,我认为这会起作用吗?

inv R12
add #1, R12

但这会起作用吗?为什么不呢? :

sub #1, R12
inv R12

还是新手,感谢您的帮助!

【问题讨论】:

  • 似乎有效(溢出情况除外)
  • 你检查过极端情况吗,比如INT_MININT_MAX

标签: assembly twos-complement msp430 negate


【解决方案1】:

INC dst是模拟ADD #1, dst,所以前两个版本完全一样。

至于第三个版本:在二进制补码表示中,反转所有位会计算负数减一,因此您正在计算 (−x − 1) + 1 或 -(x  + 1) + 1,确实是一样的。

如果您想要更实际的演示,只需使用蛮力:

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

【讨论】:

  • 即使考虑 alu 标志输出,输出是否相同?
  • @Damiano 那将是一个不同的问题。 (这可以通过手册来回答。)
猜你喜欢
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多