Instruction Set Architecture

An abstraction on the interface between the hardware and the low-level software.

Machine code has instructions in binary, while assembly is still human readable (to a certain extent) as compared. Machine code is written in hexadecimal.

Walkthrough

The two major components in Processor and Memory. The processor performs computations while the memory stores the code and data. The code and data resides in the memory and transfers into the processor during execution.

To prevent frequent access of memory, registers are used to provide temporary storage for values in the processor.

Register

Provides temporary storage of values in the processor. The arithmetic operations then can work directly on registers, which speed up the processes.

The load-store model Load: Moving data from memory into a register Store: Moving data from a register into memory

Memory instruction is then needed to move the data into registers and memory.

Instructions

  • memory
  • calculation
  • control flow
    • repetition (loops)
    • selection (conditional)

General Purpose Registers

Registers have no data type unlike program variables - machine/assembly instruction assumes the data stored in the register is of the correct type.

NameRegister NumberUsage
$zero0Constant value 0
$at1Reserved for assembler
$v0-$v12 - 3Values for results and expression evaluation
$a0-$a34 - 7Arguments
$t0-$t78 - 15Temporaries
$s0-$s716 - 23Program variables
$t8-$t924 - 25More temporaries
$k0-$k126 - 27Reserved for operating system
$gp28Global pointer
$sp29Stack pointer
$fp30Frame pointer
$ra31Return address

Assembly Language

Each line of assembly code contains at most one instruction.

# Anything tagged with the hex-sign to end-of-line will be ignored by the assembler (comments)

General Instruction Syntax

OPERATION DESTINATION, SOURCE, SOURCE

Naturally, most of the MIPS arithmetic/logic operations have three operands: 2 sources and 1 destination. MIPS arithmetic operations are mainly register-to-register.

Addition

Example

In C, a = b + c is converted into the MIPS assembly code add $s0, $s1, $s2.

Variable mapping is assumed (the values of a, b, c are loaded into registers $s0, $s1, $s2 respectively.)

Subtraction

Example

In C, a = b - c is converted into the MIPS assembly code sub $s0, $s1, $s2

Complex Expressions

Example

In C, a = b + c - d is converted into the MIPS assembly code:

add $t0, $s1, $s2
sub $s0, $t0, $s3

two source operands. Thus, a complex statement needs to be broken into multiple MIPS instructions.

A single MIPS instruction can handle at most

Use of temporary registers t0 to t7 for intermediate results.

To store intermediate results, use the temporary registers

Immediate Operands

Immediate values are numerical constants frequently used in operations.

addi

The addi instruction is used to add numerical constants. The syntax is then addi OPERAND, OPERAND, IMMEDIATE_VALUE. The constant can range from to , using the 2s complement number system on a 16-bit number.

subi does not exist Use addi with a negative constant instead.

Register Zero $zero

The assignment of f = g is equivalent to add $s0, $s1, $zero (after assumed variable mapping). There is a equivalent pseudo-instruction move $s0, $s1.

Logical Operations

Arithmetic operations view the content of a register as a single quantity. Logical operations view the content of a register as 32 raw bits instead - allowing for operation on individual bits/bytes within a word.

Logical OperationC OperatorJava OperatorMIPS Instruction
Shift Left<<<<sll
Shift Right>>>>, >>>srl
Bitwise AND&&and, andi
Bitwise OR||or, ori
Bitwise NOR~~nor
Bitwise XOR^^xor
*Truth tables for bitwise operations
aba AND b
000
010
100
111
aba OR b
000
011
101
111
aba NOR b
001
010
100
110
aba XOR b
000
011
101
110

Shifting

sll TEMPORARY_REGISTER, VARIABLE_REGISTER, CONSTANT
srl TEMPORARY_REGISTER, VARIABLE_REGISTER, CONSTANT

The operation shifts the value in the variable_register, $s0 for example, by the values in the constant, and fills the emptied position with zeroes, into the temporary_register, $t0 for example.

Bitwise operations

AND operation:

Can be used for masking operations (setting irrelevant parts of the register to 0)

  • to ignore bits in certain positions, place in the positions to be ignored.
  • to only focus on certain positions, place in the interested positions.

OR operation:

Forces certain bits to 1s.

NOR operation:

Can be used to implement the NOT operation to toggle bits. nor $t0, $t0, $zero

Why is a NOT instruction not provided in the instruction set?

Keep instruction set small.

XORoperation:

This can also be used to implement the NOT. xor $t0, $t0, $t2 where $t2 contains all 1s.

Large Constant

How to load 32-bit constant into register?

  1. Use lui (load upper immediate) to set upper 16-bit: lui $t0, 0xAAAA
  2. Use ori (or immediate) to set lower-order bits ori $t0, $t0, 0xF0F0