0
votes

$a0 - used for argument passing

$t0 - temporary register used for lw from $a0

Here is my code

.data
    Str:    .asciiz "please input words "
    input:  .space20

main:
    la $a0, Str   # asciiz loaded at $a0
    li $v0, 4     # print string command
    syscall       # execute print string

    la $a0, input # space for input
    li $a1, 20    # space for input
    li $v0, 8     # read string command
    syscall       # execute reading string

    jal nextStep

nextStep:
    lw $t0, 0($a0) # load first word from address $a0
    move $a0, $t0  # move $t0 -> $a0
    li $v0, 4      # print string command
    syscall        # execute print string

    jr $ra

after the execution, I get an error 'memory out of bound' at 'move $a0, $t0'.

I also tried with

and $a0, $t0, $a0

instead of move line from the above, but I still get the same error

What I want to see is the value of $t0 (from [lw $t0, 0($a0)]) printed using syscall.

For example, under the assumption the user always inputs more than a word long, my expectation is that when user inputs '1a2b3c4d5e6f', $t0 will contain '1a2b3c4d' (a word long), then it will only print these values and not the rest if exceeds.

Any help will be thankful.

1

1 Answers

0
votes

Are you sure you don't want to print the first word in the usual human sense? That is the part of the string up to the first space? In any case, the logic is the same: put a zero byte at the location you want to end the string, since the print string will go that far only.

Also note that you should not use jal to invoke nextStep, or if you must, then take proper care of the return address.