Question

I am trying to convert hex to binary. I am now stuck. Storing the bite returns an error "address out of range". Here is my code:

.data
Input: .word 0x12345678
buff: .space 40
la $a0, Input
la $a1, buff
.text

move $t2, $a0 
    li $t1, 0 
Loop:
    rol $t2, $t2, 1 
    and $t0, $t2, 1
    add $t0, $t0, 48
    move $a0, $t0
    li $v0, 11
    syscall
    sb $a1, 0($a0) # Runtime exception at 0x00400228: address out of range 0x000000030
    subi $t1, $t1, 1
    bne $t1, 32, Loop
    jr $ra

I found out that during compilation Mips turns the hex into decimal form, meaning that 0x12345678 should change to 305419896 and then this code should output a binary of 00010010001101000101011001111000. Instead, I am getting the output of 00010000000000010000000000000000. If I could get help figuring out why my address is out of range and why my hex doesn’t seem to be changing to the proper decimal during the compile, that would be much appreciated. Thank you.

Update: I have changed the move $t2, $a0 to lw $t2, 0($a0). Now I a m able to get the correct binary representation. However, I am still receiving the address out of range error on my sb line.

Was it helpful?

Solution 2

I figured out I needed to add an add before the sb line. I used this add to add the current counter to a0 so I can use it as an index to save my 1s and 0s.

OTHER TIPS

Your address is out of range, because you are accessing the wrong register.

sb $a0, 0($a1) would store the byte (48) to memory buff.

Similarly the instruction move $t2, $a0 just copies the address of Input from one register to another, when you should access the memory. lb $t2, ($a0) to load the first byte of Input. lw to read a word and ld to read double word.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top