Hello,

I am trying to write a program that reverses an array with integers. Here is what I have so far:

driver.asm

# Test driver for reverser.asm
#
          .data
tsize:    .word  10
tdata:    .word  2, 3, 5, 7, 11, 13, 17, 19, 23, 29

          .text
main:
          la   $a0, tdata   # $a0 points to tdata[0]
          lw   $a1, tsize   # $a1 equals number of elements in tdata[]

          jal  reverser     # call procedure reverser

          li   $v0, 10      # terminate program
          syscall

And now for my reverser.asm

.globl reverser
# Pre:
#      $a0 is not null, $a0 points to list[0]
#      $a1 equals number of elements in list[]
# Post:
#      $a0 points to list[0]
#      $a1 equals number of elements in list[]
#      Elements in list[] are now in reversed order.
#
reverser:
        # modify nothing above this line

	move $t0, $a0

	li $s0, 0		# left pointer
	addi $s1, $a1, -1	# right pointer value
	li $t3, 4		# word size

loop:	bge $s0, $t0, done
	mul $t1, $t1, $t3	# multiply the left pointer value by 4 to be able to refer to it

	mul $t2, $t2, $t3	# multiply the right pointer value by 4 to be able to refer to it
	
	sw $a0, $s2($t0)	# insert the left pointer value into the right pointer value in the oriignal array
	sw $a0, $s3($t0)	# insert the right pointer value into the left pointer value in the original array

	addi $s0, $s0, 1	# increment the left pointer
	addi $s1, $s1, -1	# decrement the right pointer

done:

        # modify nothing below this line
        jr    $ra

However, I am getting an error for lines 24 and 25:

sw $a0, $s2($t0)	# insert the left pointer value into the right pointer value in the oriignal array
sw $a0, $s3($t0)	# insert the right pointer value into the left pointer value in the original array

Error is that the operand is not of correct type. Why wouldn't $s2 and $s3 work? They store integers.

Also, is my algorithm correct? If not, how can I possibly use bitshift operators to reverse an array?

Any help is appreciated...
Thank you,

This article has been dead for over six months. Start a new discussion instead.