assembly – Understanding how `lw` and `sw` actually work in a MIPS program

Assembly – Understanding How `Lw` And `Sw` Actually Work In A Mips Program

assembly – Understanding how `lw` and `sw` actually work in a MIPS program

lw (load phrase) hundreds a phrase from reminiscence to a register.

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

$2 is the vacation spot register and $4 the deal with register. And the supply of data is the reminiscence.

4 is an offset that’s added (not multiplied) to the deal with register. This sort of reminiscence entry is known as based mostly addressing and is it fairly helpful in many conditions. For occasion, if $4 maintain the deal with of a struct, the offset permits to choose the completely different fields of the struct. Or the following or earlier aspect in a array, and so forth. offset doesn’t should be a a number of of 4, however (deal with register + offset) should and more often than not each are.

Sw is analogous, however shops a register into reminiscence.

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

Again $7 is the register holding the reminiscence deal with, 8 an offset and $5 is the supply of the data that can be written in reminiscence.

Note that opposite to others MIPS directions, the primary operand is the supply, not the vacation spot. Probably that is to implement the truth that the deal with register performs a comparable function in each instruction, whereas in lw it’s used to compute the reminiscence deal with of the supply of knowledge and in sw the reminiscence vacation spot deal with.

READ :  [Solved] npm ERR! code E401 npm ERR! Unable to authenticate, need: Bearer authorization_uri

assembly – Understanding how `lw` and `sw` actually work in a MIPS program

Related posts on Assembly :

Leave a Reply

Your email address will not be published. Required fields are marked *