9

我曾经以为我知道该怎么做。但后来我真的试着去做了。这是我编写的程序,但用于 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
4

2 回答 2

23

好的。从年初开始,我发现一个深埋在其他文件中的程序可以满足我的需求。我不能对所提供的建议发表评论,因为我不是经验丰富的 spim 或低级程序员。这里是:

         .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
             ###############################
于 2011-11-02T23:26:42.510 回答
10
# 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
于 2012-12-28T01:43:26.387 回答