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"▼
ucipher: .ascii "ZYXWVUTSRQPONMLKJIHGFEDCBA"▼
▲ # set up temporary registers t0, t1, t3, t4
li t0, 'a
li t1, 'z
li
li
.dcB: # begin loop
beqz a1, .dcE # break condition
lb
blt
bgt
addi
j .dcA
.dcU: # uppercase check
blt
bgt
addi
.dcA: # convert and save ciphertext character
add
lb
sb
.dcI: # increment registers
addi a1, a1, -1
addi a0, a0, 1
j .dcB
.dcE:
# 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.
# 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>
|