sra(算术右移)与 srl(逻辑右移)

发布于 2024-11-14 14:13:56 字数 397 浏览 8 评论 0原文

请看一下这两段伪汇编代码:

1)

li $t0,53

sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1  
print $t2  
print $t3  

2)

li $t0,-53


sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1
print $t2
print $t3

第一种情况的输出是:
212
13
13

后者中的 是:
-212
107374...
-14
但不应该: sra (-53) = - (srl 53) 吗?

Please take a look at these two pieces of pseudo-assembly code:

1)

li $t0,53

sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1  
print $t2  
print $t3  

2)

li $t0,-53


sll $t1,$t0,2
srl $t2,$t0,2
sra $t3,$t0,2

print $t1
print $t2
print $t3

in the first case the output is:
212
13
13

in the latter is:
-212
107374...
-14
But shouldn't : sra (-53) = - (srl 53) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-11-21 14:13:56
-53 = 1111111111001011

           sra 2

      1111111111110010(11) = -14
       ^^              ^^
      sign           dropped
    extension

由于对于正结果和负结果都会简单地删除额外的位,因此如果将移位视为除法,则结果始终向下舍入。

 53 sra 2 = floor( 53 / 2^2) = floor( 13.25) =  13
-53 sra 2 = floor(-53 / 2^2) = floor(-13.25) = -14
-53 = 1111111111001011

           sra 2

      1111111111110010(11) = -14
       ^^              ^^
      sign           dropped
    extension

Because the extra bits are simply dropped for both positive and negative results, the result is always rounded down if you view the shift as a division.

 53 sra 2 = floor( 53 / 2^2) = floor( 13.25) =  13
-53 sra 2 = floor(-53 / 2^2) = floor(-13.25) = -14
是你 2024-11-21 14:13:56

答案与二进制补码表示法有关。 sra 的目的是支持以二进制补码表示的负数。当以“算术”方式右移时,最高有效位(如果值为负则为 1)会被复制。

在你的 32 位 x86 上,这意味着:

 53 = 00000000000000000000000000110101
-53 = 11111111111111111111111111001011

 srl( 53, 2) =  13 = 00000000000000000000000000001101
               -13 = 11111111111111111111111111110011

 sra(-53, 2) = -14 = 11111111111111111111111111110010

我想要认识到的是,在二进制补码中,数字的负数并不是数字中每个位的反转 - 它是每个位的反转,然后在该数字上加 1。考虑:

 1 = 0000001
-1 = 1111111

不:

-1 = 1111110

这会导致:

 0 = -1 + 1 = 11111111

换句话说,二进制补码中不存在“负零”。零占据了该域中的空间,否则被视为“正号”,因为高位为零。

The answer relates to two's complement notation. The purpose of sra is to support negative numbers represented in two's complement. The most significant bit, which is one if the value is negative, is duplicated when shifted right in the "arithmetic" fashion.

On your 32-bit x86, this means that:

 53 = 00000000000000000000000000110101
-53 = 11111111111111111111111111001011

 srl( 53, 2) =  13 = 00000000000000000000000000001101
               -13 = 11111111111111111111111111110011

 sra(-53, 2) = -14 = 11111111111111111111111111110010

I suppose the thing to realize is that in two's complement, the negative of a number is not the inversion of every bit in the number -- it is the inversion of every bit, then the addition of 1 to that number. Consider:

 1 = 0000001
-1 = 1111111

Not:

-1 = 1111110

Which would lead to:

 0 = -1 + 1 = 11111111

In other words, there is no "negative zero" in two's complement. Zero takes up space in the realm otherwise considered "positive sign" because the high bit is zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文