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

标签 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 个空格,以便所有内容都能正确显示,从而更易于人们阅读!)

希望这对您有所帮助!

关于mips - 在 MIPS 中询问时准确打印用户输入的数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67085719/

相关文章:

linux - libc_p.so 的源代码在哪里?

c - 宏观协议(protocol)中的错误、分歧

C函数参数神秘漂移?

assembly - 如何从 4 位十六进制转换为 7 位 ASCII?

c++ - 如何向 MIPS 中的函数添加 4 个以上的参数?

architecture - $ra 注册被调用者保存还是调用者保存在 mips 中?

generics - 如何用汇编语言表示泛型类型

mips - 如何将两个十六进制值相加?

perl - 为 MIPS 处理器交叉编译 Perl 或 Expect

linux - 如何反汇编原始 MIPS 代码?