Question

I'm having trouble understanding why I am getting syntax error in line 20, sw $v0, $t0. $v0 should be the integer returned from the previous call to read int, and $t0 is a temporary register. Thanks!

.data
msg:    .asciiz "Hello world.\n"
promptint:  .asciiz "Type an int: "
promptstring:   .asciiz "Type a string: "


.text

    main:
li $v0, 4       #print "Hello world."
la $a0, msg
syscall

la $a0, promptint   #prompt for int
syscall

li $v0, 5       #read int
syscall

sw $v0, $t0

li $v0, 1       #print int
la $a0, $t0
syscall

li $v0, 4
la $a0, promptstring    #prompt for string
syscall

li $v0, 8       #read string and length of string buffer
syscall
sw $a0, $t1     #string
sw $a1, $t2     #length

li $v0, 4       #print string
la $a0, $t1
syscall

    li $v0, 1
la $a0, $t2     #print length
syscall

j done

    done:
li $v0, 10
syscall
Was it helpful?

Solution

If you had consulted an instruction set reference, you would have seen that sw needs a memory operand. Thus, from a purely syntactical standpoint sw $v0, ($t0) would be correct, but it wouldn't do what you want, which is simply transferring between registers. That can be done with the move pseudoinstruction as follows: move $t0, $v0. This will probably be translated by the assembler to addu $t0, $v0, $0 which you can of course write out yourself if you are so inclined. Similarly, to transfer it into $a0 you should use another move and not la.

You are also using the read_string system call wrong. For one thing, you should load the arguments before you perform the syscall, which should be an address of a buffer in $a0 and its length in $a1. If you need to print the length of the string entered, you will need to calculate it yourself, by counting the bytes until the terminating zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top