简体   繁体   中英

Memory Access in MIPS assembly language

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.

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]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM