Substitution cipher: Difference between revisions

→‎{{header|Risc-V}}: RISC-V: Cleaned up the solution a bit
(add Risc-V solution)
(→‎{{header|Risc-V}}: RISC-V: Cleaned up the solution a bit)
Line 2,090:
<syntaxhighlight lang="risc-v">
# gnu assembler syntax
substitution_cipher: # char* str (a0), uint len (a1), const char lowerkey[26] (a2), const char upperkey[26] (a3)
lcipher: .ascii "zyxwvutsrqponmlkjihgfedcba"
# set up temporary registers t0, t1, t3t2, t4t3
ucipher: .ascii "ZYXWVUTSRQPONMLKJIHGFEDCBA"
substitution_cipher: # (a0: char* str) (a1: uint len)
# set up temporary registers t0, t1, t3, t4
li t0, 'a
li t1, 'z
li t3t2, 'A
li t4t3, 'Z
lui# t5,char tmp %hi(lciphert4)
lui# t6,char* cipher %hi(uciphert5)
 
.dcB: # begin loop
beqz a1, .dcE # break condition
lb t2t4, 0(a0) # load one character from a0
blt t2t4, t0, .dcU # lowercase check
bgt t2t4, t1, .dcAdcI
addi t2t4, t2t4, -'a
addi a5,mv t5, %lo(lcipher)a2
j .dcA
.dcU: # uppercase check
blt t2t4, t3t2, .dcI
bgt t2t4, t4t3, .dcI
addi t2t4, t2t4, -'A
addimv a5t5, t6, %lo(ucipher)a3
.dcA: # convert and save ciphertext character
add a5t5, a5t5, t2t4
lb a5t5, 0(a5t5)
sb a5t5, 0(a0)
.dcI: # increment registers
addi a1, a1, -1
addi a0, a0, 1
j .dcB
.dcE: ret # end loop
 
ret
# You can use the following cipher keys, which correspond to the Atbash cipher,
# to test the substitution. These keys are self-inverse, which means that
# applying them twice to a given plaintext yields the original plaintext again.
lcipherlatbash: .ascii "zyxwvutsrqponmlkjihgfedcba"
ucipheruatbash: .ascii "ZYXWVUTSRQPONMLKJIHGFEDCBA"
 
# For keys that are non-self-inverse, you will need to keep a separate set of
# encryption and decryption keys.
lzebras: .ascii "zebrascdfghijklmnopqtuvwxy"
uzebras: .ascii "ZEBRASCDFGHIJKLMNOPQTUVWXY"
ldzebras: .ascii "ecghbijklmnopqrstdfuvwxyza"
udzebras: .ascii "ECGHBIJKLMNOPQRSTDFUVWXYZA"
</syntaxhighlight>
 
10

edits