简体   繁体   English

了解 `lw` 和 `sw` 在 MIPS 程序中的实际工作方式

[英]Understanding how `lw` and `sw` actually work in a MIPS program

I'm having bit of a difficulty understanding what sw and lw do in a MIPS program.我有点难以理解swlw在 MIPS 程序中的作用。 My understanding of the topic is that we use lw to transfer data from the memory into the register and vice-versa for sw .我对这个话题的理解是,我们使用lw将数据从内存传输到寄存器,反之亦然sw But how is this exactly accomplished?但这究竟是如何实现的呢?

Let's say we have the following line of code:假设我们有以下代码行:

lw Reg.Dest, Offset(Reg.Source)
sw Reg.Source, Offset(Reg.Dest)

If we concentrate on lw it's essentially storing the data from the memory, Reg.Source and multiplying the address of that data with the Offset , always a multiple of $4$ because the registers deal with $32$ bits and the memory uses $8$ bits, into a specific address in the register which is equal to Offset + Reg.Source - so if we say that Offset = 16, Reg.Source = $s1 = 12 then the register will store the data from the memory into the address $28$ in the register.如果我们专注于lw它本质上是存储来自内存Reg.Source的数据,并将该数据的地址与Offset相乘,始终是 $4$ 的倍数,因为寄存器处理 $32$ 位,而内存使用 $8$ 位,进入寄存器中的特定地址,它等于Offset + Reg.Source - 所以如果我们说Offset = 16, Reg.Source = $s1 = 12那么寄存器会将内存中的数据存储到地址 $28$ 中登记册。

Assuming that my understanding of lw is correct, my question is then how does sw work?假设我对lw理解是正确的,那么我的问题是sw是如何工作的?

PS: It would be great if the answers could also contain just a one liner example such as sw $t0, 32($s3) . PS:如果答案也可以只包含一个单行示例,例如sw $t0, 32($s3) ,那就太好了。

lw (load word) loads a word from memory to a register. lw(加载字)将一个字从内存加载到寄存器中。

lw $2, 4($4) # $2 <- mem($4+4)

$2 is the destination register and $4 the address register. $2 是目标寄存器,$4 是地址寄存器。 And the source of information is the memory.而信息的来源是记忆。

4 is an offset that is added (not multiplied) to the address register. 4 是添加(未乘)到地址寄存器的偏移量。 This kind of memory access is called based addressing and is it quite useful in many situations.这种内存访问称为基于寻址,在许多情况下非常有用。 For instance, if $4 hold the address of a struct, the offset allows to pick the different fields of the struct.例如,如果 $4 保存结构的地址,则偏移量允许选择结构的不同字段。 Or the next or previous element in a array, etc. offset does not have to be a multiple of 4, but (address register + offset) must and most of the time both are.或者数组中的下一个或上一个元素等。偏移量不必是 4 的倍数,但(地址寄存器 + 偏移量)必须并且大多数时候两者都是。

Sw is similar, but stores a register into memory. Sw 类似,但将寄存器存储到内存中。

 sw $5, 8($7) # mem[$7+8] <- $5

Again $7 is the register holding the memory address, 8 an offset and $5 is the source of the information that will be written in memory.同样,$7 是保存内存地址的寄存器,8 是偏移量,$5 是将写入内存的信息的来源。

Note that contrary to others MIPS instructions, the first operand is the source, not the destination.请注意,与其他 MIPS 指令相反,第一个操作数是源,而不是目标。 Probably this is to enforce the fact that the address register plays a similar role in both instruction, while in lw it is used to compute the memory address of the source of data and in sw the memory destination address.这可能是为了加强地址寄存器在两条指令中扮演类似角色的事实,而在 lw 中它用于计算数据源的内存地址,而在 sw 中则用于计算内存目标地址。

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

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