Tuesday, 15 September 2015 More on MIPS Today we’ll use lots of slides from the textbook publisher. For Thursday: Keep reading chapter 2 in P&H (try to get at least through 2.5); start “skimming” K&R, 2.1--3.8 (much of this material will be familiar--like Java)
Chapter 2 Instructions: Language of the Computer
Instruction Set Introduction §2.1 ■ The repertoire of instructions of a computer ■ Different computers have different instruction sets ■ But with many aspects in common ■ Early computers had very simple instruction sets ■ Simplified implementation ■ Many modern computers also have simple instruction sets RISC = “Reduced Instruction Set Computer” Chapter 2 — Instructions: Language of the Computer — 3
The MIPS Instruction Set ■ Used as the example throughout the book ■ Stanford MIPS commercialized by MIPS Technologies (http://imgtec.com/mips/) Acquired by Imagination Technologies in 2013 ■ Large share of embedded core market ■ Applications in consumer electronics, network/storage equipment, cameras, printers, … ■ Typical of many modern ISAs Instruction Set Architecture ■ See MIPS Reference Data tear-out card, and Appendixes B and E Chapter 2 — Instructions: Language of the Computer — 4
Arithmetic Operations Computer Hardware §2.2 Operations of the ■ 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 favours regularity ■ Regularity makes implementation simpler ■ Simplicity enables higher performance at lower cost Chapter 2 — Instructions: Language of the Computer — 5
Arithmetic Example ■ C code: f = (g + h) - (i + j); Using the “three-operand notation: add t0, g, h # temp t0 = g + h add t1, i, j # temp t1 = i + j sub f, t0, t1 # f = t0 - t1 This is almost MIPS! Chapter 2 — Instructions: Language of the Computer — 6
Register Operands Computer Hardware §2.3 Operands of the ■ 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. main memory: millions of locations Chapter 2 — Instructions: Language of the Computer — 7
Register Operand Example ■ C code: f = (g + h) - (i + j); ■ Assume that f, …, j are in $s0, …, $s4 ■ Compiled MIPS code: add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1 Chapter 2 — Instructions: Language of the Computer — 8
Memory Operands Main memory used for composite data ■ ■ Arrays, structures, dynamic data NOTE: quite often, ■ To apply arithmetic operations “ordinary” variables ■ Load values from memory into registers (“int i”, “char x”, etc.) Store result from register to memory ■ are saved in registers, ■ Memory is byte addressed not in memory. Each address identifies an 8-bit byte ■ ■ Words are aligned in memory This is what the “ . Address must be a multiple of 4 ■ align ” assembler ■ MIPS is Big Endian directive is for! Most-significant byte at least address of a word ■ ■ c.f. Little Endian: least-significant byte at least address
Notation Suppose register $t0 contains a memory address, and suppose we want the contents of the word at that address. The notation is: offset 0($t0) base register If we want the contents of the next word after that, we write 4($t0) ; the next one is 8($t0) ; and so on. The item inside the parentheses MUST be a register; the value outside MUST be an integer.
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 offset ■ 4 bytes per word lw $t0, 32($s3) # load word add $s1, $s2, $t0 base register Chapter 2 — Instructions: Language of the Computer — 11
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) # load word add $t0, $s2, $t0 sw $t0, 48($s3) # store word Chapter 2 — Instructions: Language of the Computer — 12
MIPS example (in repository): .data .align 2 # make sure we're on a word boundary a: .word 10,20,25 # three consecutive words sum: .space 4 # space for the sum .text la $t0,a # "la" = "load address" lw $t1,0($t0) # load CONTENTS of a into t1 lw $t2,4($t0) # load contents of a+4 into t2 add $t1,$t1,$t2 # add this to t1 lw $t2,8($t0) # load contents of a+8 into t2 add $t1,$t1,$t2 # add this to t1 sw $t1,sum # store sum in memory li $v0,10 # standard code for normal exit syscall # “ “ “ “ “
Registers vs. Memory ■ Registers are faster to access than memory ■ Operating on memory data requires loads and stores ■ More instructions to be executed ■ Compiler must use registers for variables as much as possible ■ Only spill to memory for less frequently used variables ■ Register optimization is important! Chapter 2 — Instructions: Language of the Computer — 14
Immediate Operands ■ Constant data specified in an instruction addi $s3, $s3, 4 ■ No subtract immediate instruction ■ Just use a negative constant ??!! But MARS does have a subi “pseudo- instruction” addi $s2, $s1, -1 ■ Design Principle 3: Make the common case fast ■ Small constants are common ■ Immediate operand avoids a load instruction Chapter 2 — Instructions: Language of the Computer — 15
The Constant Zero ■ MIPS register 0 ($zero) is the constant 0 ■ Cannot be overwritten ■ Useful for common operations ■ E.g., move between registers add $t2, $s1, $zero “Copy the contents of $s1 into $t2” Chapter 2 — Instructions: Language of the Computer — 16
Recommend
More recommend