Module 3 (02-02-2025)
Module 3 (02-02-2025)
Topic – 3:
Instructions: Language of the Computer
Course Teacher:
Dr. Md. Tarek Habib
Assistant Professor
Department of Computer Science and Engineering
Reference Book
2
Topic Contents
• Introduction [Sec. – 2.1]
3
Topic Contents
• MIPS Addressing for 32-bit Immediates and
Addresses [Sec. – 2.10]
4
The Five Classic
Components of A Computer
5
• Section 2.1:
Introduction
6
Instructions: Overview
■ To command a computer’s hardware, you must speak its
language.
■ The words of a computer’s language are called instructions,
and its vocabulary is called an instruction set.
■ So, the vocabulary of commands understood by a given
architecture is called an instruction set.
■ By learning how to represent instructions, you will also discover
the secret of computing: the stored-program concept.
■ Stored-program concept is the idea that instructions and
data of many types can be stored in memory as numbers,
leading to the stored-program computer.
7
Instructions: Overview
■ Language of the machine, i.e. instruction is more primitive
than higher level languages, e.g., no sophisticated control
flow such as while or for loops
■ Very restrictive
■ e.g., MIPS arithmetic instructions
■ We’ll be working with the MIPS instruction set architecture
■ inspired most architectures developed since the 1980's
■ used by NEC, Nintendo, Silicon Graphics, Sony
■ Design goals: maximize performance and minimize cost
and reduce design time
8
• Section 2.2:
Operations of the Computer Hardware
9
MIPS Arithmetic
■ All MIPS arithmetic instructions have 3 operands
■ Operand order is fixed (e.g., destination first)
■ Example:
compiler’s job to associate
C code: A = B + C variables with registers
10
MIPS Arithmetic
■ Design Principle 1: simplicity favors regularity.
Translation: Regular instructions make for simple hardware!
12
MIPS Arithmetic
■ Operands must be in registers – only 32 registers provided
(which require 5 bits to select one register).
■ Reason for small number of registers:
13
Registers vs. Memory
■ Arithmetic instructions operands must be in registers
■ MIPS has 32 registers
■ Compiler associates variables with registers
■ What about programs with lots of variables (arrays,
etc.)?
■ Use memory, load/store operations to transfer data
from memory to register – if not enough registers spill
registers to memory
14
Registers vs. Memory…
■ Many programs have more variables than computers have registers.
■ Consequently, the compiler tries to keep the most frequently used variables
in registers and places the rest in memory, using loads and stores to move
variables between registers and memory.
■ The process of putting less commonly used variables (or those needed later)
into memory is called spilling registers.
■ MIPS is a load/store architecture.
Control Input
Memory
Datapath Output
Processor I/O
15
Memory Organization
■ Viewed as a large single-dimension array with access by address
■ A memory address is an index into the memory array
■ Byte addressing means that the index points to a byte of
memory, and that the unit of memory accessed by a load/store
is a byte
0 8 bits of data
1 8 bits of data
2 8 bits of data
3 8 bits of data
4 8 bits of data
5 8 bits of data
6 8 bits of data
...
16
Memory Organization
■ Bytes are load/store units, but most data items use larger words
■ For MIPS, a word is 32 bits or 4 bytes.
0 32 bits of data
4 32 bits of data Registers correspondingly hold 32 bits of data
8 32 bits of data
12 32 bits of data
...
17
Load/Store Instructions
■ Load and store instructions
■ Example:
■ Instruction Meaning
20
MIPS Operands
21
MIPS Assembly Instructions
MIPS Assembly Instructions…
23
• Section 2.5:
Representing Instructions in the Computer
24
Machine Language
■ Instructions, like registers and words of data, are also 32 bits long
■ Example: add $t0, $s1, $s2
■ registers are numbered, e.g., $t0 is 8, $s1 is 17, $s2 is 18
25
Machine Language
■ Consider the load-word and store-word instructions,
■ what would the regularity principle have us do?
■ we would have only 5 or 6 bits to determine the offset from a base
register - too little…
26
Stored-Program Concept
■ Instructions are represented as bit sequences, i.e. numbers
■ Programs are stored in memory to be read or written, just like
data.
28
Memory Organization:
Big/Little Endian Byte Order
■ Bytes in a word can be numbered in two ways:
■ byte 0 at the leftmost (most significant) to byte 3 at the rightmost
(least significant), called big-endian 0 1 2 3
■ byte 3 at the leftmost (most significant) to byte 0 at the rightmost
(least significant), called little-endian 3 2 1 0
Big-endian Little-endian
Bit 31
Bit 31
Bit 0
Bit 0
Memory Memory
Byte 0 Byte 1 Byte 2 Byte 3 Word 0 Byte 3 Byte 2 Byte 1 Byte 0 Word 0
Byte 4 Byte 5 Byte 6 Byte 7 Word 1 Byte 7 Byte 6 Byte 5 Byte 4 Word 1
29
Memory Organization:
Big/Little Endian Byte Order
■ SPIM’s memory storage depends on that of the underlying
machine
■ Intel 80x86 processors are little-endian
■ because SPIM always shows words from left to right a “mental
adjustment” has to be made for little-endian memory as in Intel PCs
in our labs: start at right of first word go left, start at right of next
word go left, …!
■ Word placement in memory (from .data area of code) or word
access (lw, sw) is the same in big or little endian
■ Byte placement and byte access (lb, lbu, sb) depend on big or
little endian because of the different numbering of bytes within a
word
■ Character placement in memory (from .data area of code)
depend on big or little endian because it is equivalent to byte
placement after ASCII encoding
■ Run storeWords.asm from SPIM examples!!
30
• Section 2.6:
Logical Operations
31
Logical Operators
■ Logical operators are executed bit-wise, which
means that each bit in the first operand is
compared to the corresponding bit (in accordance
with the position) in the second operand.
32
Logical Operators…
33
Shift Instructions
34
Shift Instructions…
35
Shift Instructions…
36
AND Instruction
37
OR Instruction
38
NOR and XOR Instructions
■ The full MIPS instruction set also includes NOR (NOT OR)
and XOR (EXCLUSIVE OR).
■ NOT instruction is not included in the MIPS instruction set.
Why?
■ In keeping with the three-operand format, the designers of
MIPS decided to include the instruction NOR (NOT OR)
instead of NOT. If one operand is zero, then it is equivalent
to NOT: A NOR 0 = NOT (A OR 0) = NOT (A).
39
NOR and XOR Instructions
■ If register $t3 has the value 0,
nor $t0,$t1,$t3 # reg $t0 = ~(reg $t1 | reg $t3)
40
• Section 2.7:
Instructions for Making Decisions
41
Control: Conditional Branch
■ Decision making instructions
■ alter the control flow,
■ i.e., change the next instruction to be executed
I op rs rt 16 bit
offset
44
Control: Unconditional Branch
(Jump)
■ MIPS unconditional branch instructions:
j Label
■ Example:
if (i!=j) beq $s4, $s5, Lab1
h=i+j; add $s3, $s4, $s5
else j Lab2
h=i-j; Lab1: sub $s3, $s4, $s5
Lab2: ...
■ J-type (“J” for Jump) instruction format
word-relative
■ Example: j Label # addr. Label = 100
addressing:
25 words = 100 bytes
000010 0000000000000000000001100
6 bits
1 26 bits
op 26 bit number
45
Addresses in Jump
■ Word-relative addressing also for jump instructions
J op 26 bit
address
■ MIPS jump j instruction replaces lower 28 bits of the PC with
A00 where A is the 26 bit address; it never changes upper 4 bits
■ Example: if PC = 1011X (where X = 28 bits), it is replaced with
1011A00
■ there are 16(=24) partitions of the 232 size address space, each
partition of size 256 MB (=228), such that, in each partition the upper
4 bits of the address is same.
■ if a program crosses an address partition, then a j that reaches a
different partition has to be replaced by jr with a full 32-bit address
first loaded into the jump register
■ therefore, OS should always try to load a program inside a single
partition
46
Constants
■ Small constants are used quite frequently (50% of operands)
e.g., A = A + 5;
B = B + 1;
C = C - 18;
■ MIPS Instructions:
addi $29, $29, 4
slti $8, $18, 10
andi $29, $29, 6
ori $29, $29, 4
op rs rt 16 bit number
48
How about larger constants?
■ First we need to load a 32 bit constant into a register
■ Must use two instructions for this: first new load upper immediate
instruction for upper 16 bits
lui $t0, 1010101010101010 filled with zeros
1010101010101010 0000000000000000
0000000000000000 1010101010101010
ori
1010101010101010 1010101010101010
■ Formats:
R op rs rt rd shamt funct
I op rs rt 16 bit address
op 26 bit address
J
50
Control Flow
■ We have: beq, bne. What about branch-if-less-than?
■ New instruction:
if
$s1 < $s2 then
$t0 = 1
slt $t0, $s1, $s2 else
$t0 = 0
52
Policy-of-Use Convention for
Registers
53
Assembly Language vs.
Machine Language
■ Assembly provides convenient symbolic representation
■ much easier than writing down numbers
■ regular rules: e.g., destination first
54
Procedures
■ Example C code:
int add10(int i)
{ return (i + 10);}
55
Procedures
■ Translated MIPS assembly
■ Note more efficient use of registers possible! save register
in stack, see
.text figure below
.globl main add10:
addi $sp, $sp, -4
main: sw $s0, 0($sp)
addi $s0, $0, 5
add $a0, $s0, $0 addi $s0, $a0, 10
argument add $v0, $s0, $0
to callee jal add10 result
control returns here to caller
jump and link lw $s0, 0($sp)
add $s1, $v0, $0 restore
addi $sp, $sp,
values 4
add $s0, $s1, $0
return
jr $ra
li $v0, 10 system code
syscall MEMORY High address
& call to
exit $sp
Content of $s0
Low address
Run this code with PCSpim: procCallsProg1.asm 56
MIPS: Software Conventions
for Registers
0 zero constant 0 16 s0 callee saves
1 at reserved for assembler ... (caller can clobber)
2 v0 results from callee 23 s7
3 v1 returned to caller 24 t8 temporary (cont’d)
4 a0 arguments to callee 25 t9
5 a1 from caller: caller saves 26 k0 reserved for OS kernel
6 a2 27 k1
7 a3 28 gp pointer to global area
8 t0 temporary: caller saves 29 sp stack pointer
... (callee can clobber) 30 fp frame pointer
15 t7 31 ra return Address (HW):
caller saves
57
Procedures (recursive)
■ Example C code – recursive factorial subroutine:
int main()
{ int i;
i = 4;
j = fact(i);
return 0;}
int fact(int n)
{ if (n < 1) return (1);
else return ( n*fact(n-1) );}
58
■
Procedures (recursive)
Translated MIPS assembly:
.text
.globl main slti $t0, $a0, 1
branch to
beq $t0, $0, L1
main: L1 if
n>=1
nop
addi $a0, $0, 4
control
jal fact addi $v0, $0, 1
returns nop return addi $sp, $sp, 8
from 1 jr $ra
fact if n <
move $a0, $v0
1 L1:
print li $v0, 1 addi $a0, $a0, -1
value syscall if n>=1 call
jal fact
returned fact
by recursively nop
fact li $v0, 10 with argument
exit syscall n-1 lw $a0, 0($sp)
restore return lw $ra, 4($sp)
fact: address,
addi $sp, $sp, 8
argument,
addi $sp, $sp, -8 and stack pointer
save return
address and sw $ra, 4($sp) return mul $v0, $a0, $v0
argument in sw $a0, 0($sp) n*fact(n-1
stack )
jr $ra
Run this code with PCSpim: factorialRecursive.asmreturn 59
control
Using a Frame Pointer
Variables that are local to a procedure but do not fit into registers (e.g., local arrays, struc-
tures, etc.) are also stored in the stack. This area of the stack is the frame. The frame pointer
$fp points to the top of the frame and the stack pointer to the bottom. The frame pointer does
not change during procedure execution, unlike the stack pointer, so it is a stable base
register from which to compute offsets to local variables.
Use of the frame pointer is optional. If there are no local variables to store in the stack it is
not efficient to use a frame pointer. 60
Using a Frame Pointer
■ Example: procCallsProg1Modified.asm
This program shows code where it may be better to use $fp
■ Because the stack size is changing, the offset of variables stored in
the stack w.r.t. the stack pointer $sp changes as well. However, the
offset w.r.t. $fp would remain constant.
■ Why would this be better?
The compiler, when generating assembly, typically maintains a table
of program variables and their locations. If these locations are
offsets w.r.t $sp, then every entry must be updated every time the
stack size changes!
■ Exercise:
Modify procCallsProg1Modified.asm to use a frame pointer
■ Observe that SPIM names register 30 as s8 rather than fp. Of course,
you can use it as fp, but make sure to initialize it with the same
value as sp, i.e., 7fffeffc.
61
• Section 2.10:
MIPS Addressing for 32-bit Immediates and
Addresses
62
MIPS Addressing Modes
63
Overview of MIPS
■ Simple instructions – all 32 bits wide
■ Very structured – no unnecessary baggage
■ Only three instruction formats
op rs rt rd shamt funct
R
I op rs rt 16 bit address
op 26 bit address
J
64
Summarize MIPS:
65
Summary
■ Instruction complexity is only one variable
■ lower instruction count vs. higher CPI / lower clock rate
■ Design Principles:
■ simplicity favors regularity
■ smaller is faster
■ good design demands compromise
■ make the common case fast
66
Acknowledgement