了解MIPS程序中的lw和sw实际工作方式 [英] Understanding how `lw` and `sw` actually work in a MIPS program

查看:3743
本文介绍了了解MIPS程序中的lw和sw实际工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解MIPS程序中swlw的功能.我对该主题的理解是,我们使用lw将数据从内存传输到寄存器中,反之亦然.但是,这到底是如何实现的呢?

假设我们有以下代码行:

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

如果我们专注于lw,它实际上是存储来自存储器的数据,Reg.Source并将该数据的地址乘以Offset,始终是$ 4 $的倍数,因为寄存器处理的是$ 32 $位,而内存使用$ 8 $位到寄存器中等于Offset + Reg.Source的特定地址-因此,如果我们说Offset = 16, Reg.Source = $s1 = 12,则寄存器会将来自存储器的数据存储到寄存器中的地址$ 28 $中. /p>

假设我对lw的理解是正确的,那么我的问题是sw如何工作?

PS:如果答案也只包含一个衬里示例(例如sw $t0, 32($s3)),那就太好了.

解决方案

lw(加载字)将一个字从内存加载到寄存器.

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

$ 2是目标寄存器,$ 4是地址寄存器.信息的来源就是记忆.

4是一个偏移量,该偏移量被添加(而不是相乘)到地址寄存器.这种内存访问称为基于地址的访问,在许多情况下非常有用.例如,如果$ 4保留一个结构的地址,则偏移量允许选择该结构的不同字段.或数组中的下一个或上一个元素,等等.offset不必是4的倍数,但是(地址寄存器+ offset)必须并且在大多数情况下都是.

Sw与之类似,但是将寄存器存储在内存中.

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

再次$ 7是保存内存地址的寄存器,$ 8是一个偏移量,$ 5是将要写入内存的信息的来源.

请注意,与其他MIPS指令相反,第一个操作数是源,而不是目标.可能这是为了强制执行以下事实:地址寄存器在两条指令中都起着类似的作用,而在lw中,它用于计算数据源的存储地址,而在sw中,它用于存储目标地址.

I'm having bit of a difficulty understanding what sw and lw do in a MIPS program. My understanding of the topic is that we use lw to transfer data from the memory into the register and vice-versa for 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.

Assuming that my understanding of lw is correct, my question is then how does sw work?

PS: It would be great if the answers could also contain just a one liner example such as sw $t0, 32($s3).

解决方案

lw (load word) loads a word from memory to a register.

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

$2 is the destination register and $4 the address register. And the source of information is the memory.

4 is an offset that is added (not multiplied) to the address register. 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. 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.

Sw is similar, but stores a register into memory.

 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.

Note that contrary to others MIPS instructions, the first operand is the source, not the destination. 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.

这篇关于了解MIPS程序中的lw和sw实际工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆