简体   繁体   English

MIPS汇编语言中的内存访问

[英]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. 令x []为整数数组, k为整数。 suppose that the memory address of x and k are specified by the two labels "x" and "k" respectively. 假设x和k的存储地址分别由两个标签“ x”和“ k”指定。 implement the following statement in the mips assembly language x[4] = x[5] +k 在mips汇编语言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. 反馈说我应该有lw和sw,但是我不确定该如何处理。

Assuming an "integer" means a 32-bit word size, x[4] is at address (x+16) and x[5] is at address (x+20). 假设“整数”表示32位字长,则x [4]位于地址(x + 16),x [5]位于地址(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]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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