Chapter 2 Instructions Language of the Computer n
- Slides: 92
Chapter 2 Instructions: Language of the Computer
n n The repertoire of instructions of a computer Different computers have different instruction sets n n But with many aspects in common Early computers had very simple instruction sets n n § 2. 1 Introduction Instruction Set Simplified implementation Many modern computers also have simple instruction sets Chapter 2 — Instructions: Language of the Computer — 2
The MIPS Instruction Set n n n Used as the example throughout the book Stanford MIPS commercialized by MIPS Technologies (www. mips. com) Large share of embedded core market n n Applications in consumer electronics, network/storage equipment, cameras, printers, … Typical of many modern ISAs n See MIPS Reference Data tear-out card, and Appendixes B and E Chapter 2 — Instructions: Language of the Computer — 3
n Add and subtract, three operands n n n 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 n n § 2. 2 Operations of the Computer Hardware Arithmetic Operations Regularity makes implementation simpler Simplicity enables higher performance at lower cost Chapter 2 — Instructions: Language of the Computer — 4
Arithmetic Example n C code: f = (g + h) - (i + j); n Compiled MIPS code: add t 0, g, h add t 1, i, j sub f, t 0, t 1 # temp t 0 = g + h # temp t 1 = i + j # f = t 0 - t 1 Chapter 2 — Instructions: Language of the Computer — 5
n n Arithmetic instructions use register operands MIPS has a 32 × 32 -bit register file n n Assembler names n n n Use for frequently accessed data Numbered 0 to 31 32 -bit data called a “word” $t 0, $t 1, …, $t 9 for temporary values $s 0, $s 1, …, $s 7 for saved variables § 2. 3 Operands of the Computer Hardware Register Operands Design Principle 2: Smaller is faster n c. f. main memory: millions of locations Chapter 2 — Instructions: Language of the Computer — 6
Register Operand Example n C code: f = (g + h) - (i + j); n f, …, j in $s 0, …, $s 4 n Compiled MIPS code: add $t 0, $s 1, $s 2 add $t 1, $s 3, $s 4 sub $s 0, $t 1 Chapter 2 — Instructions: Language of the Computer — 7
Memory Operands n Main memory used for composite data n n To apply arithmetic operations n n n Each address identifies an 8 -bit byte Words are aligned in memory n n Load values from memory into registers Store result from register to memory Memory is byte addressed n n Arrays, structures, dynamic data Address must be a multiple of 4 MIPS is Big Endian n n Most-significant byte at least address of a word c. f. Little Endian: least-significant byte at least address Chapter 2 — Instructions: Language of the Computer — 8
Memory Operand Example 1 n C code: g = h + A[8]; n g in $s 1, h in $s 2, base address of A in $s 3 n Compiled MIPS code: n Index 8 requires offset of 32 n 4 bytes per word lw $t 0, 32($s 3) add $s 1, $s 2, $t 0 offset # load word base register Chapter 2 — Instructions: Language of the Computer — 9
Memory Operand Example 2 n C code: A[12] = h + A[8]; n h in $s 2, base address of A in $s 3 n Compiled MIPS code: Index 8 requires offset of 32 lw $t 0, 32($s 3) # load word add $t 0, $s 2, $t 0 sw $t 0, 48($s 3) # store word n Chapter 2 — Instructions: Language of the Computer — 10
Registers vs. Memory n n Registers are faster to access than memory Operating on memory data requires loads and stores n n More instructions to be executed Compiler must use registers for variables as much as possible n n Only spill to memory for less frequently used variables Register optimization is important! Chapter 2 — Instructions: Language of the Computer — 11
Immediate Operands n Constant data specified in an instruction addi $s 3, 4 n No subtract immediate instruction n Just use a negative constant addi $s 2, $s 1, -1 n Design Principle 3: Make the common case fast n n Small constants are common Immediate operand avoids a load instruction Chapter 2 — Instructions: Language of the Computer — 12
The Constant Zero n MIPS register 0 ($zero) is the constant 0 n n Cannot be overwritten Useful for common operations n E. g. , move between registers add $t 2, $s 1, $zero Chapter 2 — Instructions: Language of the Computer — 13
n n n Given an n-bit number Range: 0 to +2 n – 1 Example n n § 2. 4 Signed and Unsigned Numbers Unsigned Binary Integers 0000 0000 10112 = 0 + … + 1× 23 + 0× 22 +1× 21 +1× 20 = 0 + … + 8 + 0 + 2 + 1 = 1110 Using 32 bits n 0 to +4, 294, 967, 295 Chapter 2 — Instructions: Language of the Computer — 14
2 s-Complement Signed Integers n n n Given an n-bit number Range: – 2 n – 1 to +2 n – 1 Example n n 1111 1111 11002 = – 1× 231 + 1× 230 + … + 1× 22 +0× 21 +0× 20 = – 2, 147, 483, 648 + 2, 147, 483, 644 = – 410 Using 32 bits n – 2, 147, 483, 648 to +2, 147, 483, 647 Chapter 2 — Instructions: Language of the Computer — 15
2 s-Complement Signed Integers n Bit 31 is sign bit n n n 1 for negative numbers 0 for non-negative numbers –(– 2 n – 1) can’t be represented Non-negative numbers have the same unsigned and 2 s-complement representation Some specific numbers n n 0: 0000 … 0000 – 1: 1111 … 1111 Most-negative: 1000 0000 … 0000 Most-positive: 0111 1111 … 1111 Chapter 2 — Instructions: Language of the Computer — 16
Signed Negation n Complement and add 1 n n Complement means 1 → 0, 0 → 1 Example: negate +2 n n +2 = 0000 … 00102 – 2 = 1111 … 11012 + 1 = 1111 … 11102 Chapter 2 — Instructions: Language of the Computer — 17
Sign Extension n Representing a number using more bits n n In MIPS instruction set n n addi: extend immediate value lb, lh: extend loaded byte/halfword beq, bne: extend the displacement Replicate the sign bit to the left n n Preserve the numeric value c. f. unsigned values: extend with 0 s Examples: 8 -bit to 16 -bit n n +2: 0000 0010 => 0000 0010 – 2: 1111 1110 => 1111 1110 Chapter 2 — Instructions: Language of the Computer — 18
n Instructions are encoded in binary n n MIPS instructions n n Called machine code Encoded as 32 -bit instruction words Small number of formats encoding operation code (opcode), register numbers, … Regularity! Register numbers n n n $t 0 – $t 7 are reg’s 8 – 15 $t 8 – $t 9 are reg’s 24 – 25 $s 0 – $s 7 are reg’s 16 – 23 § 2. 5 Representing Instructions in the Computer Representing Instructions Chapter 2 — Instructions: Language of the Computer — 19
MIPS R-format Instructions n op rs rt rd shamt funct 6 bits 5 bits 6 bits Instruction fields n n n op: operation code (opcode) rs: first source register number rt: second source register number rd: destination register number shamt: shift amount (00000 for now) funct: function code (extends opcode) Chapter 2 — Instructions: Language of the Computer — 20
R-format Example op rs rt rd shamt funct 6 bits 5 bits 6 bits add $t 0, $s 1, $s 2 special $s 1 $s 2 $t 0 0 add 0 17 18 8 0 32 000000 10001 10010 01000 00000 10000001100100100001000002 = 0232402016 Chapter 2 — Instructions: Language of the Computer — 21
Hexadecimal n Base 16 n n 0 1 2 3 n Compact representation of bit strings 4 bits per hex digit 0000 0001 0010 0011 4 5 6 7 0100 0101 0110 0111 8 9 a b 1000 1001 1010 1011 c d e f 1100 1101 1110 1111 Example: eca 8 6420 n 1110 1100 1010 1000 0110 0100 0010 0000 Chapter 2 — Instructions: Language of the Computer — 22
MIPS I-format Instructions n rs rt constant or address 6 bits 5 bits 16 bits Immediate arithmetic and load/store instructions n n op rt: destination or source register number Constant: – 215 to +215 – 1 Address: offset added to base address in rs Design Principle 4: Good design demands good compromises n n Different formats complicate decoding, but allow 32 -bit instructions uniformly Keep formats as similar as possible Chapter 2 — Instructions: Language of the Computer — 23
Stored Program Computers The BIG Picture n n n Instructions represented in binary, just like data Instructions and data stored in memory Programs can operate on programs n n e. g. , compilers, linkers, … Binary compatibility allows compiled programs to work on different computers n Standardized ISAs Chapter 2 — Instructions: Language of the Computer — 24
n n Instructions for bitwise manipulation Operation C Java MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR | | or, ori Bitwise NOT ~ ~ nor § 2. 6 Logical Operations Useful for extracting and inserting groups of bits in a word Chapter 2 — Instructions: Language of the Computer — 25
Shift Operations n n rs rt rd shamt funct 6 bits 5 bits 6 bits shamt: how many positions to shift Shift left logical n n n op Shift left and fill with 0 bits sll by i bits multiplies by 2 i Shift right logical n n Shift right and fill with 0 bits srl by i bits divides by 2 i (unsigned only) Chapter 2 — Instructions: Language of the Computer — 26
AND Operations n Useful to mask bits in a word n Select some bits, clear others to 0 and $t 0, $t 1, $t 2 0000 0000 1101 1100 0000 $t 1 0000 0011 1100 0000 $t 0 0000 0000 1100 0000 Chapter 2 — Instructions: Language of the Computer — 27
OR Operations n Useful to include bits in a word n Set some bits to 1, leave others unchanged or $t 0, $t 1, $t 2 0000 0000 1101 1100 0000 $t 1 0000 0011 1100 0000 $t 0 0000 0011 1100 0000 Chapter 2 — Instructions: Language of the Computer — 28
NOT Operations n Useful to invert bits in a word n n Change 0 to 1, and 1 to 0 MIPS has NOR 3 -operand instruction n a NOR b == NOT ( a OR b ) nor $t 0, $t 1, $zero Register 0: always read as zero $t 1 0000 0011 1100 0000 $t 0 1111 1100 0011 1111 Chapter 2 — Instructions: Language of the Computer — 29
n Branch to a labeled instruction if a condition is true n n beq rs, rt, L 1 n n if (rs == rt) branch to instruction labeled L 1; bne rs, rt, L 1 n n Otherwise, continue sequentially § 2. 7 Instructions for Making Decisions Conditional Operations if (rs != rt) branch to instruction labeled L 1; j L 1 n unconditional jump to instruction labeled L 1 Chapter 2 — Instructions: Language of the Computer — 30
Compiling If Statements n C code: if (i==j) f = g+h; else f = g-h; n n f, g, … in $s 0, $s 1, … Compiled MIPS code: bne add j Else: sub Exit: … $s 3, $s 4, Else $s 0, $s 1, $s 2 Exit $s 0, $s 1, $s 2 Assembler calculates addresses Chapter 2 — Instructions: Language of the Computer — 31
Compiling Loop Statements n C code: while (save[i] == k) i += 1; n n i in $s 3, k in $s 5, address of save in $s 6 Compiled MIPS code: Loop: sll add lw bne addi j Exit: … $t 1, $t 0, $s 3, Loop $s 3, 2 $t 1, $s 6 0($t 1) $s 5, Exit $s 3, 1 Chapter 2 — Instructions: Language of the Computer — 32
Basic Blocks n A basic block is a sequence of instructions with n n No embedded branches (except at end) No branch targets (except at beginning) n n A compiler identifies basic blocks for optimization An advanced processor can accelerate execution of basic blocks Chapter 2 — Instructions: Language of the Computer — 33
More Conditional Operations n Set result to 1 if a condition is true n n slt rd, rs, rt n n if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant n n Otherwise, set to 0 if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t 0, $s 1, $s 2 bne $t 0, $zero, L # if ($s 1 < $s 2) # branch to L Chapter 2 — Instructions: Language of the Computer — 34
Branch Instruction Design n n Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠ n n Combining with branch involves more work per instruction, requiring a slower clock All instructions penalized! beq and bne are the common case This is a good design compromise Chapter 2 — Instructions: Language of the Computer — 35
Signed vs. Unsigned n n n Signed comparison: slt, slti Unsigned comparison: sltu, sltui Example n n n $s 0 = 1111 1111 $s 1 = 0000 0000 0001 slt $t 0, $s 1 # signed n n – 1 < +1 $t 0 = 1 sltu $t 0, $s 1 n # unsigned +4, 294, 967, 295 > +1 $t 0 = 0 Chapter 2 — Instructions: Language of the Computer — 36
n Steps required 1. 2. 3. 4. 5. 6. Place parameters in registers Transfer control to procedure Acquire storage for procedure Perform procedure’s operations Place result in register for caller Return to place of call § 2. 8 Supporting Procedures in Computer Hardware Procedure Calling Chapter 2 — Instructions: Language of the Computer — 37
Register Usage n n n $a 0 – $a 3: arguments (reg’s 4 – 7) $v 0, $v 1: result values (reg’s 2 and 3) $t 0 – $t 9: temporaries n n $s 0 – $s 7: saved n n n Can be overwritten by callee Must be saved/restored by callee $gp: global pointer for static data (reg 28) $sp: stack pointer (reg 29) $fp: frame pointer (reg 30) $ra: return address (reg 31) Chapter 2 — Instructions: Language of the Computer — 38
Procedure Call Instructions n Procedure call: jump and link jal Procedure. Label n Address of following instruction put in $ra n Jumps to target address n Procedure return: jump register jr $ra n Copies $ra to program counter n Can also be used for computed jumps n e. g. , for case/switch statements Chapter 2 — Instructions: Language of the Computer — 39
Leaf Procedure Example n C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } n Arguments g, …, j in $a 0, …, $a 3 n f in $s 0 (hence, need to save $s 0 on stack) n Result in $v 0 Chapter 2 — Instructions: Language of the Computer — 40
Leaf Procedure Example n MIPS code: leaf_example: addi $sp, -4 sw $s 0, 0($sp) add $t 0, $a 1 add $t 1, $a 2, $a 3 sub $s 0, $t 1 add $v 0, $s 0, $zero lw $s 0, 0($sp) addi $sp, 4 jr $ra Save $s 0 on stack Procedure body Result Restore $s 0 Return Chapter 2 — Instructions: Language of the Computer — 41
Non-Leaf Procedures n n Procedures that call other procedures For nested call, caller needs to save on the stack: n n n Its return address Any arguments and temporaries needed after the call Restore from the stack after the call Chapter 2 — Instructions: Language of the Computer — 42
Non-Leaf Procedure Example n C code: int fact (int n) { if (n < 1) return f; else return n * fact(n - 1); } n Argument n in $a 0 n Result in $v 0 Chapter 2 — Instructions: Language of the Computer — 43
Non-Leaf Procedure Example n MIPS code: fact: addi sw sw slti beq addi jr L 1: addi jal lw lw addi mul jr $sp, $ra, $a 0, $t 0, $v 0, $sp, $ra $a 0, fact $a 0, $ra, $sp, $v 0, $ra $sp, -8 4($sp) 0($sp) $a 0, 1 $zero, L 1 $zero, 1 $sp, 8 $a 0, -1 0($sp) 4($sp) $sp, 8 $a 0, $v 0 # # adjust stack for 2 items save return address save argument test for n < 1 # # # # # if so, result is 1 pop 2 items from stack and return else decrement n recursive call restore original n and return address pop 2 items from stack multiply to get result and return Chapter 2 — Instructions: Language of the Computer — 44
Local Data on the Stack n Local data allocated by callee n n e. g. , C automatic variables Procedure frame (activation record) n Used by some compilers to manage stack storage Chapter 2 — Instructions: Language of the Computer — 45
Memory Layout n n Text: program code Static data: global variables n n n Dynamic data: heap n n e. g. , static variables in C, constant arrays and strings $gp initialized to address allowing ±offsets into this segment E. g. , malloc in C, new in Java Stack: automatic storage Chapter 2 — Instructions: Language of the Computer — 46
n Byte-encoded character sets n ASCII: 128 characters n n Latin-1: 256 characters n n 95 graphic, 33 control ASCII, +96 more graphic characters § 2. 9 Communicating with People Character Data Unicode: 32 -bit character set n n n Used in Java, C++ wide characters, … Most of the world’s alphabets, plus symbols UTF-8, UTF-16: variable-length encodings Chapter 2 — Instructions: Language of the Computer — 47
Byte/Halfword Operations n n Could use bitwise operations MIPS byte/halfword load/store n String processing is a common case lb rt, offset(rs) n Sign extend to 32 bits in rt lbu rt, offset(rs) n lhu rt, offset(rs) Zero extend to 32 bits in rt sb rt, offset(rs) n lh rt, offset(rs) sh rt, offset(rs) Store just rightmost byte/halfword Chapter 2 — Instructions: Language of the Computer — 48
String Copy Example n C code (naïve): Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='