0

I got this question on a midterm, and I want to know what the right answer is. Here is the question:
Let x[] be an array of integers and k be an integer. suppose that the memory address of x and k are specified by the two labels "x" and "k" respectively. implement the following statement in the mips assembly language x[4] = x[5] +k

Here is my attempt of answering, and i only got half mark:

//addresses of x, k, 4, and 5 la $s0, x la $s1, k li $s2, 4 li $s3, 5

//assume $s1 = x[4] and $s2 = x[5] la $s3, k add $s1, $s2, $s3 //x[4] = x[5] + k

the feedback said i should have lw and sw but i'm not sure what to do with them.

1
  • lw is load word and sw is store word. you need to read the word at address x+(5*sizeof(word)) (which is x + 20 yes?), so that means lw from the address x+20 which there is a way to do that with one lw. Then you need to take that add k to it which you know how to do. and then use sw to store that to the address x + (4*sizeof(word)) or x+16. Commented Feb 19, 2015 at 3:32

1 Answer 1

1

Assuming an "integer" means a 32-bit word size, x[4] is at address (x+16) and x[5] is at address (x+20).

You were probably supposed to do something like this :

la $s0, x
la $s1, k
lw $s2, 0($s1)       ; Get "k" from its memory location
lw $s3, 20($s0)      ; Get "x[5] from its memory location
add $s2, $s2, $s3    ; Compute k + x[5]
sw $s2, 16($s0)      ; Store result at location "x[4]"
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.