凯撒密码在 MIPS 中加密单个字符
Caesar cipher encrypting a single character in MIPS
我在创建加密消息的程序时遇到了一些问题。此时我只是想输入一个字符,输出应该是字母表中的字符+5个位置。
因此程序应该读取 ASCII 中的字符并将其加 5,然后打印字母。例如:
Input: A
Output: F
它应该只适用于大写字母,所以每个字符都应该 >=65 且 <=90。所以,如果我写 'Z' 它应该从头开始字母表并打印 'E'.
到目前为止,我的代码如下所示:
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,代码应该是这样的:
.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
我在创建加密消息的程序时遇到了一些问题。此时我只是想输入一个字符,输出应该是字母表中的字符+5个位置。
因此程序应该读取 ASCII 中的字符并将其加 5,然后打印字母。例如:
Input: A
Output: F
它应该只适用于大写字母,所以每个字符都应该 >=65 且 <=90。所以,如果我写 'Z' 它应该从头开始字母表并打印 'E'.
到目前为止,我的代码如下所示:
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,代码应该是这样的:
.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