Mips rot encryption

1

So i'm trying to program the Rot47 algorithm in MIPS

.data
    message: .asciiz "This text should probably contain something useful!"
   message_size:.word   51
.text

main:
    li $t1, 0
    la $t0, message


loop:
    lb   $a0, 0($t0) #load the first ascii-char of the string 
    beqz $a0, done   
    addi $t0, $t0,1
    addi $t1, $t1,1
    j     rot47

rot47:
    sb $t3, 0($a0)    #store the first ascii-char into $t3
    ble $t3, 79, do   #$t3 <= 79 do $t3 + 47
    sub $t3, $t3, 47  #else $t3 - 47
    j next

here is where i face my first hurdle "line 19(sb $t3, 0($a0)): Runtime exception at 0x00400020: address out of range 0x00000054"

what exactly does that mean? its supposed to be a zero terminated string to store characters.

do: 
    addi $t3, $t3, 47
    j next
next:
    addi $a0, $a0, 1   #increment $a0 for the next byte 
    j loop
done:                 #print the completed string

    li   $v0, 4        
    add  $a0, $0, $t3
    syscall

    li   $v0, 10
    syscall

I commented my code a little to make my steps a little bit clearer

assembly
mips
mars-simulator
asked on Stack Overflow Jan 12, 2019 by SupEldrix • edited Apr 2, 2021 by Peter Cordes

1 Answer

1

There are a number of bugs.

Here is an annotated version of your code, showing the bugs:

    .data
message:    .asciiz     "This text should probably contain something useful!"
# NOTE/BUG: this isn't needed since you scan for the 0 byte at the end of
# message
message_size:   .word   51
    .text

# NOTE/BUG: a0 is never initialized -- it should point to message_size
main:
# NOTE/BUG: setting t1 isn't needed
    li      $t1,0
    la      $t0,message

loop:
# NOTE/BUG: using a0 to fetch the byte destroys the pointer -- use a different
# register (it should be something else)
    lb      $a0,0($t0)              # load the first ascii-char of the string
    beqz    $a0,done

# NOTE/BUG: these addi insts don't do much
    addi    $t0,$t0,1
    addi    $t1,$t1,1
# NOTE/BUG: jump is not needed -- just fall through
    j       rot47

rot47:
# NOTE/BUG: this store should be done _after_ rotation (i.e. at next:)
    sb      $t3,0($a0)              # store the first ascii-char into $t3
    ble     $t3,79,do               # $t3 <= 79 do $t3 + 47
    sub     $t3,$t3,47              # else $t3 - 47
    j       next

do:
    addi    $t3,$t3,47
    j       next

next:
    addi    $a0,$a0,1               # increment $a0 for the next byte
    j       loop

    # print the completed string
done:

# NOTE/BUG: t3 now points to end of string [or worse]
    li      $v0,4
    add     $a0,$0,$t3
    syscall

    li      $v0,10
    syscall

Here is a cleaned up and fixed version:

    .data
message:    .asciiz     "This text should probably contain something useful!"
output:     .space  1000
    .text

main:
    la      $t0,message             # point to input buffer
    la      $a0,output              # point to output buffer

loop:
    lb      $t3,0($t0)              # load the next ascii-char of the string
    beqz    $t3,done                # at end? if yes, fly

    ble     $t3,79,do               # $t3 <= 79 do $t3 + 47
    sub     $t3,$t3,47              # else $t3 - 47
    j       next

do:
    addi    $t3,$t3,47
    j       next

next:
    sb      $t3,0($a0)              # store the current char into output
    addi    $t0,$t0,1               # increment input pointer for the next byte
    addi    $a0,$a0,1               # increment output pointer for the next byte
    j       loop

    # print the completed string
done:
    sb      $zero,0($a0)
    li      $v0,4
    la      $a0,output
    syscall

    li      $v0,10
    syscall
answered on Stack Overflow Jan 12, 2019 by Craig Estey • edited Jan 12, 2019 by Craig Estey

User contributions licensed under CC BY-SA 3.0