Professional Documents
Culture Documents
Assembly Language
Lecture
Instruction Set Architecture (ISA
Dr Hashim Al
Fall - 2021
1
3
Introduction
Software
• Computing Platform
• Hardware — To execute program
Application
• Gates: NOR, NAND
Digital Circuits
• Low Level Languages [Machine Level]
• A functional contract
Application
OS
• All ISAs similar in high-level ways
Compiler Firmware
• But many design choices in details
CPU I/O
• Good ISA…
Memory
• Enable high performance
Digital Circuits
• Compatibility is a powerful force
Gates & Transistors
• Tricks: binary translation, ISAs Hardware
3 𝝻
What Constitutes ISA?
• Main Features: Operations:
• How many?
• Location
• How to specify?
}
• How addressees are speci ed
Instruction Format:
• Size
fi
The Big Picture
• Assembly Language
5
Software Layers
• Assembly language
translated to machine
language by assembler.
6
Basic ISA Classes
• Memory to Memory Machines
• Can access memory directly in instructions: e.g., Mem[0] = Mem[1] + 1
• Memory is big (need lots of address bits in code —> large code)
• Architectural Registers
• registers can hold temporary variables
7
ffi
Basic ISA Classes (cont’d)
• Accumulator (1 register):
• Comparison:
• Bytes per instruction? Number of Instructions? Cycles per instruction?
9
fi
Comparing Number of Instructions
• Code sequence for C = A + B for four classes of instruction sets:
10
General Purpose Register Machines Dominate
• Literally all machines use general purpose registers
• Advantages of registers
11
ffi
Example: MIPS Assembly Language Notation
• Generic
• Addition
• Subtraction
• f=(g+h)-(i+j)
12
Instruction Set De nition (Programming Model)
Objects = architected entities
Operations = instruction types
= machine state
• Data operation
• Registers • Arithmetic (add, multiply, subtract,
• General purpose
divide, etc.)
• Special purpose (e.g. program • Logical (and, or, xor, not etc.)
• Memory locations
• Load (memory —> register)
2n-1
• Instruction sequencing
• Branch (conditional, e.g. less than,
greater than, equal)
• Jump (unconditional)
13
fi
Registers and Memory (MIPS)
• 32 registers provided
• R0 .. R31
14
Memory Organization
• Viewed as a large, single-dimension array, with an address.
• Bytes are nice, but most data items use larger "words"
15
Memory Organization
• Bytes are nice, but most data items use larger “words"
• 32-bit computer
• 232 bytes with byte addresses from 0 to 232-1
17
Programmable Machine
• Computing Machine
• Input — Code + Data
Instruction Format/Decoding
Operand Fetch — Addressing Modes
Operation Codes and Data Types
Addressing Modes
Instruction Sequencing
20
Executing an Assembly Instruction
• Program Counter holds the
instruction address
21
fl
fi
fi
fi
Register File Program Execution
Register File Memory
4 bytes wide
R0 0 Program Counter
R1 0x00000000 0x00 add R4, R0, R0
R2
R3 00000006 Instruction Register 0x04 sub R1, R3, R4
R4 00000004 0 add R4, R0, R0 0x08
……
R30
R31
22
Register File Program Execution
Register File Memory
4 bytes wide
R0 0 Program Counter
R1 00000006 0x00 add R4, R0, R0
0x00000004
R2
R3 00000006 Instruction Register 0x04 sub R1, R3, R4
R4 00000000 sub R1, R3, R4 0x08
……
R30
R31
23
Register File Program Execution
Register File Memory
4 bytes wide
R8 Program Counter
R9 0x00 add R8, R17, R18
R16 ???
R17 0x00000002 Instruction Register 0x04 add R9, R19, R20
R18 0x00000003 0x08 sub R16, R8, R9
R19 0x00000001
R20 0x00000004 Example:
… f=(g+h)-(i+j)
R16 == f
R17 == g
R18 == h
R19 == i
R20 == j
24
Accessing Data from Memory
• ALU generated address
25
Memory Operations - Load
Register File Memory
4 bytes wide
R0 Program Counter
R1 0x00000004
0x00000000 0x00 addi R5, R0, 0x14
R2
R3 Instruction Register 0x04 lw R6, 0 (R5)
R4 lw R6,
addi 0(R5)
R5, R0, 0x14 0x08
R5 0x00000014
R6 0x12345678 ➡Loading data from Memory
0x0C
… R6 <— mem[0x14]
Assume &A = 0x14
0x10
• addi: adds 16-bit constant to source register
➡Address can be computed by adding an 0x14 0x12345678
o set to register
LW R6, 0(R5)
R6 <— mem[R5+0]
26
ff
Memory Operations - Store
Register File Memory
4 bytes wide
R0 Program Counter
R1 0x00000008 0x00 addi R5, R0, 0x18
R2
R3 Instruction Register 0x04 addi R6, R0, 200
R4 sw R6, 0 (R5) 0x08 sw R6, 0 (R5)
R5 0x00000018
R6 0x000000C8 ➡Storing data to memory works 0x0C
… essentially the same way
Field Description
op Operation of the instruction
rs First register source operand
rt Second register source operand
rd Register destination operand Why are there 5
shamt Shift amount bits in the register
funct Function (select type pf operation) eld??
• add = 3210
• sub = 3410
28
fi
fi
MIPS Instruction Formats
• More than more than one format for instructions, usually
• Di erent kinds of instructions need di erent kinds of elds, data
Questions:
I-format: How big an immediate can you have?
(What is the maximum value?)
J-format: How far can you jump in instructions?
29
ff
ff
Constants
• Small constants are used quite frequently (50% of operands)
e.g., A = A + 5;
B = B + 1;
• Solutions? Why not....
C = C - 18;
• ...just put 'typical constants' in memory and load them.
• ...just create hard-wired registers (like $zero) for constants like one.
31
MIPS Machine Language
32
Loading Immediate Values
• What’s the largest immediate value that can be loaded into a register?
33
Control (Instruction Sequencing)
• MIPS unconditional branch instructions:
• j label
• Example:
Go here if
$4 != $s5
Go here
always
• OK, so with these--Can you build a simple for(...) {...} loop?
34
Branch Instructions
• They exist because we need to change the program counter
• bne (branch not equal) compares regs and branches if regs “!=”
35
Branch Instructions
• Branch instructions are used to implement C-style loops
36
Addresses in Branches and Jumps
• Instructions
• Formats
37
fi
Addresses in Branches and Jumps
• Instructions
• Formats
38
fi
fi
Branch Instructions
• Example
39
Generic Examples of Instruction Format Widths
40
Summary of Instruction Formats
• If code size is most important, use variable length instructions
41
fi
Observation
• “Simple” computations, movements of data, etc., are not always “simple” in terms of a
single, obvious assembly instruction
• One options is to try to “anticipate” every such computation, and try to provide an assembly
instruction for it
(Complex Instruction Set Computing = CISC)
• PRO: assembly programs are easier to write by hand
• CON: hardware gets really, really complicated by instructions used very rarely.
• PRO: hardware and compiler become easier to design, cleaner, easier to optimise for
speed, performance
42
Summary
• Architecture = what’s visible to the program about the machine
• Not everything in the deep implementation is “visible”
43
MIPS Instruction Set Architecture
(Review)
44
MIPS — Key Points
• MIPS is a general-purpose register, load-store, xed-instruction-length
architecture.
• MIPS is optimised for fast pipelined performance, not for low instruction
count.
• Smaller is faster
45
MIPS Instruction Formats
46
MIPS — Accessing the Operands
47
MIPS Instruction Set
48
MIPS — Instructions / Operations
• Arithmetic • Data movement
• ADD, SUB etc.
• Load, Store
• Relational
• Less than etc.
• Branch/jump
• Conditional Branch
• Unconditional Jump
49
MIPS ISA Features — Addressing
Purpose Addressing modes
• Operand sources
• Immediate
• Result destinations
• Register
• PC relative
• (pseudo) Direct
• Register indirect
50