Computing Systems Instructions: language of the computer.

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Goal: Write Programs in Assembly
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2 Instructions: Language of the Computer
Chapter 2.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g., MIPS Arithmetic.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
1  1998 Morgan Kaufmann Publishers Chapter 3 Text in blue is by N. Guydosh Updated 1/27/04 Instructions: Language of the Machine.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
1  2004 Morgan Kaufmann Publishers Chapter 2. 2  2004 Morgan Kaufmann Publishers Instructions: Language of the Machine We’ll be working with the MIPS.
MIPS Instruction Set Advantages
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
1  2004 Morgan Kaufmann Publishers Instructions: bne $t4,$t5,Label Next instruction is at Label if $t4≠$t5 beq $t4,$t5,Label Next instruction is at Label.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
순천향대학교 정보기술공학부 이 상 정 1 2. Instructions: Language of the Computer.
1 CS/EE 362 Hardware Fundamentals Lecture 10 (Chapter 3: Hennessy and Patterson) Winter Quarter 1998 Chris Myers.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1  1998 Morgan Kaufmann Publishers Machine Instructions: Language of the Machine Lowest level of programming, control directly the hardware Assembly instructions.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? Why not? put 'typical constants' in memory.
CHAPTER 6 Instruction Set Architecture 12/7/
Chapter 2 Decision-Making Instructions (Instructions: Language of the Computer Part V)
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Computer Organization Rabie A. Ramadan Lecture 3.
EE472 – Spring 2007P. Chiang, with Slide Help from C. Kozyrakis (Stanford) ECE472 Computer Architecture Lecture #3—Oct. 2, 2007 Patrick Chiang TA: Kang-Min.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Instruction Set Architecture Chapter 3 – P & H. Introduction Instruction set architecture interface between programmer and CPU Good ISA makes program.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
1  1998 Morgan Kaufmann Publishers Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow.
Computer Architecture Instruction Set Architecture
MIPS Instruction Set Advantages
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Systems Architecture Lecture 5: MIPS Instruction Set
Henk Corporaal TUEindhoven 2010
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Instruction encoding The ISA defines Format = Encoding
Computer Architecture
September 17 Test 1 pre(re)view Fang-Yi will demonstrate Spim
COMS 361 Computer Organization
COMS 361 Computer Organization
Systems Architecture I
Computer Architecture
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Presentation transcript:

Computing Systems Instructions: language of the computer

2 Instructions  Instructions are the language of the machine  We’ll be work with the MIPS instruction set architecture  Design goals  find a language that make it easy to build the hardware and the compiler  maximizing performance and minimizing cost  Stored-program concept  programs (= instructions and data) can be stored in memory as numbers  Fetch & execute cycles  Fetch the instruction from memory and put it into a special register (IR)  The bits in the register “control” the subsequent actions required to execute the task specified by the instruction continue with “next” instruction

3 Instruction set characteristic  Small  Few primitive instructions  Simple  Few instruction formats  Regular  Instructions can be handled in similar ways  Complete  support all kind of high level language instructions  Efficient  High level language instructions can be mapped efficiently  Compatible

4 Definition of the architecture  Data types - bit, byte, word, unsigned integer, char, …  Operations - arithmetic, logical, shift, flow control, data transfers, …  # of operands (3, 2, 1, or 0 operands) - number of operands affect the instruction length  Registers  Memory organization

5 MIPS arithmetic  all arithmetic instructions have 3 operands  operands order is fixed (destination first) Design principle: Simplicity favors regularity. Why ?  of course this complicate some things … C codeMIPS code c = a+badd $s0, $s1, $s2 C codeMIPS code f = (g + h) – (i + j) add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1

6 Registers vs. memory  Arithmetic operands must be registers, - only 32 registers provided - each register is 32 bits (word) Design Principle: Smaller is faster. Why ?  Compiler associates variables with registers, but …  What about programs with lots of variable or complex data structures ?  Only a small amount of data can be kept in the computer’s registers, the rest is kept in memory  We’ll need instructions that transfer data between memory and registers Absolute truth ? ProcessorI/O Control Datapath Memory Input Output

7 Memory organization  Viewed as a large, single-dimension array, with an address.  A memory address is an index into the array  "Byte addressing" means that the index points to a byte of memory bits of data

8 Memory organization  Bytes are nice, but most data items use larger "words"  For MIPS, a memory word is 32 bits wide (= 4 bytes)  2 32 bytes with byte addresses from 0 to  2 30 words with byte addresses 0, 4, 8, – 4  Words are aligned (alignment restriction)  words start at addresses that are multiple of 4  what are the least 2 significant bits of a word address?

9 Bytes order within a word  Which byte is first and which is last ?  There are two choices  Least significant byte is the at rightmost end (= little end)  Least significant byte is the at leftmost end (= big end)  MIPS uses Big Endian Big Endian Little Endian

10 MIPS data transfer instructions  load (it copies data from memory to a register)  store (copies data from a register to memory)  Memory address for load and store has two parts  A register whose contents is known (called base register or index register)  An offset to be added to the base register content  This way the address is 32 bits  The offset can be positive or negative (2’s complement) C codeMIPS codeMeaning A[12] = h + A[8] lw $t0, 32($s3) add $t0, $s2, $t0 sw $t0, 48($s3) $t0 = Memory[$s3 + 32] $t0 = $s2 + $t0 Memory[$s3 + 48] = $t0

11 Machine language  MIPS assembly instructions are translated into machine instructions (“binary numbers”)  Instructions, like registers and words of data, are also 32 bits long  Example: add $t1, $s1, $s2  registers are represented as numbers: $t1=9, $s1=17, $s2=18, …  The format above is called R-format (for register)  Can you guess what the field names stand for? 6 bits5 bits 6 bits

12 Machine language  Consider the load-word and store-word instructions  We must specify two registers and a constant (=immediate operand)  Regularity principle would suggest to use for the constant one of the 5-bit fields  Problem: the constant would be limited to only 32 !!!)  Solution: we introduce a new format called I-type (for immediate)  The 16 bit number is in 2’s complement form Design principle: good design demands good compromises 6 bits5 bits 16 bits

13 Examples $t0 (rt) $s3 (rs) sw (op) lw (rs) $s2 (rt) $t0

14 MIPS logical operations The above instructions are all R-type Logical operationExampleMeaning shift left logical sll $s1, $s2, 10$s1 = $s2 << 10 shift right logical srl $s1, $s2, 10$s1 = $s2 >> 10 bit-by-bit and and $s1, $s2, $s3$s1 = $s2 & $s3 bit-by-bit oror $s1, $s2, $s3$s1 = $s2 | $s3 bit-by-bit nornor $s1, $s2, $s3$s1 = ~ ($s2 | $s3)

15 Example  sll $s1, $s2, 10  srl $s1, $s3, oprtrdshamtfunc oprtrdshamtfunc

16 Control flow  Decision making instructions  alter the program control flow,  i.e., change the "next" instruction to be executed  MIPS conditional branch instructions:  bne $t0, $t1, Label  beq $t0, $t1, Label C codeMIPS code if (i==j) h=i+j; bne $s3, $s4, Label add $s5, $s3, $s4 Label: …

17 Control flow  MIPS unconditional branch instruction (jump)  j Label Can you build a simple for loop? C codeMIPS code if (i==j) f=g+h; else f=g-h bne $t0, $t1, Else add $s5, $s3, $s4 j Exit Else: sub $s5, $s3, $s4 Exit: …

18 Control flow  We have: beq (test for equality) and bne (test for inequality)  But, sometime is useful to see if a variable is less than another one (branch-less-than)  MIPS provides the instruction set-on-less-than (slt)  The format is R-type MeaningMIPS code if (s1<s2) t0=1; else t0=0 slt $t0, $s1, $s2

19 Control flow  Case switch statement  can be implemented as a chain of if-then-else statements  or more efficiently as a table of addresses  MIPS provides a jump register instruction (jr)  It is an unconditional jump to the address specified in a register  Example: jr $s0

20 Control flow – Instruction formats  slt and jr are R-format oprsrtrdshamtfunc slt $s1, $s2, $s oprsfunc jr $s1

21 Control flow – Instruction formats  beq and bne are I-format instructions  The 16-bit number is in 2’s complement form  The 16-bit number specifies the # of instructions to be skipped  Most branches are local [Principle of locality]  Next memory address = PC + (16-bit number x 4) oprsrt16-bit number beq $s1, $s2, 100

22 Control flow – Instruction formats  The unconditional jump instruction requires a new instruction format (J-format)  Example: j  The address of the next instruction is obtained by concatenating the 4 upper bits of the PC with the 26-bit address shifted left 2 bits  Next memory address = {PC[31:28], INS[25:0], 2’b00}  Address boundaries of 256 MB = (2 28 ) op26-bit number

23 MIPS Registers convention Register 1 ($at) reserved for assembler, ($k0-$k1) for operating system

24 Constants  Small constant operands occur quite frequently in programs (50% of operands)  By including constants inside instructions, execution become much faster than if constants were loaded from memory Design Principle: make the common case fast w/o immediate instructionwith immediate instruction lw $t0, AddrConstant4($s1) add $s3, $s3, $t0 addi $s3,$s3,4

25 Constants  Why doesn’t MIPS have a subtract immediate operation ?  Constants are frequently short and fit into the 16-bit field  But, what if they are bigger ?  It would be convenient to have a 32-bit constant or address !!!! CategoryInstructionExampleMeaning Arithmeticadd immediateaddi $s1, $s2, 100$s1 = $s Logical and immediateandi $s1, $s2,100$s1 = $s2 & 100 or immediateori $s1, $s2, 100$s1 = $s2 | 100 Conditional branch set less than immediate slti $s1, $s2, 100 If ($s2<100) $s1=1 else $s1=0

26 How about large constants ?  The compiler or the assembler must break large constants into pieces and then reassemble them into registers  We'd like to be able to load a 32 bit constant into a register  Must use two instructions, new "load upper immediate" instruction  Then must get the lower order bits right, i.e., ori $t0, $t0, filled with zeros ori lui $t0,

27 Assembly Language vs. Machine Language  Assembly provides convenient symbolic representation  much easier than writing down numbers  e.g., destination first  Machine language is the underlying reality  e.g., destination is no longer first  Assembly can provide 'pseudoinstructions'  e.g., “move $t0, $t1” exists only in assembly  would be implemented using “add $t0,$t1,$zero”  When considering performance you should count real instructions

28 Translating and starting a program

29 Overview of MIPS  simple instructions all 32 bits wide  very structured  only three instruction formats  rely on compiler to achieve performance op rs rt rdshamtfunct op 26 bit number RIJRIJ op rs rt 16 bit number

30 Summary – MIPS operands

31 Summary – MIPS instructions

32 Addressing modes in MIPS  Register addressing (e.g., add, sll, nor, slt, …)  the operand is a register  Immediate addressing (e.g., addi, ori, …)  the operand is a constant within the instruction  Base addressing (e.g. lw, sw) [register+offset]  The operand is at the memory location whose address is the sum of a register and a constant in the instruction  PC-relative addressing (e.g., bne, beq) [PC + offset]  The address is the sum of the PC and a constant in the instruction  Pseudo-direct addressing (e.g., j) [PC concatenation]  The address is the concatenation of a value in the instruction with the upper bits of the PC

33 Addressing modes in MIPS

34 Alternative architectures  Design alternative:  provide more powerful operations  goal is to reduce number of instructions executed  danger is a slower cycle time and/or a higher CPI  RISC vs. CISC  virtually all instruction sets since 1982 are RISC  common architectures: 80x86, IA-32, PowerPC, …

35 “Spilling” registers  Many programs have more variables than computers have registers  The compiler tries to keep the most frequently used variables in registers and places the rest in memory  The process of putting less commonly used variables (or those needed later) into memory is called spilling registers

36 Supporting procedures  Goal:  structure programs so that  it is easier to understand and reuse code  Execution of a procedure 1. place parameters in a place where the procedure can access them 2. transfer control to the procedure 3. acquire the storage resources needed for the procedure 4. Place the result value in a place where the calling program can access it 5. Return control to the point of origin  Basic MIPS facilities  $a0-$a3  4 argument registers in which to pass parameters  $v0-$v1  2 value registers in which to return values  $ra  return address register to return to the point of origin  jal ProcedureAddress  jump-and-link (it saves PC+4 in $ra)  jr $ra  jump register

37 What if we need more registers ?  A compiler may need more registers for a procedure than the four argument and two return value registers  The local variables we need may not even fit in the MIPS registers  also any register needed by the caller must be restored to the values they contained before the procedure was invoked  we need to spill the registers to memory  The ideal data structure for spilling registers is a stack (last in first out)  push – place data on the stack  pop – remove data from the stack

38 The stack  MIPS allocate a register just for the stack: the stack pointer ($sp)  it points to the most recently allocated address in the stack  Stacks grow from higher addresses to lower addresses  push values on the stack by subtracting from the $sp  pop values from the stack by adding to the $sp

39 Example – compiling a leaf procedure int leaf_example (int g, int h, int i, int j) { int f; f = (g+h) – (i+j); return f; } … leaf_example main … … jal leaf_example … Memory

40 Example – compiling a leaf procedure leaf_example: addi $sp,$sp,-12 # adjust stack to make room for 3 items sw $t1, 8($sp) # save $t1 for later us by the caller sw $t0, 4($sp) # save $t0 for later us by the caller sw $s0, 0($sp) # save $s0 for later us by the caller add $t0,$a0,$a1 # t0 = g+h add $t1,$a2,$a3 # t1 = i+j sub $s0,$t0,$t1 # s0 = t0–t1 = (g+h)-(i+j) add $v0,$s0,$zer0 # v0 = s0+0 (return f) lw $s0, 0($sp) # restore $s0 for caller lw $t0, 4($sp) # restore $t0 for caller lw $t1, 8($sp) # restore $t1 for caller addi $sp,$sp,12 # adjust stack to delete 3 items jr $ra # jump back to calling routine

41 Reduce register spilling  To avoid saving and restoring a register whose value is never used, MIPS separate temporary registers from saved registers:  $t0-$t9: are not preserved by the callee (called procedure)  $s0-$s7: must be preserved on a procedure call (if used, the callee saves and restore them)  Thus, the assembly code generated for leaf_example by a compiler can be more compact !!!  Can you guess how the code should look like ?  When a compiler finds a leaf procedure it exhaust all temporary registers before using registers it must save

42 Concluding Remarks  Instruction complexity is only one variable  lower instruction count vs. higher CPI / lower clock rate  Design Principles:  simplicity favors regularity  smaller is faster  good design demands compromise  make the common case fast  Instruction set architecture  a very important abstraction !

43 Common misconceptions and mistakes  More powerful instructions mean higher performance  Write in assembly language give the highest performance  Forgetting that sequential word addresses in machines with byte addressing do not differ by one  Using a pointer to an automatic variable outside its defining procedure