For I8, can the sw instruction not be replaced by the addi instruction? i.e. $ t6, $ t5, 0
Not. The sw
command stores the result in memory. add
just manages the registers. And lw
gets the word out of memory. This is the only MIPS instruction to do this. (Other processors may have and have add
versions that have memory access but not MIPS.)
You need to adjust your thinking when working in assembler. Registers and memory are separated. In higher-level languages, registers are (almost) completely hidden. In an assembly, registers are a separate resource that a programmer must manage. And this is a scarce resource. The HLL compiler will do this for you, but when programming in the assembly, you took over the work.
How does MIPS handle pointers?
In MIPS, pointers are integers (in registers or memory) that are memory addresses. The only way to distinguish them from data is your brain. A "pointer" is something that a higher-level language developer has come up with to ease a programmer for this burden. If you look carefully, you will see that $t5
actually contains a pointer. This is the memory address used by lw
and sw
as the address to load or save.
source share