凯撒密码在MIPS中加密单个字符 [英] Caesar cipher encrypting a single character in MIPS
问题描述
在创建用于加密消息的程序时遇到一些问题.此时,我只是想输入一个字符,而输出应该是字母中的char + 5位置.
I'm having some problems in creating a program to encrypt a message. At this point i'm just trying to input a char and the output should be the char+5 positions in the alphabet.
因此,程序应读取ASCII字符,并将其添加5,然后打印字母.例如:
So the program should read the char in ASCII and add 5 to it and then print the letter. Ex.:
Input: A
Output: F
它仅适用于大写字母,因此每个字符应为> = 65和< = 90.因此,如果我写"Z",则应从头开始输入字母,并打印"E".
It should only work for Capital letters, so every char should be >=65 and <=90. So, if I write 'Z' it should start the alphabet from the beginning and print 'E'.
到目前为止,我的代码如下:
So far, my code looks like this:
li $v0, 8 #read_string
syscall #adresses char at $v0
li $v0, 5 #char ASCII (I GUESS IT SHOULD)
move $t0, $v0 #moves char(ASCII) to $t0
li $s0, 90
li $s1, 65
bgt $t0, $s0, le_string # checks if char(ASCII) > 90
ble $t0, $s1, le_string # checks if char(ASCII) < 65
li $s0, 5
add $t1, $t1, $s0 #char=char+5
move $t2, $t1 #moves encrypted char to $t2
li $v0, 4 #print_string
move $a0, $t2
syscall
实际输出的日志:
25/DEZ/2015:
INPUT: A
OUTPUT: A
推荐答案
如果您使用的是MIPS,则代码应如下所示:
If you are using MIPS, the code should be like this:
.text
main:
li $v0, 8 # read_string
la $a0, textbuf # adresses of char at $a0
li $a1, 2 # length to read at $a1
syscall
la $t0, textbuf
lb $v0, 0($t0) # char ASCII (I GUESS IT SHOULD)
move $t0, $v0 # moves char(ASCII) to $t0
li $s0, 90
li $s1, 65
bgt $t0, $s0, le_string # checks if char(ASCII) > 90
nop # avoid instruction after branch begin executed even if jump is taken
blt $t0, $s1, le_string # checks if char(ASCII) < 65
nop # avoid instruction after branch begin executed even if jump is taken
li $s2, 5
li $s3, 26
add $t0, $t0, $s2 # char=char+5
ble $t0, $s0, nowrap_char # checks if encrypted char <= 90
nop # avoid instruction after branch begin executed even if jump is taken
sub $t0, $t0, $s3 # wrap the char
nowrap_char:
le_string:
move $t2, $t0 # moves encrypted char to $t2
la $t0, textbuf
li $v0, 4 # print_string
sb $t2, 0($t0) # put the encrypted char
la $a0, textbuf
syscall
li $v0, 10 # exit
syscall
.data
textbuf:
.space 2
这篇关于凯撒密码在MIPS中加密单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!