dave5678
dave5678

Reputation: 20

arrays and counters in mips

CASE2:
    la $t9, ARRAY       # Load base address of array into $t9
    li $t8, 36      # Initialize loop counter to 36

LOOP:   
    la $a0, Prompt2     # Load Prompt2 string into $a0.
    li $v0, 4       # system call to print string Prompt2.
    syscall

    li $v0, 12      # read character input. 
    syscall
    move $t8($t9), $v0  # move input character to an array element.

    la $a0, $t8($t9)    # load array element into $a0.
    li $v0, 11      # system call to print array element.
    syscall
    addi $t8, $t8, -4   # decrement loop counter.
    bgtz $t8, LOOP

    la $a0, MSG2        # load MSG2 string into $a0.
    li $v0, 4       # system call to print MSG2 string.
    syscall 
LOOP2:
    la $a0, $t8($t9)    # load element of array into $a0.
    li $v0, 11      # system call to print char.
    addi $t8, $t8, 4    # increment $t8.
    blt $t8, 36, LOOP2  # branch if $t8 is less than 36
    j EXIT          # when $t8 reaches 36 jump to EXIT.
    .data
Prompt2:.asciiz "\nEnter a character: "
ARRAY:  .space 10       # 10 bytes of storage to hold an array of 10 characters

I am having trouble getting this array to work, suppose to read 10 characters from input and print them right after reading them and afterwards print out the array backwards. Any help would be appreciated.

Upvotes: 0

Views: 5383

Answers (1)

user1282140
user1282140

Reputation:

One mistake is immediately apparent:

move $t8($t9), $v0 

is not in the correct format. MIPS does not allow you to use a register as an offset. Nor does MIPS allow an offset on the target register of a move operation.

The code should be replaced by something like:

addi $t8, $0, 36 # initialize the offset counter to 36
...
sll  $t8, $t8, 2 # multiply the offset counter by 4 to account for word indexing
add  $t9, $t8, $t9 # add the current offset to $t9
sw   $v0, 0($t9)   # store $v0 at the memory location held in $t9

The move operation is a pseudo-instruction that takes one register and puts the value contained within into another target register. Instead, use the store word (sw) instruction to store the contents of a register at a supplied memory location. You will have to change the rest of your code to work with the code above, but it should be enough to start you in the right direction.

Upvotes: 2

Related Questions