Memory access and the control of stored information is an incredibly powerful aspect of Assembly while also being one of the most confusing.
As we write Assembly, we control exactly where every piece of information is stored. This means it is on us to choose if a value is stored in a register for immediate use or in alternate memory to be loaded from later.
In the MIPS Assembly language, all calculations are performed exclusively on registers. Since we are limited on the number of registers we have, when we no longer need to use data right away we push it back into memory to access later.
The statements that provide these push and pull operations are
SW, which stand for
LOAD WORD and
A word is a fixed length of data defined by the ISA. In the MIPS32 architecture that we have been using, a word is a 32-bit value.
In this example we will load a number value from memory, add
15 to it, and save it back to memory:
LWloads the value stored in the memory address of Register 3 into Register 1LW $1, ($3)
ADDIadds the value of Register 1 and the constant,
15, and stores the result in Register 1ADDI $1, $1, 15
SWstores the value in Register 1 to the memory address of Register 3SW $1, ($3)
XORis used to store zero in Register 1, essentially resetting the register.XOR $1, $1, $1
Unlike high-level languages, in Assembly, it is the programmer’s responsibility to remember where pieces of data are stored and ensure not to overwrite it. Registers are limited and memory takes time to access, so it is a careful balancing act of keeping data available or pushing it deeper into memory.
Create a new variable,
answer_1, and set it equal to the opcode used to store a word value to a location.
Capitalize the whole answer.
Create another variable,
answer_2, and set it equal to the opcode used to load a word value from memory.