380 likes | 582 Views
Computer Architecture Instruction Set Architecture. Lynn Choi School of Electrical Engineering. Information Representation. Digital information Instruction (code) A program contains both instructions and data Instruction contains opcode and operands Data Text (alphanumeric codes)
E N D
Computer ArchitectureInstruction Set Architecture Lynn Choi School of Electrical Engineering
Information Representation • Digital information • Instruction (code) • A program contains both instructions and data • Instruction contains opcode and operands • Data • Text (alphanumeric codes) • ASCII (7b) – Stored in a byte (0 is padded to MSB) • Unicode (UTF-8) – Up to 6 bytes, backward compatible with ASCII • Numbers • Integer: unsigned, sign-magnitude, 2’s 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, H.264
Alphanumeric Codes • Handle text of letters and numbers • 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 (UTF-8) • Variable-length multi-byte character encodings for Unicode • Up to 6 bytes (31 bits of encodings) for alphanumeric codes • Each byte is represented by one to four bytes • Backward compatible with ASCII • 1,112,064 code points, • Represent the symbols and ideographs of the world's languages
Machine Language • Programming language • High-level programming languages • Procedural languages: C, PASCAL, Fortran, Basic • Object-oriented languages: C++, Java • Functional languages: Lisp, Scheme • Assembly programming languages: symbolic machine languages • Machine language: binary codes (1’s and 0’s) • Translator • Compiler • Translates high-level language programs into machine language programs • Interpreter • Translates and executes programs directly • Example: Java virtual machine (JVM) • Assembler • Translates symbolic assembly language programs into machine language programs
Compilation 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
Compiler • 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 Source Program Compiler Target Program
Lexical Analyzer Source Program Tokens Syntax Analyzer Tree Intermediate Code Generator I.C. Code Optimizer O.C. Target Code Generator Object Program Compiler Phases I.C. : Intermediate Code O.C. : Optimized Code
Front-End Source Programs IC Back-End Object Programs Compiler Structure • - Front-End : language dependent part • - Back-End : machine dependent part
Commercial Compilers Basic program Fortran program C program C Compiler C Compiler Basic Compiler Basic Compiler … (Java, Lisp, Ada, Pascal …) Fortran Compiler Fortran Compiler ISA x86 codes PowerPC codes Sparc, ARM, MIPS … … IBM PC Mac A wide range of source languages A wide range of target languages
Machine State • Registers • CPU internal storage to store data • 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 • 32 bit MIPS processor has 32 registers each of which is 32-bits wide • FP registers • A set of floating point registers • PC (program counter) • A special register that has the address of the next instruction • Memory • A large, single dimensional array with the address starting at 0 • Store instructions 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.
Data 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 • Each byte has an address. • Alignment • Objects must start at addresses that are multiple of their object 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)
Machine 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
Instruction 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 and these addresses are called ports (e.g. network port, printer port, etc.) • Control transfer instructions (branch instructions) • Change the program control flow • Determine the address of the next instruction • Unconditional jumps and conditional branches • EX) JUMP, CALL, RETURN, BEQ
Instruction 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 I-type op rs rt address
MIPS 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
MIPS 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
000000 0 01001 9 01010 10 01000 8 00000 0 100000 32 hex MIPS 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
Procedure 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 (PC+4) in $ra and jump to the callee • Acquire the storage needed for the callee • Allocate stack frame • 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
Stack • Stack frame (activation record) of a procedure • Store variables local to a procedure • Procedure’s saved registers (arguments, return address, saved registers, local variables) • Stack pointer : points to the top of the stack • Frame pointer : points to the bottom of the topmost stack frame
MIPS 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
MIPS Example : Recursion int fact (int n) { if (n <2) 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 < 2, then $t0 = 1 beq $t0, $zero, L1 # if n >=2, 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
Life 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 function calls.
Life 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
Why Linkers? • Problems: • Efficiency: • Time: small change requires complete recompilation • Space: executable file contains 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
m.c a.c Translators Translators Separately compiled relocatable object files m.o a.o Linker (ld) Executable object file (contains code and data for all functions defined in m.c and a.c) p A Better Scheme Using a Linker
Linker • 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)
Source files main2.c vector.h Translators (cpp, cc1, as) Static libraries libvector.a libc.a Relocatable object files printf.o and any other modules called by printf.o main2.o addvec.o Linker (ld) Fully linked executable object file p2 Linking with Static Libraries
Object 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
MIPS 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)
Exercises and Discussion • What is memory-mapped IO? • What are memory spills and fills? • What is an activation record?
Homework 2 • Read Chapter 3 • Exercise • 2.1 • 2.4 • 2.6 • 2.12 • 2.15 • 2.19