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)
.
sw
does). – Ped7gSW rt, offset(base)
Description:memory[base+offset] ← rt
The least-significant 32-bit word of register rt is stored in memory at the location specified by the aligned effective address. The 16-bit signed offset is added to the contents of GPR base to form the effective address. – Michael