大端到小Endian使用MIPS没有逻辑操作?(Big Endian to Little Endia

2019-09-18 09:37发布

我使用MIPS(QtSpim)隐蔽从大端到小Endian一个32位字。 我下面展示检查和纠正。 不过,我想知道还有什么其他的方法可以让我做转换。 我虽然只用旋转和变化,但我没能做到这一点没有逻辑运算。

所以我的问题是,是否可以不用逻辑操作呢?

li $t0,0x12345678     # number to be converted supposed to be in $t0
rol $t1,$t0,8         
li $t2,0x00FF00FF     # $t2 contains mask 0x00FF00FF
and $t3,$t1,$t2     # byte 0 and 2 valid
ror $t1,$t0,8       
not $t2,$t2        # $t2 contains mask 0xFF00FF00
and $t1,$t1,$t2     # byte 1 and 3 valid
or $t3,$t3,$t1      # little endian-number in $t3

Answer 1:

这里有云不使用逻辑运算符的解决方案。 然而,这只是一个黑客:

  li $t0,0x12345678   # number to be converted supposed to be in $t0

  swl $t0, scratch+3
  lwl $t1, scratch    # Load MSB in LSB
  lwr $t1, scratch+3  # Load LSB in MSB


  swl $t0, scratch+2
  lwr $t2, scratch    # Swap second and
  lwl $t2, scratch+1  # third bytes

  sw $zero, scratch 
  lwl $t2, scratch    # Leave MSB and LSB in zero
  lwr $t2, scratch+3
  addu $t3, $t1, $t2  # Add partial results to get final result

 .data 0x2000  # Where to locate scratch space (4 bytes)
scratch:
 .space 4

输入是$t0 ,部分结果是$t1$t2和最终的结果是$t3 。 它还使用4个字节的存储器(位于scratch



文章来源: Big Endian to Little Endian using MIPS without logical operations?
标签: assembly mips