Mips如何存储用户输入字符串

时间:2011-11-01 16:25:14

标签: string mips spim

我曾经认为我知道如何做到这一点。但后来我真的试着去做。这是我写的程序但是Mac的Berkeley S ***模拟器说最后一行有语法错误。我做错了什么?

       .text
       .globl __start
    __start:
        la $a0,ask
        li $v0,4
        syscall

        li $v0,8
        syscall

        la $t0,buffer
        move $t0,$v0
        syscall

        la $a0,ret
        li $v0,4
        syscall

        move $a0,$t0
        li $v0,4
        syscall

        .data
      ask:  .asciiz "Enter string: "
      ret:  .asciiz "You wrote: "
      buffer:   .space 100

2 个答案:

答案 0 :(得分:16)

确定。我发现一个程序从今年年初开始深埋在其他文件中,可以满足我的需要。我无法评论所提出的建议,因为我不是一个经验丰富的垃圾邮件或低级程序员。这就是:

         .text
         .globl __start
    __start:
         la $a0,str1 #Load and print string asking for string
         li $v0,4
         syscall

         li $v0,8 #take in input
         la $a0, buffer #load byte space into address
         li $a1, 20 # allot the byte space for string
         move $t0,$a0 #save string to t0
         syscall

         la $a0,str2 #load and print "you wrote" string
         li $v0,4
         syscall

         la $a0, buffer #reload byte space to primary address
         move $a0,$t0 # primary address = t0 address (load pointer)
         li $v0,4 # print string
         syscall

         li $v0,10 #end program
         syscall


               .data
             buffer: .space 20
             str1:  .asciiz "Enter string(max 20 chars): "
             str2:  .asciiz "You wrote:\n"
             ###############################
             #Output:
             #Enter string(max 20 chars): qwerty 123
             #You wrote:
             #qwerty 123
             #Enter string(max 20 chars):   new world oreddeYou wrote:
             #  new world oredde //lol special character
             ###############################

答案 1 :(得分:7)

# This code works fine in QtSpim simulator

.data
    buffer: .space 20
    str1:  .asciiz "Enter string"
    str2:  .asciiz "You wrote:\n"

.text

main:
    la $a0, str1    # Load and print string asking for string
    li $v0, 4
    syscall

    li $v0, 8       # take in input

    la $a0, buffer  # load byte space into address
    li $a1, 20      # allot the byte space for string

    move $t0, $a0   # save string to t0
    syscall

    la $a0, str2    # load and print "you wrote" string
    li $v0, 4
    syscall

    la $a0, buffer  # reload byte space to primary address
    move $a0, $t0   # primary address = t0 address (load pointer)
    li $v0, 4       # print string
    syscall

    li $v0, 10      # end program
    syscall