在 MIPS 中询问时准确打印用户输入的数字字符串

Print String exactly the number input by the user when asked in MIPS

你好我想让 hello world 输出用户输入的次数,比如如果他们写了 3,那么 hello world 就会被打印 3 次。我该怎么做呢? 这是我目前正在使用的:

.data
    n:      .space 4
    msg:    .asciiz "Hello World"
    prom1:  .asciiz "How many Hello World want to be printed: "
    mychar1:.byte 'a'
    out_string:   .asciiz "\nHello World\n"

.text
    main:   li $v0, 4
            la $a0, msg
            syscall

            li $v0, 4     # print str
            la $a0, nl    # at nl
            syscall

            li $v0, 4     # print str
            la $a0, prom1  # at prom1
            syscall

            li $v0, 5     # read int
            syscall
            sw $v0, n     # store the user input in n

            li $v0, 4     # print str
            lw $t0, n
            mul $t0, $a0, 1
            la $a0, out_string   # at out_string
            syscall

这是一个更简单的解决方案。我不保存用户输入,而是将其保存到另一个寄存器。您可以将该值保存到一个变量中,然后再次获取它,但这样做没有任何意义。

.data
    prom1: .asciiz "How many Hello World want to be printed: " 
    out_string: .asciiz "\nHello World\n"

.text

main:   
    
        li $v0, 4           
        la $a0, prom1       # Load address of first prompt
        syscall 

        li $v0, 5           # Read int from user
        syscall
        
        li $t1, 0       # Load 0 into $t1 for comparison
        move $t0, $v0       # Move the user input to $t0
loop:
        beq  $t1, $t0, end  # Break If Equal: branch to 'end' when $t1 == $t2
        li $v0, 4       
        la $a0, out_string  # Load address of output string
        syscall
        add $t1, $t1, 1     # Increment $t1
        j loop          # Jump back up to loop
        
end:
        li $v0, 10      # Load syscall 10 to indicate end of program
        syscall 

(也是为了以后参考,你的代码需要再缩进4个空格,这样才能正确显示,更容易让人阅读!)

希望对您有所帮助!