用户输入字符串的结束字符

End character of user input string

我正在编写一个接受罗马数字(最多 12 个字符)并将其转换为十进制值的程序。我能够成功地进行这种转换并逐个字符地读取每个值,但是当输入的字符串不是 12 个字符时,我的值总是比实际值大 1000,我确定这是由于评估子例程的额外循环(如果没有匹配,则进入转换M = 1000的过程。

我认为这与我在少于 12 个字符时读取用户字符串的方式有关。据我了解,读取字符串系统调用代码一直进行到到达 '\n' 字符,并将该字符及其其余空 space 转换为 0 个字符,因此检查下一个字符是否不相等到 0 应该可以工作,因为我不希望 0 出现在用户的输入中。我确实意识到,如果在 "evaluate" 期间没有匹配,我可以通过跳转到 tailLoop 来修复我的代码,但我想了解为什么我目前拥有的代码无法正常工作。

以下代码不包含我的程序的大部分内容(完整 post 会很长),仅足以理解我正在尝试使用的逻辑过程。这是家庭作业,以防它影响您想如何回答问题。

# Program to translate Roman Numerals to decimal values

.data
    numeralString:
        .space 13
# End Strings

.text   # Begin Program
.globl main

main:
    li $v0, 8 # read string from user at next syscall
    la $a0, numeralString
    li $a1, 13
    syscall

    la $t0, numeralString # take input string and store in $t0
    move $t1, $t0 # creating a copy of base register
    lb $t2, 0($t0) # loads first byte (character) into $t2

    jal evaluate # calls evaluate subroutine.  String passed via $t0 (first byte/character specified in $t2)
             # interger value returned via $v1

    li $v0, 1 # print result value from $v1 at next syscall
    move $a0, $v1
    syscall

exit:   # Exit Program in next syscall

    li $v0, 10 # exit program in next syscall
    syscall

evaluate: # matches numeral in string to correct subroutine
    lb $t3, 1($t0)
    beq $t2, 'M', mChar

mChar:
    addi $v1, $v1, 1000
    # beq $t3, 'C', cSlot
    j tailLoop

tailLoop:
    move $t2, $t3 # $t2 now holds next character of string
    addi $t0, $t0, 1
    bnez $t2, evaluate # Go back to evaluate if next character exists
    jr $ra

It is my understanding that the read string system call code proceeds until it reaches a '\n' character, and converts that character and the rest of its empty space to 0 characters

没有

如果您查看 spim source code,您会看到在系统调用

时调用的 read_input 函数

Simulate[s] the semantics of fgets (not gets) on Unix file.

According to CPlusPlus.com:

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

通过检查 spim 代码可以确认这一点。