Mips 腐烂加密

Mips rot encryption

所以我正在尝试在 MIPS 中编写 Rot47 算法

.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

这是我面临的第一个障碍“第 19 行(sb $t3,0($a0)): 0x00400020 处的运行时异常:地址超出范围 0x00000054

这到底是什么意思? 它应该是一个以零结尾的字符串来存储字符。

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, [=11=], $t3
    syscall

    li   $v0, 10
    syscall

我对我的代码做了一些注释,以使我的步骤更加清晰

存在一些错误。

这是您的代码的注释版本,显示了错误:

    .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,[=10=],$t3
    syscall

    li      $v0,10
    syscall

这是一个经过清理和修复的版本:

    .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