So I am trying to reverse a string inputed by a user and I am getting an error on one line of my code the line is lw $t3, $t0($t1) #load value. Why can I not do this?

.data
prompt: .asciiz "Enter in a string: "
backwards: .asciiz "That string backwards is: "
newLine: .asciiz "\n"
.text
main:
    la  $a0, prompt #call opening prompt
    li  $v0, 4      #output string
    syscall
    li  $v0, 8      #read a string
    syscall
    la  $a0, newLine    #call opening prompt
    li  $v0, 4      #output string
    syscall
    #$a0 is the string $a1 is the length of the string
    move    $t0, $a0    #move a0 to t0
    move    $t1, $a1    #move a1 to t1
    li  $v0, 11     #code to print char
Loop:
    sub $t1, $t1, 1
    lw  $t3, $t0($t1)   #load value
    lb  $a0, ($t3)  #load the byte
    syscall         #print byte
    bnez    $a1, Loop


    li      $v0, 10              #terminating
    syscall 

Dani AI

Generated

A few things are tripping you up beyond the invalid base+index form. With syscall 8 (read_string) you must pass a writable buffer in $a0 and the buffer size in $a1. In your code, $a0 still points at the prompt, so the read tries to write into static string data. Also, $a1 is not the length the user typed; it is the buffer capacity you supplied. To reverse the input, scan for the terminating byte, back up past the newline if present, then walk backward loading bytes (lb), not words (lw). Word loads require alignment and fetch four bytes, which is not what you want for characters.

Here is a compact pattern that works in SPIM/MARS:

.data
prompt: .asciiz "Enter a string: "
msg:    .asciiz "\nThat string backwards is: "
buf:    .space 256
nl:     .asciiz "\n"

.text
main:
    li   $v0,4        ; print prompt
    la   $a0,prompt
    syscall

    la   $a0,buf      ; read string into buf
    li   $a1,256
    li   $v0,8
    syscall

    move $t0,$a0      ; base = buf, find end
    move $t1,$a0
find_end:
    lb   $t2,0($t1)
    beqz $t2,got_end
    addiu $t1,$t1,1
    b    find_end
got_end:
    addiu $t1,$t1,-1  ; back up before NUL
    lb   $t2,0($t1)   ; skip trailing newline if present
    bne  $t2,10,print_hdr
    addiu $t1,$t1,-1

print_hdr:
    li   $v0,4
    la   $a0,msg
    syscall

    li   $v0,11       ; print chars backward
rev_loop:
    blt  $t1,$t0,done
    lb   $a0,0($t1)
    syscall
    addiu $t1,$t1,-1
    b    rev_loop

done:
    li   $v0,4
    la   $a0,nl
    syscall
    li   $v0,10
    syscall

Because MIPS is a RISC and there is no such instruction.

LW -- Load word

Description:
A word is loaded into a register from the specified address.
Operation:
$t = MEM[$s + offset]; advance_pc (4);
Syntax:
lw $t, offset($s)
Encoding:
1000 11ss ssst tttt iiii iiii iiii iiii

I guess the 'i's here are immediate constant bits.

You just have to do the adding first and use the result as indirect.
add $t4, $t0, $t1
lw $t3 ($t4)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.