2012-06-22 54 views
4

我正在使用MIPS(QtSpim)将Big Endian中的32位单词转换为Little Endian。我在下面显示的是检查和更正。不过,我想知道还有哪些方法可以让我进行转换。我虽然只是使用旋转和移位,但我没有设法做到这一点,没有逻辑操作。使用MIPS而没有逻辑操作的Big Endian到Little Endian?

所以我的问题是,它可以做到没有逻辑操作?

enter image description here

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 

回答

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

+0

非常感谢您的支持。我已经接受你的答案。最后一个是/否的问题,这只能通过逻辑运算来解决吗? – EnexoOnoma

+0

@Kaoukkos:我相信你不能仅仅使用**逻辑运算来解决它,因为你需要一些手段来移动(移​​位)字节。 – gusbro