390 likes | 504 Views
CS37: Computer Architecture Spring Term, 2005. Instructor: Kate Forbes-Riley forbesk@cs.dartmouth.edu. CS37: Lecture 7. Floating point representation Floating point addition and multiplication Introduction to Machine Language and Assembly Language. Review: GTE ( ≥ ).
E N D
CS37: Computer ArchitectureSpring Term, 2005 Instructor: Kate Forbes-Riley forbesk@cs.dartmouth.edu
CS37: Lecture 7 • Floating point representation • Floating point addition and multiplication • Introduction to Machine Language and Assembly Language
Review: GTE (≥) 1) GTE = ¬MSB (i.e, A – B ≥ 0) 2) GTE = (MSB ● Overflow) + (¬MSB ● ¬Overflow) But this is just: ¬( MSB XOR Overflow) (GTE = 1 only if neither or both =1) MSB result xor ¬ GTEout Overflow
Binvert Op Code Review: 32-bit ALU w/ GTE If GTEout = 1, A ≥ B, else GTEout = 0 GTEout is sent to GTEin of LSB; other GTEins = 0 if OP Code = 4, GTEins = Result if A ≥ B, GTE Result = 00..01; else GTE Result = 00..00 Binvert Cin Op ALU 0 GTEin Cout A0B0 result 0 Binvert Cin Op ALU 1 GTEin Cout A1B1 0 result 1 … A30B30 0 Binvert Cin Op ALU 30 GTEin Cout result 30 result 31 A31B31 0 Binvert Cin Op ALU 31 GTEin Cout GTEout
Review: Binary Integer Multiplication 1 0 0 multiplicand (m) x 1 0 1 multiplier (n) 1 0 0 multiply and shift 0 0 0 intermediate + 1 0 0 products (Pb) 1 0 1 0 0 product: (p) m+n bits
Review: Binary Multiplication (1st version PH3) 0 0 0 0 0 0 product = m + n bits 0 0 0 1 0 0 m’cand in right m bits 1 0 1 test m’plier: LSB = 1 0 0 0 1 0 0 product = product + m’cand 0 0 1 0 0 0 SLL 1 m’cand 0 1 0 SLR 1 m’plier 0 1 0 test m’plier: LSB = 0 0 1 0 0 0 0 SLL 1 m’cand 0 0 1 SLR 1 m’plier 0 0 1 test m’plier: LSB = 1 0 1 0 1 0 0 product = product + m’cand 1 0 0 0 0 0 SLL 1 m’cand 0 0 0 SLR 1 m’plier
Floating Point • Real Numbers: include fractions of whole #s • 123,456.789 105 … 100.10-1 … 10-3 • Scientific notation: a single digit to the left of the decimal point • 0.123456789 x 106 • 0.0123456789 x 107 • 0.00123456789 x 108 • Normalized Scientific notation: scientific notation with no leading zeros • 1.23456789 x 105
Floating Point with Binary Numbers • Real Numbers: include fractions of whole #s • 110111.101101 25 … 20.2-1 … 2-6 • Scientific notation: a single digit to the left of the binary point (with exponent in base 10) • 0.110111101101 x 26 • 0.0110111101101 x 27 • Normalized Scientific notation: scientific notation with no leading zeros • 1.10111101101 x 25
Floating Point with Binary Numbers • Normalized Scientific notation: always using the same form simplifies arithmetic and increases accuracy of stored reals b/c no leading zeros: leading digit is always 1 1.10111101101 x 25 Significand Base Exponent Common to write significand in binary and base and exponent in decimal
IEEE 754 floating-point standard • Single precision = 32-bit representation (same procedure for double = 64 bit: 11 exp; 52 sig) 31 0 31 sign • exponent 23 22 significand 0 • 1 sign bit: 1 = negative; 0 = positive • 8 bits for exponent: determines range of #s that can be represented • 23 bits for significand: determines accuracy of #s that are represented
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • 23 bits for significand represents the fraction. The leading 1 is implicit! • Because fraction is already in binary form, we can just put its (first/add) 23 bits into bits 0-22 • Similarly, we just put the sign in the sign bit • E.g., 1.10111101101 x 25 31 0 22 significand 0 10111101101000000000000
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • +/- Exponents aren’t represented with two’s complement! • Exponent represented with “biased notation”. This allows efficient sorting with integer HW: 00000001 … 11111111 negative exponents < positive exponents
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • Tocompute biased exponent, add bias to exponent (both decimal) then convert to binary • Bias for single precision = 127
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • So biased exponent represents sign and magnitude of exponent in 8 bits. • Note range and reserved biased exponents (see PH3 pg 194):
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • Tocompute the value of this FP representation: Value = (-1)sign x (1+significand) x 2biasedexp –bias • Binary Val = -1.10000000000000000000000 x 23 (-1)1 x (1+.10000000000000000000000) x 2130 –127 • Decimal Val = -12.0 -1 x (1 + 2-1) x 23 = -1 x (1.5) x 8 = -12.0 1 sign 10000010 biased exp 10000000000000000000000 (significand fraction)
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • Toconvert decimal to IEEE binary floating point • If you’re lucky, it’s easy to manipulate: 0.75 = 3/4 = 3 x 2-2 = 0011. x 2-2 = (1.1 x 21) x 2-2 = 1.1 x 2-1 = (-1)0x (1 + .10000…000) x 2((-1+127) – 127) = (-1)0x (1 + .10000…000) x 2(126 – 127) 31 0 • exponent 23 • 01111110 • significand 0 • 10000000000000000000000
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) • If you’re unlucky, use brute force: - 3.1415 1. convert integer 2. convert fraction: (not sign): 11Does 2-1 fit? Does 2-2 fit? Does 2-3 fit? Does 2-4 fit? … • Here stop at 2-22 due to integer normalization
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) 2. Converting fraction part: 11.00100100001… .1415 - .1250 (1/8 = 2-3) .0165 - .015625 (1/64 = 2-6) .000875 - .000488 (1/2048 = 2-11) … Infinite # reals between 0…1; so some inaccuracy
IEEE 754 floating point: single precision 31 sign • exponent 23 • (biased) • significand 0 • (fraction) 3. Normalize with sign: -11.00100100001… -1.100100100001… x 21 4. Convert to IEEE 754 via equation: = (-1)1x (1 + .100100001…) x 2((1+127) – 127) = (-1)1x (1 + .100100001…) x 2(128 – 127) 1 sign 10000000 (biased exp) 100100100001… (significand fraction)
Convert -128.673828125 to IEEE FP standard 1. Convert integer part (w/o sign): 128 = 10000000 2. Convert fraction part: = 1010110010000000 3. Normalize with sign: -10000000.1010110010000000 = -1.00000001010110010000000 x 27
Convert -128.673828125 to IEEE FP standard -1.00000001010110010000000 x 27 4. Convert to IEEE 754 equation: (-1)1x (1 + .00000001010110010000000) x 2((7+127) – 127) (-1)1x (1 + .00000001010110010000000) x 2(134 – 127) 5. Convert to IEEE 754 FP representation: 1 sign 10000110 (biased exp) 00000001010110010000000 (significand fraction)
IEEE 754 Floating Point: Addition Algorithm • Add 1.100 x 22 and 1.100 x 21 Step 1: Shift right (SRL) the significand of the smaller # to match exponent of larger # 1.100 x 210.110 x 22 lost accuracy: keep #bits Step 2: Add the significands 1.100 x 22 + 0.110 x 22 10.010 x 22
IEEE 754 floating point: Addition Algorithm Step 3: Normalize sum, checking for overflow (+ exponent too large for (8) bits) and underflow (- exponent too small for (8) bits) 10.010 x 221.0010 x 23 0 < 3 + 127 < 255 ok: no under/overflow Step 4: Round the sum (variously, see pg 213), then normalize again if necessary 1.0010 x 23 1.001 x 23 lost accuracy: keep #bits
IEEE 754 floating point: Multiplication Algorithm • Multiply 1.100 x 22 by 1.100 x 21 Step 1: Step 2: Add biased Multiply significands exponents then 1.100 subtract bias x 1.100 (2+127) + (1+127) 0000 = 257 - 127 = 130 0000 1100 1100 10.010000 place binary point 3 + 3 digits from right
IEEE 754 floating point: Multiplication Algorithm Step 3: Normalize product, check over/underflow 10.010000 x 21301.0010000 x 2131 0 < 131 < 255 ok: no under/overflow Step 4: Round (variously, see pg 213), then normalize again if necessary 1.0010000 x 2131 1.001 x 2131 lost accuracy Step 5: Set sign of product (+ if operands’ signs are same; else -) 1.001 x 2131
Implementation of Floating Point Operations • Block diagram of FP addition HW on pg 201 • Similar design to integer operations • But need more HW (PH3 Figure 3.17, 3.18) • 2 ALUs: one for exponent, one for significand • Logic for Shifts • Logic for Rounding
Machine Languages: Instruction Sets • An “instruction” is a specific sequence of binary numbers that tells the control to perform a single operation in a single step via the datapath • An “instruction set” is the set of all instructions that a computer understands • “Machine Language” is the numeric version of these instructions • “Assembly Language” is a symbolic notation that the assembler translates into machine language (basically a 1-to-1 correspondence)
C Program swap(int v[], int k) { int tmp; tmp = v[k]; v[k] = v[k+1]; v[k+1] = tmp;} MIPS AL Program swap: muli $2, $5, 4 add $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 MIPS ML Program 00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 … C compiler MIPS assembler
Processors and Assembly Languages • Many different CPU architectures (machine languages, assembly languages, assemblers, instruction set types, etc.) PH3 CH 2 and X86 1.2 have some comparative discussion • PH3 focuses mainly on the MIPS architecture • We will learn the modern Intel 80x86 assembly language because it is the most prevalent today • However, X86 is much more complex, due to backwards compatibility with earlier versions; thus, we will gloss over some details. • We will use the X86 book on the website.
Instruction Set Types • MIPS uses RISC (Reduced Instruction Set Computer) • Fixed instruction length: 32 bits • Fewer instruction formats • X86 uses CISC (Complex Instruction Set Computer) • Instructions vary from 1 to 17 bytes; more common operations are shorter • More instruction formats
Machine Language Programs • An ML “program” consists of a specific sequence of instructions (machine code) • In other words, ML programs are just sequences of (sequences of) binary numbers • Therefore, both ML programs and data can be treated alike: both can be stored in memory to be read and written to: stored program concept
Inside Computer: Stored Program Concept Main Memory (volatile) Accounting program (machine code) Editor program (machine code) Monthly bills data Master’s thesis data Processor DEVICES R E G I S T E R S Control IN PUT Datapath OUT PUT ALU Both the data and the programs for manipulating that data are stored in memory
Inside Computer: Stored Program Concept Main Memory Accounting program (machine code) Editor program (machine code) Monthly bills data Master’s thesis data Processor DEVICES R E G I S T E R S Control IN PUT Datapath OUT PUT ALU Control takes program as input; each instructiontells it to do operations on data in memory, via datapath and registers
Using Registers • Basic repeated pattern occurs when performing a program: • Load values from memory into registers • Operate on values in registers • Store values from registers into memory • Why use registers as intermediary? • Main memory is large (~1 GB) but slow • Registers are much faster: they are closer (on processor!) and smaller
Using Registers • Processor uses registers for “scratch paper”: • Registers are the primary source and destination for the results of operations • These results are often used repeatedly, so keeping them in registers is faster • Some registers are “general purpose” (can be used for anything), others play specific roles • MIPS has 32 32-bit registers (don’t worry about names right now); 24 are general purpose registers
Using Registers • X86 has 8 32-bit registers: • ESP, EBP: often play specific roles • EAX, ECX, EDX, EBX, ESI, EDI: truly general purpose registers • Don’t worry about the 16-bit registers discussed in the X86 book; most exist for backwards compatibility • Why does X86 have so many fewer registers than MIPS? In part, because X86 has much more complex instructions
How Instructions access Memory • To access memory elements (e.g., load/store data), the instruction must know memory organization • Memory is organized as a huge array, where the memory address is the index to the array
How Instructions access data in Memory • In x86, memory is byte-addressable: address 0 is the first byte, address 1 is the second byte, etc. (different in MIPS) • Note: in x86, 2 bytes = word; 4 bytes = double word • Protected mode: each program has own virtual address space, managed by OS (details later)
Assignment • Reading: • PH Sections 3.6, skim 3.7 – 3.10 • Skim PH3, Chapter 2 • X86 book (as needed)