Title: Computer Architecture Instruction Set Architecture
1Computer Architecture Instruction Set
Architecture
- Lynn Choi
- School of Electrical Engineering
2Information Representation
- Digital information
- Program (code)
- A sequence of instructions
- Instruction contains opcode and operands
- Data
- Text (alphanumeric codes)
- ASCII (7b) Stored in a byte (0 is padded to
MSB) - Unicode (16b)
- Numbers
- Integer unsigned, sign-magnitude, 2s complement
- Floating point sign-significand-exponent
- Logical data n 1-bit Boolean data
- Address the location of memory (data or
instruction address) - Multimedia
- Image 1024 x 768 pixels (1b 3B per pixel)
- Video, audio MPEG
3Alphanumeric Codes
- Handle text of letters and numbers
- Set of elements include 10 digits, 26 letters,
special characters - 36 64 letters if only capital letters need 6
bits - 64 128 letters if upper/lower letters need
7 bits - ASCII character code
- American standard code for information exchange
- Standard binary code is ASCII (table 1.4)
- ASCII contain 94 printable chars 34 control
chars - Unicode
- A new standard for 16-bit (2 byte) alphanumeric
codes - Referred to as Unicode/10646
- 16 bits provide 65,536 code words,
- Represent the symbols and ideographs of the
world's languages
4(No Transcript)
5Machine Language
- Programming language
- High-level programming languages
- C, C, PASCAL, Java, Lisp, Fortran, etc.
- Assembly programming languages
- Symbolic machine languages
- Machine language
- Binary codes (1s and 0s)
- Translator
- Compiler
- Translates high-level language programs into
machine language programs - Interpreter
- Translates and executes programs directly
- Assembler
- Translates symbolic assembly language programs
into machine language programs
6Compilation Process
Source program
Expands macros into the source program
Preprocessor
Expanded Source Program
Compiler
Assembly Program
Assembler
Relocatable code
Libraries, relocatable object files
Linker Loader
Target program
7Compiler
- Compiler
- A program that translates a source program
(written in language A) into an equivalent target
program (written in language B) - Source program
- Usually written in high-level programming
languages (called source language) such as C,
C, Java, FORTRAN - Target program
- Usually written in machine languages (called
target language) such as x86, Alpha, MIPS, SPARC,
or ARM instructions - What qualities do you want in a compiler?
- Generate correct code
- Target code runs fast
- Compiler runs fast
- Support for separate compilation, good
diagnostics for errors
Compiler
Target Program
Source Program
8Compiler Phases
I.C. Intermediate Code O.C. Optimized Code
9Compiler Structure
- Front-End language dependent part Back-End
machine dependent part
10Commercial Compilers
Basic program
Fortran program
C program
C Compiler
C Compiler
Basic Compiler
Basic Compiler
(Java, Lisp, Ada, Pascal )
Fortran Compiler
Fortran Compiler
x86 codes
PowerPC codes
ISA
Sparc, ARM, MIPS
IBM PC
Mac
A wide range of source languages A wide range of
target languages
11Machine State
- Registers
- CPU internal storage to store data fetched from
memory - The fastest memory in the memory hierarchy
- Can be read or written in a single cycle
- Arithmetic and logic operations are usually
performed on registers (RISC architecture) - GPR (general purpose registers)
- A set of integer registers used for data
transfers from/to memory - Example MIPS ISA has thirty two 32-bit (i.e. 32
flip-flops) registers - PC (program counter)
- A special register that has the address of the
next instruction to be fetched/executed. - Memory
- A large, single dimensional array, with the
address starting at 0 - Store programs and data
- Data transfer instructions
- Load to move data from memory to a register
- Store to move data from a register to memory
- Address
- To access a data item in memory, an instruction
must supply an address.
12Data Size, Alignment
- Data size
- Word the basic unit of data for computation
same as the size of a register (in bits) - 32b for 32b ISA, 64b for 64b ISA
- Double word 64b data, Half word 16b data, Byte
8b data - Load/store instructions can designate data sizes
transferred ldw, lddw, ldhw, ldb - Byte addressability
- Most ISA addresses individual bytes
- Therefore, addresses of sequential words differ
by 4 - Alignment
- Objects must start at addresses that is multiple
of their size - Words must always start at addresses that are
multiples of 4 - Object addressed Aligned addresses Misaligned
addresses - Byte 0, 1, 2, 3, 4, 5, 6, 7 Never
- Half Word 0, 2, 4, 6 1, 3, 5, 7
- Word 0, 4 1, 2, 3, 5, 6, 7
- Double Word 0 1, 2, 3, 4, 5, 6, 7
- Misaligned memory access may take longer
(multiple aligned memory references) or may not
be allowed (misaligned memory access fault)
13Machine Instruction
- Elements of a machine instruction
- Opcode specifies the operation to be performed
- EX) ADD, MULT, LOAD, STORE, JUMP
- Operands specifies the location of data
- Source operands (input data)
- Destination operands (output data)
- The location can be
- Memory specified by a memory address EX) 8(R2),
x1004F - Register specified by a register number R1
- Next instruction reference
- Specifies where to fetch the next instruction
- If not specified, the next instruction to be
fetched immediately follows the current
instruction (implicit) PC lt- PC 4 - Or specified by a separate instruction
- These instructions are called control transfer or
branch instructions
14Instruction Types
- Arithmetic and logic instructions
- Performs actual computation on operands
- EX) ADD, MULT, SHIFT, FDIVIDE, FADD
- Data transfer instructions (memory instructions)
- Move data from/to memory to/from registers
- EX) LOAD, STORE
- Input/Output instructions are usually implemented
by memory instructions (memory-mapped IO) - IO devices are mapped to memory address space
- Control transfer instructions (branch
instructions) - Change the program control flow
- Specifies the next instruction to be fetched
- Unconditional jumps and conditional branches
- EX) JUMP, CALL, RETURN, BEQ
15Instruction Format
- Op Opcode, basic operation of the instruction
- Rs 1st source register
- Rt 2nd source register
- Rd destination register
- shamt shift amount
- funct Function code, the specific variant of the
opcode - Used for arithmetic/logic instructions
- Rs base register
- Address /- 215 bytes offset (or also called
displacement) - Used for loads/stores and conditional branches
6 5 5 5
5 6
op
rs
rt
rd
shamt
funct
R-type
6 5 5
16
op
rs
rt
address
I-type
16MIPS Addressing Modes
- Register addressing
- Address is in a register
- Jr ra
- Base addressing
- Address is the sum of a register and a constant
- Ldw s0, 100(s1)
- Immediate addressing
- For constant operand
- Add t1, t2, 3
- PC-relative addressing
- Address is the sum of PC and a constant (offset)
- Beq s0, s1, L1
- Pseudodirect addressing
- Address is the 26 bit offset concatenated with
the upper bits of PC - J L1
17MIPS Instruction formats
- Arithmetic instructions
- Data transfer, conditional branch, immediate
format instructions - Jump instructions
R-type
6 5 5 5
5 6
op
rs
rt
rd
shamt
funct
I-type
6 5 5
16
op
rs
rt
address/immediate
J-type
6 26
op
address
18MIPS Instruction Example R-format
- MIPS Instruction
- add 8,9,10
Decimal number per field representation
Binary number per field representation
hex representation 012A 4020hex
decimal representation 19,546,144ten
- Called a Machine Language Instruction
19(No Transcript)
20(No Transcript)
21Procedure Call Return
- Steps of procedure call return
- Place parameters in a place where the callee can
access - a0 - a3 four argument registers
- Transfer control to the callee
- Jal callee_address Jump and link instruction
- put return address (PC4) in ra and jump to the
callee - Acquire the storage needed for the callee
- Perform the desired task
- Place the result value in a place where the
caller can access - v0 - v1 two value registers to return values
- Return control to the caller
- Jr ra
22Stack
- Stack frame (activation record) of a procedure
- Store variables local to a procedure
- Procedures saved registers (arguments, return
address, saved registers, local variables) - Stack pointer points to the top of the stack
- Frame pointer points to the first word of the
stack frame
23MIPS Memory Allocation
24MIPS Register Convention
25MIPS Example Procedure
- int leaf_example (int g, int h, int i, int j)
- int f
- f (g h) ( i j)
- return f
- Assembly code
- leaf_example
- sub sp, sp, 8
- sw t1, 4(sp) save register t1,
t0 onto stack - sw t0, 0(sp)
- add t0, a0, a1 t0 g h
- add t1, a2, a3 t1 i j
- sub v0, t0, t1 v0 (g h) (i j)
- lw t0, 0(sp) restore t0, t1 for
caller - lw t1, 4(sp)
- add sp, sp, 8
- jr ra
26MIPS Example Recursion
- Int fact (int n)
- if (n lt2) return 1
- else return n fact (n 1)
- Assembly code
- fact addi sp, sp, -8 adjust stack pointer
for 2 items - sw ra, 4(sp) save return
address and argument n - sw a0, 0(sp)
- slt t0, a0, 2 if n lt 2, then t0 1
- beq t0, zero, L1 if n gt2, go to L1
- addi v0, zero, 1 return 1
- addi sp, sp, 8 pop 2 items off stack
- jr ra
- L1 addi a0, a0, -1 a0 n - 1
- jal fact call fact(n 1)
- lw a0, 0(sp) pop argument n
and return address - lw ra, 4(sp)
- addi sp, sp, 8
- mul v0, a0, v0 return n
fact(n 1) - jr ra
27Life and Scope of an Object
- Life vs. scope
- Life of an object determines whether the object
is still in memory (of the process) whereas the
scope of an object determines whether the object
can be accessed at this position - It is possible that an object is live but not
visible. - It is not possible that an object is visible but
not live. - Local variables
- Variables defined inside a function
- The scope of these variables is only within this
function - The life of these variables ends when this
function completes - So when we call the function again, storage for
variables is created andvalues are
reinitialized. - Static local variables - If we want the value to
be extent throughout the life of a program, we
can define the local variable as "static." - Initialization is performed only at the first
call and data is retained between func calls.
28Life and Scope of an Object
- Global variables
- Variables defined outside a function
- The scope of these variables is throughout the
entire program - The life of these variables ends when the program
completes - Static variables
- Static variables are local in scope to their
module in which they are defined, but life is
throughout the program. - Static local variables static variables inside a
function cannot be called from outside the
function (because it's not in scope) but is alive
and exists in memory. - Static variables if a static variable is defined
in a global space (say at beginning of file) then
this variable will be accessible only in this
file (file scope) - If you have a global variable and you are
distributing your files as a library and you want
others not to access your global variable, you
may make it static by just prefixing keyword
static
29Why Linkers?
- Problems
- Efficiency
- Time small change requires complete
recompilation - Space executable file can contain only code that
are actually used rather than the entire code - Modularity hard to share common functions (e.g.
printf) - Solution
- Separate compilation use linker
m.c
ASCII source file
Translator
Binary executable object file (memory image on
disk)
p
30A Better Scheme Using a Linker
31Linker
- Linking
- The process of collecting and combining various
pieces of code and data into a single file that
can be loaded (copied) into memory and executed. - Performs the following tasks
- Merges multiple relocatable object files into a
single executable object file that can loaded and
executed by the loader. - Resolves external references
- External reference reference to a symbol defined
in another object file. - Relocates symbols from their relative locations
in the .o files to new absolute positions in the
executable. - Linking time
- Can be done at compile time, i.e. when the source
code is translated (Static Linker) - Or, at load time, i.e. when the program is loaded
into memory or event at runtime (Dynamic Linker)
32Linking with Static Libraries
33Object Files
- Relocatable object file
- Contains binary code and data in a form that can
be combined with other relocatable object files
at compile time to create an executable object
file - Compilers and assemblers generate relocatable
object files - Executable object file
- Contains binary code and data in a form that can
be copied directly into memory and executed - Linkers generate executable object files
- Shared object file
- A special type of relocatable object file that
can be loaded into memory and linked dynamically,
at either load time or at run time
34MIPS Object File Format
- An object file (in UNIX) contains six sections
- Object file header
- Size and position of all the sections of the file
- Text
- Code contains unresolved references
- Data
- Binary data
- Relocation information
- Identifies instructions and data that depends on
the location where the program is loaded - Symbol table
- External references (labels) and their
information - Information about functions and global variables
- Debugging information
- How the module is compiled (for a debugger to
associate the machine instructions with C source
files and make data structures readable)
35(No Transcript)
36Linking ??
37Exercises and Discussion
- What is memory-mapped IO?
- What are memory spills and fills?
- What is an activation record?
38Homework II
- 2.2
- 2.6
- 2.15
- 2.19
- 2.20
- 2.30
- 2.31
- Read Chapter III
- Due date 4/7 (Tue)