0

我正在尝试解决编写汇编语言程序以检测用户输入的短语或字符是否为回文的问题。

我已经做到了这一点,我相信一切都应该正常工作,但我想知道如何实现这一点,以便需要一个实际的单词来测试。当我在 MARS 中运行时,根本没有输入选项。

.data
string_space: .space 1024
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"

.text
main: 
la $a0, string_space
li $a1, 1024
li $v0, 8
syscall

la $t1, string_space
la $t2, string_space

length_loop:
lb $t3, ($t2)
beqz $t3, end_length_loop
addu $t2, $t2, 1 
b length_loop

end_length_loop:
subu $t2, $t2, 2

test_loop:
bge $t1, $t2, is_palin

lb $t3, ($t1)
lb $t4, ($t2)
bne $t3, $t4, not_palin

addu $t1, $t1, 1
subu $t2, $t2, 1
b test_loop

is_palin: 
la $a0, is_palin_msg
li $v0, 4
syscall
b exit

not_palin:
la $a0, not_palin_msg
li $v0, 4
syscall
b exit

exit: 
li $v0, 10
syscall

我试过了

string_space: .asciiz "Enter your word:\n"

并且

string_space: .asciiz "racecar"

但我还没有完全得到它。

有小费吗?

4

1 回答 1

0

再见,

所以 - 在这里也将这个问题作为模板[强烈邀请您在发布之前检查类似的问题] - 您需要在代码中引入一个.data带有input字符串的部分,要求用户输入要检查的字符串

.data
string_space: .space 1024

input:  .asciiz "Enter a string: "
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"  
# other strings you may need

然后您可以开始设置逻辑以将它们推出

.text

main:
    li $v0, 4              # system call code for print_str
    la $a0, input          # address of string to print
    syscall                # print the input

    li $v0, 8              # code for syscall read_string
    la $a0, string_space   # tell syscall where the buffer is
    li $a1, 1024           # tell syscall how big the buffer is
    syscall    

    # double check the buffer content [see the next snippet]

# rest of the code to test      

您将开始询问用户一个字符串 [在这种情况下限制为 1024 个字节/字符]。阅读此链接"System Calls and I/O"的部分,您会发现上面的片段中使用的相同提示 [在页面内搜索]"Print out string (useful for prompts)"

同一节中的表格将向您解释li $v0, 4li $v0, 8说明的含义。这是另一个好读物。同一张表会让您明白,在调用打印字符串之前,您必须设置一个参数 [ $a0],而对于读取字符串操作,您需要两个 [$a0$a1]

在您的代码中,主要以读取字符串操作开始。但请注意,string_space它既用于分配从 [in .datasection] 读取的缓冲区大小,也用于要求输入单词 [you are trying to call string_space: .asciiz "Enter your word:\n"]。上面的代码片段已解决此问题

如果出现问题,请不要忘记仔细检查string_space缓冲区的内容:

la $a0, string_space  # move buffer into a0
li $v0, 4             # print buffer
syscall

在 MARS 4.5 中测试和工作的完整代码:

.data
string_space: .space 1024

input:  .asciiz "Enter a string: "
is_palin_msg: .asciiz "The string is a palindrome.\n"
not_palin_msg: .asciiz "The string is not a palindrome.\n"

.text
main: 

li $v0, 4              # system call code for print_str
la $a0, input          # address of string to print
syscall                # print the input

la $a0, string_space
li $a1, 1024
li $v0, 8
syscall

#la $a0, string_space  # move buffer into a0
#li $v0, 4             # print buffer
#syscall

la $t1, string_space
la $t2, string_space

length_loop:
lb $t3, ($t2)
beqz $t3, end_length_loop
addu $t2, $t2, 1 
b length_loop

end_length_loop:
subu $t2, $t2, 2

test_loop:
bge $t1, $t2, is_palin

lb $t3, ($t1)
lb $t4, ($t2)
bne $t3, $t4, not_palin

addu $t1, $t1, 1
subu $t2, $t2, 1
b test_loop

is_palin: 
la $a0, is_palin_msg
li $v0, 4
syscall
b exit

not_palin:
la $a0, not_palin_msg
li $v0, 4
syscall
b exit

exit: 
li $v0, 10
syscall

MARS 4.5 输出

重要提示:此代码逻辑存在错误。如果您输入长度为奇数的回文,则会给出错误的结果[例如,civic 被检测为非回文,但实际上是]

于 2018-12-08T03:25:42.100 回答