ECE232: Hardware Organization and Design Lecture 4: MIPS Instructions Adapted from Computer Organization and Design, Patterson & Hennessy, UCB
From Last Time Two values enter from the left (A and B) Need to perform (A+B)+A In -> X (Load A) In -> Y (Load B) A+B -> Y (A+B)+A -> Out Four steps and then repeat InPass OutPass In Out LoadX X Y LoadY Function ALU AluPass ECE232: MIPS Instructions-I 2
Building Blocks Tri-State Buffer Truth table Gated D-latch (based on static value of enable pin, E) input output E D Q Q Comment 0 X Q prev Q prev No change 1 0 0 1 Reset 1 1 1 0 Set ECE232: MIPS Instructions-I 3
Arithmetic Logic Unit Examine the functionality of this 74LS382 ALU chip datasheet http://pdf.datasheetcatalog.com/datasheet/nationalsemiconductor/ds009529.pdf Performs 8 functions ECE232: MIPS Instructions-I 4
Conventions In the world, there are all kinds of conventions that we take for granted. Some are more commonly shared than others: ECE232: MIPS Instructions-I 5
Unsigned Binary Integers Given an n-bit number x = x + Range: 0 to +2 n 1 n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Example 0000 0000 0000 0000 0000 0000 0000 1011 2 = 0 + + 1 2 3 + 0 2 2 +1 2 1 +1 2 0 = 0 + + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295 Using 64 bits 0 to + 18,446,744,073,709,551,616 ECE232: MIPS Instructions-I 6
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 Example (4 bits): 0111 à +7 1000 à 8 ECE232: MIPS Instructions-I 7
2s-Complement Signed Integers Given an n-bit number x = x + n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Range: 2 n 1 to +2 n 1 1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = 1 2 31 + 1 2 30 + + 1 2 2 +0 2 1 +0 2 0 = 2,147,483,648 + 2,147,483,644 = 4 10 Using 32 bits 2,147,483,648 to +2,147,483,647 ECE232: MIPS Instructions-I 8
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 (found in old Macintosh with PowerPC chip) Little Endian: address of least significant byte = word address (xx00 = Little End of word) Intel 80x86, DEC Vax, DEC Alpha (found in all modern PC computers: Mac and Windows) 3 2 1 0 little endian byte 0 msb lsb 0 1 2 3 big endian byte 0 ECE232: MIPS Instructions-I 9
Computer Organization 5 classic components of any computer Processor (CPU) (active) Control ( brain ) Datapath Computer Memory (passive) (where programs, & data live when running) Devices Input Output Keyboard, Mouse Disk (where programs, & data live when not running) Display, Printer The CPU Control fits in closely with datapath ECE232: MIPS Instructions-I 10
Program View of Memory Processor (CPU) Control Datapath Computer Memory Devices Input Output Memory viewed as a large, single -dimension array, with an address? 8 bits of data A memory address is an index into array The index points to a byte of memory - "Byte addressing" A 32-bit machine addresses memory by a 32-bit address Access bytes (8 bits), words (32 bits) or half-words 0 1 2 3 4 5 6... 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data ECE232: MIPS Instructions-I 11
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) ECE232: MIPS Instructions-I 12
Registers Computer Processor (CPU) Control Memory Devices Input Datapath Registers Output Once a memory is fetched, the data must be placed somewhere in CPU Advantages of registers registers are faster than memory registers can hold variables and intermediate results memory traffic is reduced, so program runs faster code density improves (later) ECE232: MIPS Instructions-I 13
Registers code for A = B + C (This is not MIPS code, It is in English) load R1,B load R2,C add R3,R1,R2 store R3,A # R1 = B # R2 = C # R3 = R1+R2 # A = R3 Many current processors support 32 registers (MIPS) The more registers available, the fewer memory accesses will be necessary Registers can hold lots of intermediate values Instructions must include bits to specify which registers to operate on register address ECE232: MIPS Instructions-I 14
Instruction Set Architecture (ISA) Application (FireFox) Software Hardware Compiler Assembler Processor Operating System Memory (Unix; Windows) Datapath & Control Digital Design Circuit Design transistors, IC layout I/O system Instruction Set Architecture Key Idea: abstraction hide unnecessary implementation details helps us cope with enormous complexity of real systems ECE232: MIPS Instructions-I 15
The MIPS Instruction Set Used as the example throughout the book Stanford MIPS commercialized by MIPS Technologies (www.mips.com) Large share of embedded core market Applications in consumer electronics, network/storage equipment, cameras, printers, Typical of many modern ISAs See MIPS Reference Data tear-out card, and Appendices B and E ECE232: MIPS Instructions-I 16
Arithmetic Operations Add and subtract, three operands Two sources and one destination add a, b, c # a gets b + c All arithmetic operations have this form Design Principle 1: Simplicity favors regularity Regularity makes implementation simpler Simplicity enables higher performance at lower cost ECE232: MIPS Instructions-I 17
Arithmetic Example C or Java code: f = (g + h) - (i + j); Compiled MIPS code: add t0, g, h # temp t0 = g + h add t1, i, j # temp t1 = i + j sub f, t0, t1 # f = t0 - t1 ECE232: MIPS Instructions-I 18
Register Operands Arithmetic instructions use register operands MIPS has a 32 32-bit register file Use for frequently accessed data Numbered 0 to 31 32-bit data called a word Assembler names $t0, $t1,, $t9 for temporary values $s0, $s1,, $s7 for saved variables Design Principle 2: Smaller is faster c.f. (compare with) main memory: millions of locations ECE232: MIPS Instructions-I 19
Register Operand Example C or Java code: f = (g + h) - (i + j); f,, j in $s0,, $s4 Compiled MIPS code: add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1 ECE232: MIPS Instructions-I 20
Simplified Datapath Two register fetches in one cycle Load from memory Store to memory Registers A L U Data Memory From Register for SW To register for LW ECE232: MIPS Instructions-I 21
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 ECE232: MIPS Instructions-I 22
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 ECE232: MIPS Instructions-I 23
Memory Operand Example 2 C code: A[12] = h + A[8]; h in $s2, base address of A in $s3 Compiled MIPS code: Index 8 requires offset of 32 lw $t0, 32($s3) add $t0, $s2, $t0 sw $t0, 48($s3) # load word # store word ECE232: MIPS Instructions-I 24