嘿伙计们,我想在MIPS中写一个简单的Caesar Shift。我的程序接受要加密/解密的字符串。然后程序要求输入密钥(要移位的字母数),然后要求编码或解码。
--- 编辑 ---
抱歉,我忘了澄清我的问题。我的程序的输出是“estuvwL”,在字符串“abcde”上移位1。显然,这是不正确的。关于我哪里出错的任何想法?
这是我的代码:
.data # Data declaration section
prompt: .asciiz "Enter a message: " #prompt to enter message
key_prompt: .asciiz "Enter a key for this message: " # enter number to shift letters
enc_dec: .asciiz "(e)ncrypt or (d)ecrypt? " # encrypt or decrypt prompt
encode_bytes: .byte 1 #bytes for encoding
key: .word 0 # the variable for encoding key
String: .space 255 # the string to be encrypted/decrypted
encode: .space 1 #the character (e) or (d) to ask encrypt or decrypt
e: .ascii "e"
d: .ascii "d"
.text
main: # Start of code section
__Start:
li $v0, 4 #print string
la $a0, prompt # print the first prompt
syscall
li $v0, 8 # read in string to be encrpyted
la $a0, String # stores string in variable
li $a1, 255 # the string allocates room for up to 255 chars
syscall
li $v0, 4 #print string
la $a0, key_prompt
syscall
li $v0, 5 #code to read in int
syscall # reading in the integer
move $t5, $a0 #moving key to $t5
li $v0, 4 #print string
la $a0, enc_dec #print the encode string
syscall
li $v0, 12 #read character code
#la $a0, encode #load address of encode to register $a0
#li $a1, 2 #limit input to 1 char
syscall
li $s7, 101
li $s6, 100
beq $v0, $s7, __encrypt_Loop
beq $v0, $s6, __decrypt_Loop
__encrypt_Loop:
la $t0, String #holding address of String
__encode_Loop:
lb $t2, ($t0) #loading the byte to be manipulated into $t2
beqz $t2, __print_String # if string is equal to zero, jump to print new string
__continue:
add $t2, $t2, $t5 #adding the key to the string (manipulating chars)
li $t3, 'z' #loading the value of z, for checking purposes
bgt $t3, $t2, __encrypt_Check #if the char is "bigger" than z jump to fix
sb $t2, ($t0) #store the value
addi $t0, 1 #incrementing to next char
j __encode_Loop
__encrypt_Check:
li $s4, 25
sub $t2, $t2, $s4
sb $t2, ($t0) #store char in string
addi $t0, 1 #incrementing to next char
j __continue
__decrypt_Loop:
la $t0, String #holding address of String
# la $t5, key #loading the key into $t5
__decode_Loop:
lb $t2, ($t0) #making $t2 the address of the string
beqz $t2, __print_String # if string is equal to zero, jump to print new string
___continue:
sub $t2, $t2, $t5 #subtracting the key to the string (manipulating chars)
li $t3, 'a' #loading the value of z, for checking purposes
blt $t3, $t2, __decode_Check #if the char is "smaller" than a jump to fix
sb $t2, ($t0) #store the value
addi $t0, 1 #incfementing to next char
j __decode_Loop
__decode_Check:
li $s4, 25
add $t2, $t2, $s4
sb $t2, ($t0) #store char in string
addi $t0, 1 #incrementing to next char
j __continue
__print_String:
li $v0, 4
#move $a0, $t0
la $a0, String
syscall
__end_program:
li $v0, 10
syscall
答案 0 :(得分:1)
这里有一些问题:
首先是行move $t5, $a0 #moving key to $t5
,因为系统调用返回move $t5, $v0
而不是$v0
中的键,所以应该读取$a0
。
其次,我不确定“检查”功能的用途是什么,所以我将分支注释掉给他们,一切都按预期工作。
无论如何,检查它们显然是错误的。你写道:
blt $t3, $t2, __decode_Check #if the char is "smaller" than a jump to fix
但这些论点与评论相反。它应该读
blt $t2, $t3, __decode_Check #if the char is "smaller" than a jump to fix
同样适用于__encrypt_Check
功能。