ECE331: Hardware Organization and Design Lecture 15: Midterm 1 Review Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
Basics Midterm to cover Book Sections (inclusive) 1.1 1.5 2.1 2.8 3.1 3.5 4.1 4.5 MIPS programming Green sheet to be provided as part of the exam (only the first page will actually be needed) à You should know hexadecimal conversion No calculators or additional materials from you are necessary Just bring a writing implement and a method for making corrections Recommended: Pencil and Eraser Don t forget to eat lunch/dinner! Food is energy for your brain!! ECE331: Midterm Review 2
Memory word addressing CPU Address Bus Memory Every word in memory has an address Today machines address memory as bytes, hence word addresses differ by 4 Memory[0], Memory[4], Memory[8], Word 0 (bytes 0 to 3) Word 1 (bytes 4 to 7) 0x00000000 0x00000004 0x00000008 0x0000000C 0x00000010 0x00000014 0x00000018 0x0000001C 0xfffffff4 0xfffffffc 0xfffffffc Called the address of a word Memory 4GB Max (Typically 512MB-2GB) ECE331: Midterm Review 3
Datapath I/O A wire (or by extension, a bus) can be driven by only one tri-state at a time If InPass is active, AluPass must be inactive If AluPass is active, InPass must be inactive InPass OutPass LoadX X Y LoadY Function ALU AluPass ECE331: Midterm Review 4
Implementing the Control ROM Two values enter from the left (A and B) Need to perform (A+B)+A In -> X (Load A) - State 00 In -> Y (Load B) - State 01 A+B -> Y - State 10 (A+B)+A -> Out - State 11 Addr PS ROM 0100010100 00 1000001100 01 1101101010 10 0001100011 11 NS PS NS Function LoadX LoadY InPass AluPass OutPass 00 01 000 1 0 1 0 0 01 10 000 0 1 1 0 0 10 11 011 0 1 0 1 0 11 00 011 0 0 0 1 1 Control outputs ECE331: Midterm Review 5
Addressing words: Big or Small Endian Big Endian: address of most significant byte = word address (xx00 = Big End of word) IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA Little Endian: address of least significant byte = word address (xx00 = Little End of word) Intel 80x86, DEC Vax, DEC Alpha msb 3 2 1 0 little endian byte 0 lsb 0 1 2 3 big endian byte 0 ECE331: Midterm Review 6
Memory Operands Main memory used for composite data Arrays, structures, dynamic data To apply arithmetic operations Load values from memory into registers Store result from register to memory Memory is byte addressed Each address identifies an 8-bit byte Words are aligned in memory Address must be a multiple of 4 MIPS is Big Endian Most-significant byte at least address of a word c.f. Little Endian: least-significant byte at least address ECE331: Midterm Review 7
Memory Operand Example 1 C code: g = h + A[8]; g in $s1, h in $s2, base address of A in $s3 Compiled MIPS code: Index 8 requires offset of 32 4 bytes per word lw $t0, 32($s3) # load word add $s1, $s2, $t0 offset base register ECE331: Midterm Review 8
MIPS Registers and Usage Name Register number Usage $zero 0 the constant value 0 $at 1 reserved for assembler $v0-$v1 2-3 values for results and expression evaluation $a0-$a3 4-7 arguments $t0-$t7 8-15 temporary registers $s0-$s7 16-23 saved registers $t8-$t9 24-25 more temporary registers $k0-$k1 26-27 reserved for Operating System kernel $gp 28 global pointer $sp 29 stack pointer $fp 30 frame pointer $ra 31 return address ECE331: Midterm Review 9
2s-Complement Signed Integers Bit 31 is sign bit 1 for negative numbers 0 for non-negative numbers ( 2 n 1 ) can t be represented Non-negative numbers have the same unsigned and 2scomplement representation Some specific numbers 0: 0000 0000 0000 1: 1111 1111 1111 Most-negative: 1000 0000 0000 Most-positive: 0111 1111 1111 Does not apply to floating point numbers!!! ECE331: Midterm Review 10
MIPS Arithmetic Instructions Instruction Example Meaning Comments add add $1,$2,$3 $1 = $2 + $3 3 operands; subtract sub $1,$2,$3 $1 = $2 $3 3 operands; add immediate addi $1,$2,100 $1 = $2 + 100 + constant; add unsigned addu $1,$2,$3 $1 = $2 + $3 3 operands; subtract unsigned subu $1,$2,$3 $1 = $2 $3 3 operands; add imm. unsign. addiu $1,$2,100 $1 = $2 + 100 + constant; multiply mult $2,$3 Hi, Lo = $2 x $3 64-bit signed product multiply unsigned multu$2,$3 Hi, Lo = $2 x $3 64-bit unsigned product divide div $2,$3 Lo = $2 $3, Lo = quotient, Hi = remainder Hi = $2 mod $3 divide unsigned divu $2,$3 Lo = $2 $3, Unsigned quotient & remainder Hi = $2 mod $3 Move from Hi mfhi $1 $1 = Hi Used to get copy of Hi Move from Lo mflo $1 $1 = Lo Used to get copy of Lo ECE331: Midterm Review 11
Sign Extension Representing a number using more bits Preserve the numeric value In MIPS instruction set addi: extend immediate value lb, lh: extend loaded byte/halfword Replicate the sign bit to the left c.f. unsigned values: extend with 0s Examples: 8-bit to 16-bit +2: 0000 0010 => 0000 0000 0000 0010 2: 1111 1110 => 1111 1111 1111 1110 ECE331: Midterm Review 12
I-Format vs. R-Format Instructions Compare with R-Format 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits op rs rt rd shamt funct R-Format 6 bits 5 bits 5 bits 16 bits op rs rt offset I-Format Note similarity! ECE331: Midterm Review 13
Binary Representation - Branch 6 bits 5 bits 5 bits 16 bits op rs rt offset Branch instructions use I-Format offset is added to PC when branch is taken beq r0, r1, offset has the effect: Conversion to byte offset if (r0==r1) pc = pc + 4 + (offset << 2) else pc = pc + 4; Offset is specified in instruction words (why?) What is the range of the branch target addresses? ECE331: Midterm Review 14
Binary Representation - Jump 6 bits 26 bits op address Jump Instruction uses J-Format (op=2) What happens during execution? PC = PC[31:28] : (IR[25:0] << 2) Concatenate upper 4 bits of PC to form complete 32-bit address Conversion to byte offset ECE331: Midterm Review 15
Storage Management on a Call/Return A new procedure must create space for all its local variables on the stack Arguments are copied into $a0-$a3; the jal is executed After the callee creates stack space, it updates the value of $sp Once the callee finishes, it copies the return value into $v0, frees up stack space, and $sp is incremented The caller can use the value in $v0 $t0-$t7 are not stored on the stack during procedure calls ECE331: Midterm Review 16
Full-Adder (FA) Examine the Full Adder table Cin x y Cin Cout S 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1 Cout = x y + Cin (x + y) S = x y c + x yc + xy c + xyc = x y c x y l In general, for bit i: c i+1 = x i y i + c i (x i +y i ) where c i+1 = Cout, c i = Cin Cout Sum Half adder has 2 inputs. In principle HA is same as FA, with Cin set to 0. ECE331: Midterm Review 17
Parallel Adder: Ripple Carry In a parallel arithmetic unit All 2n input bits available at the same time Carry propagates from the FA to the right to FA to the left Carries ripple through all n FAs before we can claim that the sum outputs are correct and may be used in further calculations Each FA has a finite delay ECE331: Midterm Review 18
Example x3,x2,x1,x0=1111 y3,y2,y1,y0=0001 ΔFA - operation time - delay Assuming equal delays for sum and carry-out Longest carry propagation chain when adding two 4-bit numbers In synchronous arithmetic units - time allowed for adder's operation is worst-case delay - nδfa ECE331: Midterm Review 19
Single-Precision Range Exponents 00000000 and 11111111 reserved Smallest value Exponent: 00000001 actual exponent = 1 127 = 126 Fraction: 000 00 significand = 1.0 ±1.0 2 126 ±1.2 10 38 Largest value exponent: 11111110 actual exponent = 254 127 = +127 Fraction: 111 11 significand 2.0 ±2.0 2 +127 ±3.4 10 +38 ECE331: Midterm Review 20
Floating-Point Example Represent 0.75 0.75 = ( 1) 1 1.1 2 2 1 S = 1 Fraction = 1000 00 2 Exponent = 1 + Bias Single: 1 + 127 = 126 = 01111110 2 Double: 1 + 1023 = 1022 = 01111111110 2 Single: 1011111101000 00 Double: 1011111111101000 00 ECE331: Midterm Review 21
Floating-Point Addition Consider a 4-digit decimal example 9.999 10 1 + 1.610 10 1 1. Align decimal points Shift number with smaller exponent 9.999 10 1 + 0.016 10 1 2. Add significands 9.999 10 1 + 0.016 10 1 = 10.015 10 1 3. Normalize result & check for over/underflow 1.0015 10 2 4. Round and renormalize if necessary 1.002 10 2 ECE331: Midterm Review 22
Floating-Point Multiplication Now consider a 4-digit binary example 1.000 2 2 1 1.110 2 2 2 (0.5 0.4375) 1. Add exponents Unbiased: 1 + 2 = 3 Biased: ( 1 + 127) + ( 2 + 127) = 3 + 254 127 = 3 + 127 2. Multiply significands 1.000 2 1.110 2 = 1.1102 1.110 2 2 3 3. Normalize result & check for over/underflow 1.110 2 2 3 (no change) with no over/underflow 4. Round and renormalize if necessary 1.110 2 2 3 (no change) 5. Determine sign: +ve ve ve 1.110 2 2 3 = 0.21875 ECE331: Midterm Review 23
Datapath With Control (no jumps) op rs rt rd addr/ func ECE331: Midterm Review 24