ec 413 computer organizationascslab.org/courses/ec413/lectures/review-i.pdfthe significand always...

60
Department of Electrical & Computer Engineering EC 413 Computer Organization Prof. Michel A. Kinsy Review I

Upload: hacong

Post on 29-Mar-2018

217 views

Category:

Documents


2 download

TRANSCRIPT

Department of Electrical & Computer Engineering

EC 413 Computer Organization

Prof. Michel A. Kinsy

Review I

Department of Electrical & Computer Engineering

Computing: The Art of Abstraction

Algorithm

Register-Transfer Level (RTL)

Application

Instruction Set Architecture (ISA) Operating System/Virtual Machine

Microarchitecture

Devices

Programming Language

Circuits

Physics

Department of Electrical & Computer Engineering

Bit & Byte §  A bit is the smallest unit of information

•  It represents one 2-way decision or a 2-way choice •  yes / no, true / false, on / off ... •  Abstraction of all of these is represented as 0 or 1

§  A single digit with one of two values §  Binary digit à bit

§  All information in a computer is stored and processed as bits •  High voltage/low voltage, current flowing/not

flowing §  A byte is 8 bits that are treated as a unit

Department of Electrical & Computer Engineering

Binary Number System §  Binary numbers are short-hands for sums of

powers of 2 •  11011 = 1 x 16 + 1 x 8 + 0 x 4 + 1 x 2 + 1 x 1 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20

§  Most of us count in "base 10", using powers of 10

§  Computer counts in "base 2", using power of 2

Department of Electrical & Computer Engineering

Binary arithmetic §  It works just like decimal arithmetic

Addition

0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10

Subtraction 0 - 0 = 0 0 - 1 = -1 1 - 0 = 1 1 - 1 = 0

Small Problem We will see how to solve it later in the lecture

Department of Electrical & Computer Engineering

Converting binary to decimal §  Converting to decimal, so we can use

polynomial evaluation •  101101012 =1x27 + 0x26 + 1x25 + 1x24 + 0x23 + 1x22 + 0x21 + 1x20 =128 + 32 + 16 + 4 + 1 = 18110

Department of Electrical & Computer Engineering

Converting decimal to binary §  Divide by target base (2 in this case) §  Remainders become digits in the new

representation §  Digits produced in right to left order §  Quotient is used as next dividend §  Stop when the quotient becomes zero, but use

the corresponding remainder

Department of Electrical & Computer Engineering

Converting decimal to binary  97 ÷ 2 à quotient = 48, remainder = 1 (LSB) 48 ÷ 2 à quotient = 24, remainder = 0 24 ÷ 2 à quotient = 12, remainder = 0 12 ÷ 2 à quotient = 6, remainder = 0 6 ÷ 2 à quotient = 3, remainder = 0 3 ÷ 2 à quotient = 1, remainder = 1 1 ÷ 2 à quotient = 0 (Stop) remainder = 1 (MSB)

Answer=11000012

Department of Electrical & Computer Engineering

Hexadecimal notation §  Binary number can be long and hard to read, so

hexadecimal numbers were introduced §  It combines 4 bits into a single digit, written in

base 16 §  A more compact and more readable

representation of the same information §  Hexadecimal number system uses the symbols

A, B, C, D, E, F for the digits 10, 11, 12, 13, 14, and 15, respectively

Department of Electrical & Computer Engineering

Numbers and Bases Decimal Base-10 Binary Base-2 Hexadecimal Base-16

0 00000 01 00001 12 00010 23 00011 34 00100 45 00101 56 00110 67 00111 78 01000 89 01001 910 01010 A11 01011 B12 01100 C13 01101 D14 01110 E15 01111 F16 10000 1017 10001 11

Department of Electrical & Computer Engineering

Signed Integer Representation §  There are three ways in which signed binary

numbers may be expressed: •  Signed magnitude •  One’s complement •  Two’s complement

§  In an 8-bit binary number, signed magnitude representation places the absolute value of the number in the 7 bits to the right of the sign bit

•  4 = 0 0000100 •  -4 = 1 0000100

Department of Electrical & Computer Engineering

One’s complement §  It amounts to little more than flipping the bits of a

binary number §  In an 8-bit binary number using One’s

complement •  4 = 0 0000100 •  -4 = 1 1111011

§  In one’s complement, as with signed magnitude, negative values are indicated by a 1 in the high order bit

•  Complement systems are useful because they eliminate the need for subtraction

Department of Electrical & Computer Engineering

Two’s complement §  To express a value in two’s complement: §  If the number is positive, convert it to binary and

it is done §  If the number is negative, find the one’s

complement of the number and then add 1 •  4 = 0 0000100 •  -4 = 1 1111100

Department of Electrical & Computer Engineering

Two’s complement §  With Two’s complement arithmetic §  Just add the two binary numbers §  Discard any carries emitting from

the high order bit §  Sum of 48 and – 19

•  19 in binary is 00010011 •  -19 in one’s complement is

11101100 •  -19 in two’s complement is

11101101 •  Answer is 29

0 0 1 1 0 0 0 0 1 1 1 0 1 1 0 1 = 0 0 0 1 1 1 0 1

Department of Electrical & Computer Engineering

Floating-Point Representation §  Computer systems use a form of scientific notation

for floating-point representation §  Numbers written in scientific notation have three

components: •  Sign Mantissa Exponent

+ 1.5 x 10-1

§  Computer representation of a floating-point number consists of three fixed-size fields:

Sign Exponent Significand

Department of Electrical & Computer Engineering

Floating-Point Representation

§  The one-bit sign field is the sign of the stored

value §  The size of the exponent field, determines the

range of values that can be represented §  The size of the significand determines the

precision of the representation

Sign Exponent Significand

Department of Electrical & Computer Engineering

Floating-Point Representation

§  The significand of a floating-point number is always preceded by an implied binary point

§  The significand always contains a fractional binary value

§  The exponent indicates the power of 2 to which the significand is raised

Sign Exponent Significand

Department of Electrical & Computer Engineering

Floating-Point Representation §  Express 3210 in the simplified 14-bit floating-

point representation §  32 = 25 = 1.0 x 25 = + 0.1 x 26

§  Exponent field = 1102 = 610 §  Significand field 1

§  How do we express fractional numbers 0.5 =2-1?

+ 00110 10000000

Department of Electrical & Computer Engineering

Floating-Point Representation §  Sum of 1210 and 1.2510 using the 14-bit floating-

point representation •  1210 = 0.1100 x 24 •  1.2510 = 0.101 x 21 = 0.000101 x 24 •  Thus, the sum is 0.110101 x 24

Department of Electrical & Computer Engineering

Memory Allocation §  There are two types of memory allocation

§  Static memory allocation: Memory is allocated at the start of the program, and freed when program exits §  Done by the compiler automatically (implicitly) §  Global variables or objects

§  Alive throughout program execution §  Can be access anywhere in the program

§  Local variables (inside a function) §  Memory is allocated when the function starts and freed when

the routine returns §  A local variable cannot be accessed from another function

Department of Electrical & Computer Engineering

Memory Allocation §  There are two types of memory allocation

§  Dynamic memory allocation deals with objects whose size can be adjusted depending on needs §  Dynamic – Done explicitly by programmer §  Programmer explicitly requests the system to allocate

memory and return starting address of memory allocated

§  This address can be used by the programmer to access the allocated memory

§  When done using memory, it must be explicitly freed

Department of Electrical & Computer Engineering

Application Side §  Higher-level languages

§  Allow the programmer to think in a more natural language and for their intended use

§  Improve programmer productivity Improve program maintainability

§  Allow programs to be independent of the computer on which they are developed §  Compilers and assemblers can translate high-level

language programs to the binary instructions of any machine

Department of Electrical & Computer Engineering

Application Side §  Higher-level languages

§  Allow the programmer to think in a more natural language and for their intended use

§  Improve programmer productivity Improve program maintainability

§  Allow programs to be independent of the computer on which they are developed

§  Emergence of optimizing compilers that produce very efficient assembly code

§  As a result, very little programming is done today at the assembler level

Department of Electrical & Computer Engineering

System Software Side §  System software

§  Operating system – supervising program that interfaces the user’s program with the hardware (e.g., Linux, MacOS, Windows) §  Handles basic input and output operations §  Allocates storage and memory §  Provides for protected sharing among multiple

applications

§  Compiler – translate programs written in a high-level language (e.g., C, Java) into instructions that the hardware can execute

Department of Electrical & Computer Engineering

Application Compiling Process §  C Language

Cprogram

compiler

assemblycode

assembler

objectcode libraryrou4nes

executable

linker

loader

memory

HumanReadable

MachineCode

Department of Electrical & Computer Engineering

Assembly Code §  Three types of statements in assembly language

§  Typically, one statement per a line 1. Executable assembly instructions

§  Operations to be performed by the processor 2. Pseudo-Instructions and Macros

§  Translated by the assembler into real assembly instructions

§  Simplify the programmer task 3. Assembler Directives

§  Provide information to the assembler while translating a program

§  Used to define segments, allocate memory variables, etc.

Department of Electrical & Computer Engineering

Assembly Code §  There are 3 main types of assembly instructions

§  Arithmetic - add, sub, mul, shifts, and, or, etc. §  Load/store §  Conditional - branches

§  Assembly language instructions have the format: §  [label:] mnemonic [operands] [#comment]

§  Label: (optional) §  Marks the address of a memory location §  Typically appear in data and text segments

Department of Electrical & Computer Engineering

Assembly Languages §  Assemblers:

§  Convert mnemonic operation codes to their machine language equivalents

§  Convert symbolic operands to their equivalent machine addresses

§  Build the machine instructions in the proper format

§  Convert the data constants to internal machine representations

§  Write the object program and the assembly listing

Department of Electrical & Computer Engineering

System Calls §  Programs do input/output through system calls §  To obtain services from the operating system §  Using the syscall system services §  In MIPS:

§  Load the service number in register $v0 §  Load argument values, if any, in registers $a0, $a1,

etc. §  Issue the syscall instruction §  Retrieve return values, if any, from result

registers

Department of Electrical & Computer Engineering

Big Endian – Little Endian §  Processors can order bytes within a word in two

ways §  Little Endian

§  Least significant byte stored at lowest byte address §  Intel IA-32, Alpha, AMD

§  Big Endian §  Most significant byte stored at lowest byte address §  SPARC, PA-RISC, IBM

Byte 3 Byte 0 Byte 1 Byte 2 Byte 3 32-bit Register

MSB LSB . . . . . . Byte 0 Byte 1 Byte 2

A A+3 A+2 A+1

Memory

Memory address

Byte 0 Byte 1 Byte 2 Byte 3 32-bit Register

MSB LSB . . . . . . Byte 0 Byte 1 Byte 2 Byte 3

A A+3 A+2 A+1

Memory

Memory address

Department of Electrical & Computer Engineering

Instruction Set Architecture (ISA) §  Instructions are the language the computer

understand §  Instruction Set is the vocabulary of that language §  It serves as the hardware/software interface

§  Defines data types §  byte, int, float, double, string, vector…

§  Defines set of programmer visible state §  Known as the programmer’s model of the machine

§  Defines instruction semantics (operations, sequencing) §  operand location: register, immediate, indirect, . . . §  add, sub, mul, move, compare, …

Department of Electrical & Computer Engineering

§  Instructions are the language the computer understand

§  Instruction Set is the vocabulary of that language §  It serves as the hardware/software interface

§  Defines instruction format (bit encoding) §  Number of explicit operands per instruction §  Operand location §  Number of bits per instruction §  Instruction length: fixed, short, long, or variable., …

§  Examples: MIPS, Alpha, x86, IBM 360, VAX, ARM, JVM

Instruction Set Architecture (ISA)

Department of Electrical & Computer Engineering

§  Four principles are used in designing instruction set architecture:

1. Simplicity favors regularity §  Total number of instructions in the instruction set

2. Smaller is faster §  Number of addressable registers §  Large number of registers increases access time

3. Good design demands good compromise §  Computer designer must balance the programmer’s

desire for more registers with the need to minimize access time

4. Make the common case fast

Instruction Set Architecture (ISA)

Department of Electrical & Computer Engineering

Instruction Set Architecture (ISA) §  Instructions can be divided into 3 classes

1. Data movement instructions §  Move data from a memory location or register to

another memory location or register without changing its form

§  Load—source is memory and destination is register §  Store—source is register and destination is memory §  lw $a0, 0($t0)

2. Arithmetic and logic (ALU) instructions §  Change the form of one or more operands to produce

a result stored in another location

Department of Electrical & Computer Engineering

Instruction Set Architecture (ISA) §  Instructions can be divided into 3 classes

1. Data movement instructions 2. Arithmetic and logic (ALU) instructions

§  Change the form of one or more operands to produce a result stored in another location

§  Add, Sub, Shift, etc. §  add $t3, $t4, $t5

3. Branch instructions (control flow instructions)

Department of Electrical & Computer Engineering

Instruction Set Architecture (ISA) §  Instructions can be divided into 3 classes

1. Data movement instructions 2. Arithmetic and logic (ALU) instructions 3. Branch instructions (control flow instructions)

§  Alter the normal flow of control from executing the next instruction in sequence

§  bgez $t8, else

Department of Electrical & Computer Engineering

ISA and Performance §  Instructions per program depends on source code,

compiler technology and ISA §  Cycles per instructions (CPI) depends upon the

ISA and the microarchitecture §  Time per cycle depends upon the

microarchitecture and the base technology

Time=Instruc@onsCyclesTime

ProgramProgram*Instruc@on*Cycle

Department of Electrical & Computer Engineering

Reduced Instruction Set Computer §  Relatively few number of instructions (~50) §  Basic instructions §  Relatively few different addressing modes §  Fixed length instruction format §  Only load/store instructions can access memory §  Large number of registers §  Hardwired rather than micro-program control

Department of Electrical & Computer Engineering

Reduced Instruction Set Computer §  Simpler to design §  Higher Performance

§  Smaller die size

§  Lower power consumption §  Easier to develop compilers to take advantage

of all features §  Simple code generation §  Regularity in CPI

Department of Electrical & Computer Engineering

Reduced Instruction Set Computer §  RISC ISA is extensively used for desktop, server,

and embedded: MIPS, PowerPC, UltraSPARC, ARM, MIPS16, Thumb §  Apple iPods (custom ARM7TDMI SoC) §  Apple iPhone (Samsung ARM1176JZF) §  Palm and PocketPC PDAs and smartphones (Intel

XScale family, Samsung SC32442 - ARM9) §  Nintendo Game Boy Advance (ARM7) §  Nintendo DS (ARM7, ARM9)

Department of Electrical & Computer Engineering

Reduced Instruction Set Computer §  Disadvantages

§  Higher instruction counts §  Lower instruction density §  Put a greater burden on the software or system

programmer

Department of Electrical & Computer Engineering

Complex Instruction Set Computer §  Large number of instructions (~200-300

instructions) §  Specialized complex instructions §  Many different addressing modes §  Variable length instruction format §  Examples : 68000, 80x86, VAX, PDP-11

Department of Electrical & Computer Engineering

Complex Instruction Set Computer §  Large number of instructions (~200-300

instructions) §  Specialized complex instructions §  Many different addressing modes §  Variable length instruction format §  General advantages

§  Each instruction is more capable §  Fewer instructions needed to implement a given task §  More efficient use of slow main memory

Department of Electrical & Computer Engineering

Complex Instruction Set Computer §  Disadvantages

§  Backward compatibility means with each new generation of computers instruction set and hardware become more and more complex

§  Individual instructions could be of almost any length §  Different instructions will take different amounts of

clock time to execute §  Slows down the overall performance of the machine

§  Many specialized instructions are not used frequently enough to justify their existence

Department of Electrical & Computer Engineering

Complex Instruction Set Computer §  Disadvantages

§  Many specialized instructions are not used frequently enough to justify their existence §  Approximately 20% of the available instructions are

used in a typical program

§  CISC instructions typically set the condition codes as a side effect of the instruction §  Setting the condition codes take time §  Programmers must remember to examine the condition

code bits before a subsequent instruction changes them

Department of Electrical & Computer Engineering

RISC: MIPS §  MIPS: Microprocessor without Interlocked Pipeline

Stages §  There 3 types of instruction in MIPS

1. R-Type

§  op: Basic operation of the instruction, typically called the opcode

§  rs: The first register source operand §  rt: The second register source operand

31 26 01516202125 561011

op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

Department of Electrical & Computer Engineering

§  MIPS: Microprocessor without Interlocked Pipeline Stages

§  There 3 types of instruction in MIPS 1. R-Type

§  rd: The register destination operand, it gets the result of the operation

§  shamt: Shift amount (0 if not shift instruction)

31 26 01516202125 561011

op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

RISC: MIPS

Department of Electrical & Computer Engineering

§  MIPS: Microprocessor without Interlocked Pipeline Stages

§  There 3 types of instruction in MIPS 1. R-Type

§  funct: Function. This field selects the specific variant of the operation in the op field, and is sometimes called the function code

§  For R-Type op = 0

31 26 01516202125 561011

op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

RISC: MIPS

Department of Electrical & Computer Engineering

§  MIPS: Microprocessor without Interlocked Pipeline Stages

§  There 3 types of instruction in MIPS 1. R-Type

§  add $t0, $s1, $s2

0 17 18 8 0 32

000000 10001 10010 01000 00000 100000

RISC: MIPS

Department of Electrical & Computer Engineering

§  MIPS: Microprocessor without Interlocked Pipeline Stages

§  There 3 types of instruction in MIPS 2.  I-Type §  Register immediate

§  lw $t0, 8($t3)

§  Branch displacement

op

31 26 01516202125

rs rt Immediate

op

31 26 01516202125

rs rt/funct Displacement

RISC: MIPS

Department of Electrical & Computer Engineering

RISC: MIPS §  Function call

1. Caller places parameters in a place where the procedure can access them

2. Transfer control to the procedure 3. Acquire storage resources required by the

procedure 4. Execute the statements in the procedure 5. Called function places the result in a place where

the caller can access it 6. Return control to the statement next to the

procedure call

Department of Electrical & Computer Engineering

RISC: MIPS §  Function call

§  Argument Passing §  Arguments to a function passed through $a0-$a3 §  Functions with more than 4 arguments

§  First four arguments are put in $a0-$a3 §  Remaining arguments are put on stack by the caller

§  Return Values §  Return values from a function passed through $v0-$v1 §  Functions with more than 2 return values

Department of Electrical & Computer Engineering

RISC: MIPS §  Function call

§  Argument Passing §  Arguments to a function passed through $a0-$a3 §  Functions with more than 4 arguments

§  Return Values §  Return values from a function passed through $v0-$v1 §  Functions with more than 2 return values

§  First two return values put in $v0-$v1 §  Remaining return values put on stack by the function §  The remaining return values are popped from the stack by the

caller

Department of Electrical & Computer Engineering

RISC: MIPS §  Case study ‣  Assume that A is an array of 64 words and the

compiler has associated registers $s1 and $s2 with the variables x and y. Also assume that the starting address, or base address is contained in register $s3. Determine the MIPS instructions associated with the following C statement: §  x = y + A[4]; // adds 4th element in array A to y and stores result in x

Department of Electrical & Computer Engineering

RISC: MIPS §  Case study ‣  Assume that A is an array of 64 words and the compiler

has associated registers $s1 and $s2 with the variables x and y. Also assume that the starting address, or base address is contained in register $s3. §  x = y + A[4]; // adds 4th element in array A to y and stores result in x

§  Solution: §  lw $t0, 16($s3) # $s3 contains the base address of array and

# 16 is the offset address of the 4th element

§  add $s1, $s2, $t0 # performs addition

Department of Electrical & Computer Engineering

Assembly Programs §  If (condition) then do this else do that

if ( i == j && i == k ) i++ ;

else j++;

j = i + k ;

Assuming $s1 stores i, $s2 stores j, and s3 stores k:

bne $s1, $s2, ELSE # cond1: branch if !( i == j ) bne $s1, $s3, ELSE # cond2: branch if !( i == k ) addi $s1, $s1, 1 # if-body: i++ j NEXT # jump over else

ELSE: addi $s2, $s2, 1 # else-body: j++NEXT: add $s2, $s1, $s3 # j = i + k

Department of Electrical & Computer Engineering

Assembly Programs §  While (condition) then keep doing this

while ( i < j ) { k = k * 2; i++;

}

Assuming $s1 stores i, $s2 stores j, and s3 stores k:

Loop: bge $s1, $s2, DONE # branch if ! ( i < j ) add $s3, $s3, $s3 # k = k * 2 addi $s1, $s1, 1 # i++ j Loop # jump back to top of loopDONE:

Department of Electrical & Computer Engineering

Assembly Programs §  For (these many times) do this

for (i = 0; i < j; i++) { k = k * 2;

}

Assuming $s1 stores i, $s2 stores j, and s3 stores k:

addi $s1, $0, 0 # i = 0Loop: bge $s1, $s2, DONE # branch if ! ( i < j ) add $s3, $s3, $s3 # k = k * 2 addi $s1, $s1, 1 # i++ j Loop # jump back to top of loopDONE:

Department of Electrical & Computer Engineering

Assembly Programs §  Short program

clear(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0;}

move $t0,$zero # i = 0

Loop: sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 = # &array[i] sw $zero, 0($t2) # array[i] = 0 addi $t0,$t0,1 # i = i + 1 slt $t3,$t0,$a1 # $t3 = # (i < size) bne $t3,$zero,Loop # if (…) # goto loop1

Department of Electrical & Computer Engineering

Next Class §  Exam