问题
I have a school assignment that requires me to convert a word from little endian to big endian in three different ways. One of them is by using multiplication and division. I know that shifting to the left multiplies the number by 2 but i still cant figure out how to utilise that.
Here is me doing it using rotate. Can someone help me step on this and do it with division and multiplication?
.data
.text
.globl main
main: li $t0,0x11223344 #number to be converted in t0
rol $t1,$t0,8
li $t2,0xFF00FF00 #mask in $t2
and $t3,$t1,$t2
ror $t1,$t0,8
li $t2,0x00FF00FF #mask in $t2
and $t1,$t1,$t2
or $t3,$t3,$t1
ror $t3,$t3,16
li $v0,10
syscall
I cant seem to convert the rotates to shifts correctly. I am doing for rol $t1,$t0,8:
#rol $t1,$t0,8
loop: beq $t1,8,exit #branch to exit if t5=8
addi $t5,$t5,1 #t5++
srl $t1, $t0, 1 #shift original word 1 bit to the right
sll $t2, $t0, 31 #shift original word 31 bits to the left
or $t1, $t1, $t2
but it is not correct.
Help is appreciated, thanks.
回答1:
It's kinda late, but i made this program using only multiplication and division instead of rotate and shift. I hope it helps.
.data
test: .space 4 .text .globl main main: li $t0, 0x11223344
la $t1, test
sw $t0, ($t1)
start of conversion
mul $t1, $t0, 256
li $t2, 0x00FF0000 # $t2 contains mask 0x00FF0000
and $t3, $t1, $t2 # byte 2 valid
div $t1, $t0, 256
li $t2, 0x0000FF00 # $t2 contains mask 0x0000FF00
and $t1, $t1, $t2 # byte 1 valid
or $t3, $t3, $t1
mul $t1, $t0, 16777216
li $t2, 0xFF000000 # $t2 contains mask 0xFF000000
and $t1, $t1, $t2 # byte 3 valid
or $t3, $t3, $t1
div $t1, $t0, 16777216
li $t2, 0x000000FF # $t2 contains mask 0x000000FF
and $t1, $t1, $t2 # byte 0 valid
or $t3, $t3, $t1 # little endian-number in $t3
la $t1, test
sw $t3, ($t1) # store to address pointed by label test
jr $ra
来源:https://stackoverflow.com/questions/53695381/little-to-big-endian-using-multiplication-and-division-mips-assembly