0

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.

  • First convert your code to shift instead of rotate. Then convert your shifts to multiplication/division. – Jester Dec 09 '18 at 19:05
  • @Jester I cant convert it to shifts correctly. I uploaded my attempt. –  Dec 09 '18 at 19:08
  • There is really no point in using rotates to start with so you can write the code with shifts from scratch. `out=(in << 24) | ((in & 0xff00) << 8) | ((in & 0xff0000) >> 8) | (in >> 24)`. – Jester Dec 09 '18 at 19:35

1 Answers1

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
4ndr3
  • 11
  • 3