ошибка второй и третьей буквы в реализации шифра vigenere в mips - PullRequest
0 голосов
/ 14 апреля 2020

Я пытаюсь сделать Vigenere шифр в мипс. Я сталкиваюсь с постоянной ошибкой на втором и третьем символах. Позже, однако, вводятся цифры, пробел, маленькие и заглавные буквы, которые они печатают правильно. Я увеличил счетное число до 4 при получении ввода, так что целые числа также могут быть заданы. Поскольку ключ состоит только из букв, он увеличивается на 1

.data
msg: .asciiz "\nenter 0 for encryption and 1 for decryption\n"
msg1: .asciiz "enter the plain text\n"
msg2: .asciiz "enter the key\n"
msg3: .asciiz "enter the cipher text\n"
input: .word 100
key: .space 100

.text
# print msg
li $v0,4
la $a0,msg
syscall

#accept user input
li $v0, 5
syscall 
move $a0, $v0

bne $a0, $zero, decryption

#get text
li $v0, 4
la $a0, msg1
syscall

for1:
    li $v0, 12
    syscall
    beq $v0, 10, next
    sb $v0, input($t0)
    addi $t0, $t0, 4
    j for1

next:
li $v0, 4
la $a0, msg2
syscall
j for2
#la $t0,($a0)       #t0 plaintext
#li $t1,0       #t1 length of string

for2:
    li $v0, 12
    syscall
    beq $v0, 10, encrypt
    sb $v0, key($t2)
    addi $t2, $t2, 1
    j for2

encrypt:
    bge $t1, $t0, end
    lb $t4, input($t1)
    bge $t3, $t2, z
    lb $t5, key($t3)
    j encrypt2

z:
la $t3,0
j encrypt

encrypt2:
    blt $t4, 65, spl1
    ble $t4, 90, encryptupper
    j encryptlower  

encryptupper:
    li $t7,26
    subi $t4, $t4, 65   #ptext is sub
    bge $t5, 97, smallkeyeupper #check key is small
    subi $t5, $t5, 65   # caps key is sub
    continue2:
    add $t4, $t4, $t5   #key is added to ptext
    div $t4, $t7
    mfhi $a0
    addi $a0, $a0, 65
    j printCipher

smallkeyeupper:
    subi $t5, $t5, 97
    j continue2

encryptlower:
    li $t7,26
    subi $t4, $t4, 97   #ptext is sub
    bge $t5, 97, smallkeyelower #check key is small
    subi $t5, $t5, 65   # caps key is sub
    continue1:
    add $t4, $t4, $t5   #key is added to ptext
    div $t4, $t7
    mfhi $a0
    addi $a0, $a0, 97
    j printCipher

smallkeyelower:
    subi $t5, $t5, 97
    j continue1

spl1:
    bne $t4, 32, printnum1
    li $a0, 32
    li $v0, 11
    syscall
    addi $t1, $t1, 4
    j encrypt

printnum1:
    subi $a0, $t4, 48
    li $v0, 1
    syscall
    addi $t1, $t1, 4        #goes to next ciphertext
    j encrypt

printCipher:
    li $v0, 11
    syscall
    #add $t0, $t0, 1        #points to next char of plaintext
    addi $t1, $t1, 4        #increments the length of the plaintext
    addi $t3, $t3, 1        #increments the length of the key
    j encrypt

decryption:

#get text
li $v0, 4
la $a0, msg3
syscall

for3:
    li $v0, 12
    syscall
    beq $v0, 10, next1
    sb $v0, input($t0)
    addi $t0, $t0, 4
    j for3

next1:li $v0, 4
la $a0, msg2
syscall
j for4
#la $t0,($a0)       #t0 plaintext
#li $t1,0       #t1 length of string

for4:
    li $v0, 12
    syscall
    beq $v0, 10, decrypt
    sb $v0, key($t2)
    addi $t2, $t2, 1
    j for4

decrypt:
    bge $t1, $t0, end
    lb $t4, input($t1)
    bge $t3, $t2, y
    lb $t5, key($t3)
    j decrypt2

y:
la $t3,0
j decrypt

decrypt2:
    blt $t4, 65, spl2
    ble $t4, 90, decryptupper
    j decryptlower  


decryptupper:
    li $t7, 26
    subi $t4, $t4, 65   #ciphertext is sub
    bge $t5, 97, smallkeydupper #check key is small
    subi $t5, $t5, 65   #caps key is sub
    cont2:
    add $t4, $t4, $t7   #add 26 
    sub $t4, $t4, $t5   #cipher-key
    div $t4, $t7        #div to get modulo
    mfhi $a0        #modulo
    addi $a0, $a0, 65
    j printPlain

smallkeydupper:
    subi $t5, $t5, 97
    j cont2

decryptlower:
    li $t7,26
    subi $t4, $t4, 97   #ciphertext is sub
    bge $t5, 97, smallkeydlower #check key is small
    subi $t5, $t5, 65   #caps key is sub
    cont1:
    add $t4, $t4, $t7   #add 26
    sub $t4, $t4, $t5   #ciphertext-key
    div $t4, $t7        #div to get modulo
    mfhi $a0        #modulo
    addi $a0, $a0, 97
    j printPlain

smallkeydlower:
    subi $t5, $t5, 97
    j cont1

spl2:
    bne $t4, 32, printnum
    li $a0, 32
    li $v0, 11
    syscall
    addi $t1, $t1, 4
    j decrypt

printnum:
    subi $a0, $t4, 48
    li $v0, 1
    syscall
    addi $t1, $t1, 4        #goes to next ciphertext
    j decrypt

printPlain:
    li $v0, 11
    syscall
    #add $t0, $t0, 1    #points to next char of plaintext
    addi $t1, $t1, 4        #increments the length of the plaintext
    addi $t3, $t3, 1        #increments the length of the key
    j decrypt

end:
li $v0, 10
syscall

Пожалуйста, помогите мне с этим

enter 0 for encryption and 1 for decryption
0
enter the plain text
hello
enter the key
world
dkuwr

Вместо 'dscwr'

...