2.1. Introduction
Instruction Set language of the hardware. Vocabulary understood by a given architecture.
2.2. Operations of the Computer Hardware
- Register: Places to store variables
An instruction in MIPS looks like this:
add $s1, $s2, $s3
which means
s1
← s2
+ s3
Notice that one instruction has three operands. In MIPS, there are 32 registers and memory addresses which can be used as operands.
Simplicity favors regularity. It’s easier to design hardware for a fixed number of operands and registers.
2.3. Oper_and_s Of the Computer Hardware
- Word: size of a register (32-bits in a 32-bit architecture)
i.e.
1 word = 4 bytes = 32 bits
> Smaller is Faster. Having only 32 registers is simple; having 3 operands is simple. More registers will lead to slower clock cycles. > - Alignment Restriction: data in memory must be aligned to multiples of 4.
i.e. since each memory address refers to one byte, valid memory addresses are
0x04
,0x08
, … (each are 1 word in size). - Endian-ness: big endian refers the leftmost
0x04
referring to0x04~0x07
and so on. little endian refers to the rightmost0x04
referring to0x01~0x04
and so on. Picture: big▶️little
Data Transfer Instruction: instruction to move data between RAM and registers. Supply the memory address stored in a register
Note that the offset is in bytes, not words. → Thus to access arr[1]
you should offset 4($s1)
. Offset addressing is natural for arrays and structs (from C).
Immediate Operations: instructions to add a constant to a register value. Immediate operations are very fast, since they don’t need to load data from memory.
2.4. Signed and Unsigned Numbers
Two’s Compliment Representation: think of it like this:
This representation is the default since 1965.
Advantages of Two’s Compliment Representation:
- Binary to Decimal: most significant bit is
-2^32
and the rest is normal binary - Negation: invert bits and add one—this works both ways!
- Sign Extension(=Precision Extension) (16→32 bits, etc.): extend the sign bit leftward (most significant bit) i.e. positive number: extend the zeros; negative number: extend the one.
2.5. Representing Instructions in the Computer
Register Representation:
Instruction in Machine Code: instructions can be R-type or I-type. Each have different sized-fields (but is 32 bits in total).
Each part of the instruction is called a field. List of Field Names:
op
: opcode, the operationrs
: register sourcert
: register source 2rd
: register destination (result of the operation)shamt
: shift amountfunct
: function code, select a specific variant of the opcode.
Line label. loop
in the following instruction is a line label. It’s just there for humans; like comments in code.
loop: lw [...]
add [...]
j loop
2.6. Logical Operations
| Operation | command | example | explaination | comments |
| ----------- | --------- | ----------------- | ------------------------------ | ----------------------------------------------------------------------------------------- | ------------------------------------------------ |
| Shift Left | sll | sll s0, 4 | s0to left 4 bits, store in t2 | equivalent to multiply by 2^n |
| Shift Right | srl | srl s0, 4 | s0to right 4 bits, store in t2 | equivalent to divide by 2^n |
| AND | and, andi | and t1, $t2 | t0 = t1 & t2 | andi has a constant in place of a second register. |
| OR | or, ori | or t1, $t2 | t0 = t1 | t2 | orihas a constant in place of a second register. |
| NOT | nor | nor t1, $t2 | | NOR(A, B) = NOT (A OR B) thus if one operator is 0, then it becomes a simple NOT command. |
2.7. Instructions for Making Decisions
- Basic block: a block of assembly code without branches
Two types of jump
operations: conditional branches and unconditional jump.
Conditional Branches:
Unconditional Jump:
Less Than (branch on less than is not included since it takes clock cycles)
Treating signed numbers as unsigned is useful in checking
0 \leq x \lt y
. >sltui $t0, $s1, 10
← ifs1
is negative, treating it as an unsigned number the2^{31}
th place has a 1 which makes it a *very big signed number—*and thust0
is set to 0. Ifs1
is positive then its value is same signed or unsigned; thuss1 < 10
is evaluated normally.
2.8. Supporing Procedures in Computer Hardware
Procedure: =function call in assembly.
- Leaf Procedure. A procedure that doesn’t call any other procedures. (think: the end of a branch)
To execute a procedure you need to:
- Put parameters in a place expected by the procedure
- Transfer control to the procedure
- Perform task
- Put result value in expected place (by the caller)
- return control to caller
Registers (usually) used for procedure calling:
a0 ~ a3
: pass argumentsv0 ~ v1
: return valuesra
: return address (return here after procedure is done)- Program Counter (PC): holds the memory address of the instruction currently executing
Instruction specialized for procedure calling:
Spilling Registers into the Stack
- Stack. When
a0 ~ a3
isn’t enough for a procedure,s0 ~ s7
should be saved into a memory portion call thed stack. (t0 ~ t9
is not saved)$sp
points to the end of the stack (where spilled data should be stored next)- placing and removing data is called push / pop
- By convention stack grows from higher memory address to lower memory address, e.g. from
0x08
to0x04
, and by one word each.
- Procedure Frame (=Activation Record). Space on stack reserved for storing the procedure’s local varaibles and arrays that don’t fit into registers. The frame is located below the saved registers at the little end of the stack.
$fp
(frame pointer) points to the little end of the procedure frame. This is useful because stack pointer can change during a procedure.
Calling a leaf procedure:
-
adjust stack pointer by number of registers that requires saving:
-
execute procedure body
-
save return value to registers
v0 ~ v1
-
restore register values to registers
Data preserved/not preserved across procedure calls:
Preserved | Not Preserved |
---|---|
s0 ~ s7 | t0 ~ t9 |
sp stack pointer | a0 ~ a3 argument register |
ra return address | v0 ~ v1 return value register |
stack above stack pointer (bigger end) | stack below stack pointer (littler end) |
(Not storing the temporary registers t0~t9
reduces memory store/load.)
Allocating Data on the Heap
Structure of the executable, from big end to little end:
- The Stack grows down (higher address → lower address).
sp
→7fff fffc
- The Heap grows up (lower addr → higher addr).
- Text Segment (instructions)
- Reserved
2.9. Communicating with People
ASCII Table on p.106. Notice that:
- one ascii charater is 1 byte (=8 bits)
0b0
isNULL
- upper and lower letters differ by
To store strings, either:
- first position in string stores length
- accompanying variable holds string length or
- the last position marks the end of the string
Sinced people use ASCII a lot, and ASCII is 1 byte, there are data transfer instructions for bytes:
Unicode is used by Java and other modern languages. Notice that:
- UTF-16 is default, using 16-bits
- UTF-8 is ASCII-compatible, and is variable-length
- UTF-32 uses 32 bits
MIPS also accomodates UTF-16 by providing data transfer instructions for half-words (16-bits):
2.10. MIPS Addressing for 32-bit Immediates and Addresses
When you need to load a 32-bit constant into register, you need two instructions (a new one, lui
):
Addressing in Jumps and Branches