9

I used to think I knew how to do this. But then I actually tried to do it. Here's the program I wrote but the Berkeley S*** simulator for mac said there was a syntax error on the last line. What did I do wrong?

       .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
jason dancks
  • 1,152
  • 3
  • 9
  • 29
  • Does it work if you use `.asciiz ""` or similar instead? Have you tried other common directives instead of `.space` e.g. `.skip`, `.fill` or `.array`? What's the exact version you're using and error message you're getting? (Your code also has problems with the usage of `$t0`) – user786653 Nov 01 '11 at 17:06
  • Are you sure it isn't spelled `.asciz`? You probably also need `.align 2` before `buffer:` to ensure that the buffer starts on a word boundary. – markgz Nov 01 '11 at 18:11
  • MARS simulator assembles that with no errors. I strongly suggest MARS as a replacement for SPIM. – m0skit0 Nov 02 '11 at 13:03

2 Answers2

23

Ok. I found a program buried deep in other files from the beginning of the year that does what I want. I can't really comment on the suggestions offered because I'm not an experienced spim or low level programmer.Here it is:

         .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
             ###############################
jason dancks
  • 1,152
  • 3
  • 9
  • 29
11
# 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
Zeyi Fan
  • 2,213
  • 3
  • 17
  • 19
AceRam
  • 279
  • 3
  • 6
  • I'm not familiar with QtSpim, in fact I haven't coded in mips awhile. I'm a little taken aback that code written for one editor wouldn't work with another. Its spim. Its not even like there are different dialects. Whatever works is fine. Its nice to know the different methods. – jason dancks Jan 04 '13 at 15:03