【问题标题】:How to pass an input string from user to a MIPS program如何将输入字符串从用户传递到 MIPS 程序
【发布时间】:2018-12-08 02:04:00
【问题描述】:

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

我已经做到了这一点,我相信一切都应该有效,但我想知道如何实现这一点,以便需要一个实际的单词来测试。 当我在 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"

但我还没有完全得到它。

有什么建议吗?

【问题讨论】:

    标签: mips palindrome sql-server-mars


    【解决方案1】:

    喏,

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

    .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 个字节/字符]。阅读this link"System Calls and I/O"部分你会发现上面sn-p中使用的相同提示[在页面内搜索"Print out string (useful for prompts)"]

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

    在您的代码中,main 以读取字符串操作开始。但请注意,string_space 既用于分配缓冲区的大小,从 [in .data 部分] 中读取,也用于要求输入单词 [您正在尝试调用 string_space: .asciiz "Enter your word:\n"]。上面的sn-ps已经修复了这个问题

    如果出现问题,别忘了仔细检查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
    

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

    【讨论】:

    • 感谢您的回复!我想我在上面列出的代码中确实有 .data 和 .text 分隔。这是正确的路线吗?
    • @Jonathan 你是对的......我向下滚动代码,开始搜索提供的链接,当输入答案时,可见代码正好从 length_loop 开始......现在检查一下,很好运气!
    • 谢谢!毕竟,我遇到了另一种类型的错误:/Users/jonathanlin/Downloads/HW3Problem4.asm 第 19 行中的错误:0x00400024 处的运行时异常:地址超出范围 0x00000000 关于 lb $t3, ($t2)。你知道这个错误意味着什么吗?
    • @Jonathan 我粘贴了在 MARS 4.5 中工作的整个代码 [附有屏幕截图]。如果您有其他错误,请不要使用这个问题 [这是关于“我想知道如何实现它以便需要一个实际单词来测试”],但请打开一个新问题。祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多