Professional Documents
Culture Documents
EE182: Computer Org. & Design Handout #05
EE182: Computer Org. & Design Handout #05
Overview
n n
Administrative Announcements Performance Review Languages Arithmetic operators Registers Memory access
n n
Administrative
n
Administrative (cont)
n
Send to majordomo@lists.stanford.edu Body should contain subscribe ee182 Yesterday, Wednesday 3:15-4:05 in Gates B03 Available via Stanford Online and on tape in Terman Greg Larchev
Mondays 1:30 3:30 pm Sweet Hall Tuesdays 1:00 3:00 pm Packard 106
Section information
n n
n n n n n n
TA Office Hours
n
Alex Liu
Sundays 7:00 9:00 pm Sweet Hall Thursdays 1:00 3:00 pm Packard 109
Problems: 1.50, 2.10-2.12, 2.18-2.23, 2.26-2.29, 2.41, 2.44 Due Tuesday, 10/10 5:00 pm Turn in at lecture or to Gates 227 May work in groups of up to two One late day for the quarter, so use wisely Check the FAQ for common questions/answers Send E-mail with questions to: ee182-help@lists.stanford.edu
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #3
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #4
Measuring Time
n
Execution Time =
n
Improving performance
n n n
Designers have to balance the length of each cycle and the number of cycles required
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #6
Fountain/Autumn 00-01
A Language
n n n
Different classes of instructions usually take different numbers of cycles If you know the number of instructions of each class of instruction
Clock Cycles = (CPI i Ci )
i =1 n
Computers speaka language Programming languages provide a means for symbolically expressing data processing Each language has a well defined syntax and grammar
where CPIi is the CPI for the class of instructions and Ci is the count of that type of instructions To compute the average CPI use
n Instruction Counti CPI = CPI i Instruction Count i =1
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #7 Fountain/Autumn 00-01 EE182 Lecture #3 Slide #8
Programming Languages
n
Assembly Languages
n n n
There are many programming languages, but they usually fall into two categories
n
High-level languages are usually machine-independent and instructions are often more expressive
C, Fortran, Pascal, Basic
Low-level languages are usually machine-specific and offer much finer-grained instructions that closely match the machine language of the target processor
Assembly languages for MIPS, x86, SGI, HP-PA
Assembly languages are text representations of the machine language One statement represents one machine instruction Abstraction layer between high-level programs and machine code
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #9
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #10
Machine Language
n n n n n
Machine language is the native language of the computer The words are called instructions The vocabulary is the instruction set Bit representation of machine operations to be executed by the hardware We will focus on the MIPS instructions
n n
Compiler
Assembly Language Program
Assembler
Machine Language Program Control Signal Specification
Other RISC-based instruction sets are similar Different instruction sets tend to share a lot of commonalities since they function similarly
Machine Interpretation
High/Low on control lines
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #11
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #12
hello.c
.file "hello.c"
gcc2_compiled.: .section ". rodata" .align 8 .LLC0: .asciz .section "Hello world! \n ".text" .align 4 .global main .LLfe1: .LL1:
nop
ret restore
.size .ident
hello.s (Part 1)
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #13 Fountain/Autumn 00-01
hello.s (Part 2)
EE182 Lecture #3 Slide #14
Assembly Instructions
n
Arithmetic Operators
n
n n
Simple, fixed formats make hardware implementation simpler ( simplicity favors regularity ) On most architectures, there are no restrictions on elements appearing more than once
Since assembly code can be difficult to read, the common practice is to use # for comments
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #15
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #16
Complex Operations
n
Data Representation
n n
n n n
n n
Compilers often use temporary variables when generating code Notice all of the comments!
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #17
Characters one byte, usually using ASCII Integer numbers stored in 2 complement s which we will review in the next chapter Floating point uses a mantissa and exponential (m 2e), also covered in the next chapter
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #18
Data Storage
n n n
Register Organization
n n
In high-level programs we store data in variables In practice, where is this data stored? The answer is that it can be stored in many different places
n n n n
Register organization is one of the defining aspects about a particular processor architecture Three basic mechanisms for operators/operands
n
n n
Accumulator architecture which uses a single register for one of the sources and the destination (ex. 8088) Stack operands are pushed and popped (ex. Java) General Purpose a limited number of registers used to store data for any purpose (ex. most systems today)
n n
A register is a small high-speed block of memory that holds data We will focus in this course on general purpose
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #20
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #19
Accumulator Example
n
Stack Example
n
In an accumulator-based architecture it is
load addressB add addressC store addressA
In Java bytecode it is
iload_1 iload_2 iadd istore_0 # # # # Loads b onto the stack Loads c onto the stack Adds and puts result on stack Stores into a
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #21
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #22
MIPS Architecture
n
When using General Purpose Registers (GPRs), data can access in different ways
n
Load-Store (L/S) data is loaded into registers, operated on, and stored back to memory (ex. all RISC instruction sets)
Hardware for operands is simple Smaller is faster since clock cycle can be kept fast Emphasis is on efficiency
Each register is 32 bits long, called a word The MIPS has 32 general purpose registers (some reserved for different purposes) MIPS also has 32 floating point only registers, which we will also discuss later
Memory-Memory operands can use memory addresses as both a source and a destination (ex. Intel)
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #23
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #24
Register Naming
n n
Using registers
n
Registers 0-31 are named using a $<num> By convention, we give them names:
n n n n
Goals
n n
$zero contains the hardwired value 0 $s0, $s1, $s7 are for save variables $t0, $t1, $t9 are for temp variables The others will be introduced as we get to them
Keep data in registers as much as possible Always use data still in registers if possible Finite number of registers available
Spill registers to memory when all registers in use Data must also be stored across procedures (covered next lecture)
Issues
n
n n
Compilers use these conventions to make linking a smooth process Unlike variables, there are a fixed number of data registers ( smaller is faster )
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #25
Arrays
Data is too large to store in registers Need to compute index
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #26
Arithmetic Operators: II
n
Complex Operations: II
n
Consider the C operation for addition where the variables are in $s0-$s2 respectively
a = b + c;
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #27
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #28
Constants
n n
Constant Example
n
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #29
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #30
n n
Data transfer instructions are used to move data to and from memory in load-store A load operation moves data from memory to a register and a store operation moves data from a register to memory One word at a time is loaded from memory to a register on MIPS using the lw instruction Load instructs have three parts
1. 2. 3. Operator name Destination register Base register address and constant offset
lw dst, offset(base)
n
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #31
Memory Access
n n n n
All memory access happens through loads and stores Aligned words, halfwords, and bytes Floating Point loads and stores for accessing FP registers Displacement based addressing
Immediate
Registers
Memory
Base
Fountain/Autumn 00-01
+
EE182 Lecture #3
Slide #33
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #34
Accessing Arrays
n n n
Array Example
n
Arrays are really pointers to the base address in memory Use offset value to indicate which index Remember that addresses are in bytes, so multiply by the size of the element
n n n n
Consider an integer array where A is the base address The data to be accessed is at index 5 Then the address from memory is A + 5 * 4 Unlike C, assembly does not handle pointer arithmetic for you!
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #35
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #36
Storing Data
n n
Storing data is just the reverse and the instruction is nearly identical Use the sw instruction to copy a word from the source register to an address in memory
sw src, offset(base)
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #37
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #38
Storing to an Array
n
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #39
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #40
MIPS Load/Store
Instruction store word store half store byte Example sw $1, 8($2) sh $1, 6($2) sb $1, 5($2) sf $f1, 4($2) lw $1, 8($2) lh $1, 6($2) lhu $1, 6($2) lb $1, 5($2) lbu $1, 5($2) lf $f1, 4($2) Meaning Mem[8+$2]=$1 Mem[6+$2]=$1 Mem[5+$2]=$1 Mem[4+$2]=$f1 $1=Mem[8+$2] $1=Mem[6+$2] $1=Mem[8+$2] $1=Mem[5+$2] $1=Mem[5+$2] $f1=Mem[4+$2] Comments Store word Stores only lower 16 bits Stores only lowest byte Store FP word Load word Load half; sign extend Load half; zero extend Load byte; sign extend Load byte; zero extend Load FP register
store float load word load halfword load half unsign load byte load byte unsign load float
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #41
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #42
Memory Addressing
n n n
Byte Ordering
n
Almost all architectures support byte addressing as well as word addressing Different architectures have different ways of ordering the bits, known as the byte order Some architectures limit the way data is stored so as to improve efficiency
Big Endian the bigend comes first and the most significant bit (MSB) is the lowest memory address Little Endian the little endcomes first and the least significant bit (LSB) is the first address (ex. Intel) Some systems such as MIPS and PowerPC can do both, but are primarily big endian
3 msb 0 Big Endian byte 0 1 2 3 2 1 Little Endian byte 0 0 lsb
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #43
Fountain/Autumn 00-01
EE182 Lecture #3
Slide #44
Alignment Restrictions
n n
In MIPS, data is required to fall on addresses that are even multiples of the data size Historically
n n n
Early machines (IBM 360 in 1964) required alignment Removed in 1970s since hard for programmers RISC reintroduced due to effect on performance
0 Aligned 1 2 3
n n
Not Aligned
Fountain/Autumn 00-01 EE182 Lecture #3 Slide #45 Fountain/Autumn 00-01 EE182 Lecture #3 Slide #46