接收一个字符串,切割第一个字符,然后转换为 int

Taking in a String, cutting the first character, then converting to int

我正在处理的代码以 "X123"(不限于 3 个字符)的形式从用户那里获取一个字符串,其中 X 可以是任何非数字字符,123 可以是任何系列数字字符。然后代码去除非数字部分,将数字部分转换为 int,加上 5,并打印结果。

 .data
    msgerror: .asciiz "The string does not contain valid digits."
    input: .space 9 
    open: .asciiz "Enter a string:\n"
    close: .asciiz "The value +5 is:\n"

.text
.globl main

main:

    li $v0, 4
    la $a0, open
    syscall

    li $v0, 8      
    la $a0, input        #read a string into a0
    move $t0, $a0
    syscall

    li $t3,0
    li $t4,9
    la $t0, input        #address of string
    lbu $t1, 1($t0)        #Get first digit of actual number
    li $a1, 10           #Ascii of line feed
    li $a0, 0            #accumulator

    addi $t1,$t1,-48  #Convert from ASCII to digit
    move $a2, $t1         #$a2=$t1 goto checkdigit
    jal checkdigit
    add $a0, $a0, $t1      #Accumulates
    addi $t0, $t0, 1      #Advance string pointer 
    lbu $t1, ($t0)        #Get next digit

buc1:   
    beq $t1, $a1, print #if $t1=10(linefeed) then print
    addi $t1,$t1,-48  #Convert from ASCII to digit
    move $a2, $t1         #$a2=$t1 goto checkdigit
    jal checkdigit
    mul $t2, $a0, 10  #Multiply by 10
    add $a0, $t2, $t1      #Accumulates
    addi $t0, $t0, 1      #Advance string pointer 
    lbu $t1, ($t0)        #Get next digit 
    b buc1




print:  
    add $a0, $a0, 5
    li $v0, 1         
    syscall
    b end

checkdigit:
    blt $a2, $t3, error  
    bgt $a2, $t4, error
    jr $ra




error:
    la $a0, msgerror
    li $v0, 4            #print eror
    syscall

end:    
   li $v0, 10           #end program
   syscall

但是,我的代码最终生成:

Enter a string:
x123
The value +5 is:
1128

(预期为 128)。

如何消除其中一个重复号码?我已经尝试使用 print 语句将地址增加 1,但它似乎无法按预期使用任何东西 else/not。

li $v0,4
la $a0, aString
add $a0, $a0, 1
syscall

如果输入 123,上面的代码片段会生成 23,但我无法将其应用于上面的内容。

也欢迎更简单的整体方法。 mips 的新手,所以我几乎不认为我的有那么好。

看起来你读了第一个数字两次。

这部分:

li $t3,0
li $t4,9
la $t0, input          #address of string
lbu $t1, 1($t0)        #Get first digit of actual number

应改为:

li $t3,0
li $t4,9
la $t0, input+1       #address of first digit in string
lbu $t1, ($t0)        #Get first digit of actual number