0
A[k++] = A[k] + A[k]
# i-> $s0, k-> $s1 , base of A[] ->$s2

i tried the following but can't figure out how to store back in array A[k++]....

sll $t0 , $s1 , 2
add $t0 , $t0 , $s2
lw $t1, 0($t0)
add $s1, $t1 , $t1 # i added A[k] + A[k]
4
  • How about using sw to store it since you use lw to load it, or is there some other problem? Commented Feb 12, 2017 at 7:01
  • # shift left logical sll $t1 , $s1 , 2 # add 1 addi $t1 , $t1 , 1 # now add with the base add $t1, $t1 , $s2 # finally store back in array sw $s1 , 0($t1) will it work ? Commented Feb 12, 2017 at 7:47
  • You already have the address in $t0, no need to calculate it again. Just store. Commented Feb 12, 2017 at 7:54
  • the element memory location are calculated as follow: array variable base address + 4bytes index in case you have an array with or words... otherwise you should index with the element size, for string will be byte. Commented Oct 12, 2022 at 9:58

1 Answer 1

1

In order to iterate throught arrays in MIPS, you need to add 4 bytes for each position of the array. Example:

.data
  myArray: .store 12 # array with length of 3 (3 x 4 bytes)
.text
  li $t0, 0
  li $s0, 1
  sw $s0, myArray($t0) # myArray[0] = 1
  addi $t0, 4
  sw $s0, myArray($t0) # myArray[1] = 1
  addi $t0, 4
  sw $s0, myArray($t0) # myArray[2] = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.