SRA(右移算术)与SRL(右移逻辑) [英] sra(shift right arithmetic) vs srl (shift right logical)

查看:1359
本文介绍了SRA(右移算术)与SRL(右移逻辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请这两个伪组装code片来看看:

1)

 里$ t0,53SLL $ T1,$ t0,2
SRL $ T2,$ t0,2
SRA $ T3,$ t0,2打印$ T1
打印$ T2
打印$ T3

2)

 李$ T0,-53
SLL $ T1,$ t0,2
SRL $ T2,$ t0,2
SRA $ T3,$ t0,2打印$ T1
打印$ T2
打印$ T3

在第一种情况下的输出为:搜索
212结果
13结果
13

在后者是:搜索
-212结果
107374 ...结果
-14结果
但不宜:SRA(-53)= - (SRL 53)


解决方案

  -53 = 1111111111001011           SRA 2      1111111111110010(11)= -14
       ^^ ^^
      签署下降
    延期

由于额外的比特被简单地丢弃了正反两方面的结果,结果总是如果查看转变为一个部门四舍五入。

  53 SRA 2 =地板(二分之五十三^ 2)=地板(13.25)= 13
-53 SRA 2 =地板(-53 / 2 ^ 2)=地板(-13.25)= -14

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) ?

解决方案

-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

这篇关于SRA(右移算术)与SRL(右移逻辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆