Electronic – How does the Store Word(SW) and Load Word(LW) instructions work, MIPS

assemblycomputer-architecturemips

enter image description here

The SW and LW instructions are defined as:

     sw $t, offset($s)  : 1010 11ss ssst tttt iiii iiii iiii iiii
     lw $t, offset($s)  : 1000 11ss ssst tttt iiii iiii iiii iiii

SW performs the operation MEM[$s + offset] = $t, but in the data-path it looks like they have performed the operation MEM[Data($s)+ offset] = $t , because instead of taking the value $s as an input into the ALU it took in the data stored in $s.

LW performs the operation $t = MEM[$s + offset], but looking at the datapath it looks like it is performing $t = MEM[Data($s) + offset].

Another thing I don't understand is why we use sign extend instead of just moving the sign to the 32nd bit and filling the rest of the data with zeros.If you have defined an offset is

0x8fff : 1000 1111 1111 1111

it will be sign extended to 0xffff8fff : 1111 1111 1111 1111 1000 1111 1111 1111 , which is a completely different number from the offset, the number we really need is 0x80008ffff.

Best Answer

$s and $t are register specifiers. Your interpretation MEM[RegisterData(s) + offset] is basically correct. $s means, essentially, "get the data out of register number s in the register file." So if s in the instruction is the bit pattern 10001, then $s means: read the value out of register 17, add the sign-extended offset bits (the 16 bits represented by i in the encoding), and use that as the address to access the memory. Similarly for the $t in the lw instruction. It means write the data back into the register numbered t.

Your question about sign extension: look up two's complement. The 16 bit two's complement number 0000 1111 1111 1111 is decimal +4095. The 16 bit two's complement number 1000 1111 1111 1111 is decimal -28673. The 32 bit two's complement number 0xffff8fff is also -28673. The 32 bit two's complement number 0x80008fff is decimal -2147446785, which is clearly not what you want.