求助:MIPS汇编中预定义字符串与用户输入字符串的匹配实现
MIPS控制台命令解析:字符串比较失效问题
我需要实现一个可接收命令的控制台程序,但无法通过字符串比较正确解析输入的命令。以下是我当前的MIPS代码,以及我期望实现功能的Java版本示例。我试过网上的各种方法,但都没用——那些方法针对的都是非用户输入的字符串对比场景,不适用于我的情况。
当前MIPS代码
.data cmdline: .asciiz "> " helpline: .asciiz "> Commands:\n> help - displays available commands\n> echo - returns text inputted after command\n> exit - exits program\n>\n" helpcomp: .asciiz "help" echocomp: .asciiz "echo" exitcomp: .asciiz "exit" noRecog: .asciiz "> Command not recognized\n> Input 'help' for a list of commands\n>\n" tempText: .asciiz "echo found\n" buffer: .space 21 buffersize: .word 20 .text j cmdLoop cmdLoop: li $v0, 4 la $a0, cmdline syscall la $s2, buffer move $t2, $s2 syscall move $a0,$t2 la $a1, buffersize li $v0, 8 syscall j help help: la $s3, helpcomp move $t2, $s3 compareHelp: lb $t2,($s2) # get next char from str1 lb $t3,($s3) # get next char from str2 bne $t2,$t3,compareExit # are they different? if yes, fly beq $t2,$zero,helpExecute # at EOS? yes, fly (strings equal) addi $s2,$s2,1 # point to next char addi $s3,$s3,1 # point to next char j compareHelp helpExecute: li $v0, 4 la $a0, helpline syscall li $v0, 10 syscall compareExit: li $v0, 4 la $a0, tempText syscall li $v0, 10 syscall
期望实现的Java版本
public class Console { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("> "); while(true) { String command = input.nextLine(); if(command.contains("help")) { System.out.print("Commands:" + "\nhelp - displays available commands" + "\necho - prints the text inputted right after echo" + "\nexit - quits out of the program" + "\n> "); } else if (command.contains("echo")) { String echo = command.substring(5, command.length()); System.out.println(echo); System.out.print("> "); } else if (command.contains("exit")) { break; } } input.close(); } }
内容的提问来源于stack exchange,提问作者Skyler Van Kleeck
相关产品推荐
相关产品推荐

