Processor Design 5Z032 Instructions: Language of the Computer Henk Corporaal Eindhoven University of Technology 2011.

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
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
The University of Adelaide, School of Computer Science
1 ECE462/562 ISA and Datapath Review Ali Akoglu. 2 Instruction Set Architecture A very important abstraction –interface between hardware and low-level.
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
Systems Architecture Lecture 5: MIPS Instruction Set
1 INSTRUCTIONS: LANGUAGE OF THE MACHINE CHAPTER 3.
Chapter 2 Instructions: Language of the Computer
Chapter 2.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
Embedded Systems in Silicon TD5102 MIPS Instruction Set Architecture Henk Corporaal Technical University.
1 Chapter 3: Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive e.g.,
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.
1 Chapter Introduction Language of the Machine We’ll be working with the MIPS instruction set architecture –similar to other architectures developed.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
1 CSE SUNY New Paltz Chapter 3 Machine Language Instructions.
1 Lecture 5: MIPS Examples Today’s topics:  the compilation process  full example – sort in C Reminder: 2 nd assignment will be posted later today.
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.
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.
1  2004 Morgan Kaufmann Publishers Chapter 2. 2  2004 Morgan Kaufmann Publishers Instructions: Language of the Machine We’ll be working with the MIPS.
©UCB CPSC 161 Lecture 5 Prof. L.N. Bhuyan
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Computing Systems Instructions: language of the computer.
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.
순천향대학교 정보기술공학부 이 상 정 1 2. Instructions: Language of the Computer.
1 EGRE 426 Fall 09 Handout Pipeline examples continued from last class.
1 CS/EE 362 Hardware Fundamentals Lecture 10 (Chapter 3: Hennessy and Patterson) Winter Quarter 1998 Chris Myers.
1  1998 Morgan Kaufmann Publishers Machine Instructions: Language of the Machine Lowest level of programming, control directly the hardware Assembly instructions.
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
1 Copyright 1998 Morgan Kaufmann Publishers, Inc. All rights reserved. MIPS Assembly language In computer programs we have : Data Instructions (Arithmetic.
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/
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
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.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
Instruction Set Architecture Chapter 3 – P & H. Introduction Instruction set architecture interface between programmer and CPU Good ISA makes program.
1  1998 Morgan Kaufmann Publishers Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow.
Chapter 2 Instructions: Language of the Computer.
ECE3055 Computer Architecture and Operating Systems Chapter 2: Procedure Calls & System Software These lecture notes are adapted from those of Professor.
Lecture 6: Assembly Programs
CS2100 Computer Organisation
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Systems Architecture Lecture 5: MIPS Instruction Set
Henk Corporaal TUEindhoven 2010
The University of Adelaide, School of Computer Science
September 17 Test 1 pre(re)view Fang-Yi will demonstrate Spim
COMS 361 Computer Organization
Lecture 6: Assembly Programs
COMS 361 Computer Organization
Machine Instructions.
Computer Architecture
Instruction Set Architecture
Presentation transcript:

Processor Design 5Z032 Instructions: Language of the Computer Henk Corporaal Eindhoven University of Technology 2011

TU/e Processor Design 5Z0322 Topics n Instructions & MIPS instruction set n Where are the operands ? n Machine language n Assembler n Translating C statements into Assembler n Other architectures: u PowerPC u Intel 80x86 n More complex stuff, like: u while statement u switch statement u procedure / function (leaf and nested) u stack u linking object files

TU/e Processor Design 5Z0323 Instructions: n Language of the Machine n More primitive than higher level languages e.g., no sophisticated control flow n Very restrictive e.g., MIPS Arithmetic Instructions n We’ll be working with the MIPS instruction set architecture u similar to other architectures developed since the 1980's u used by NEC, Nintendo, Silicon Graphics, Sony Design goals: maximize performance and minimize cost, reduce design time, reduce energy consumption

TU/e Processor Design 5Z0324 Main Types of Instructions n Arithmetic u Integer u Floating Point n Memory access instructions u Load & Store n Control flow u Jump u Conditional Branch u Call & Return

TU/e Processor Design 5Z0325 MIPS arithmetic n Most instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B + C MIPS code: add $s0, $s1, $s2 ($s0, $s1 and $s2 are associated with variables by compiler)

TU/e Processor Design 5Z0326 MIPS arithmetic C code: A = B + C + D; E = F - A; MIPS code: add $t0, $s1, $s2 add $s0, $t0, $s3 sub $s4, $s5, $s0 n Operands must be registers, only 32 registers provided n Design Principle: smaller is faster. Why?

TU/e Processor Design 5Z0327 Registers vs. Memory n Arithmetic instructions operands must be registers, — only 32 registers provided n Compiler associates variables with registers n What about programs with lots of variables ? CPU Memory IO register file

TU/e Processor Design 5Z0328 Register allocation n Compiler tries to keep as many variables in registers as possible n Some variables can not be allocated u large arrays (too few registers) u aliased variables (variables accessible through pointers in C) u dynamic allocated variables F heap F stack n Compiler may run out of registers => spilling

TU/e Processor Design 5Z0329 Memory Organization n Viewed as a large, single-dimension array, with an address. n A memory address is an index into the array n "Byte addressing" means that the index points to a byte of memory bits of data

TU/e Processor Design 5Z03210 Memory Organization n Bytes are nice, but most data items use larger "words" n For MIPS, a word is 32 bits or 4 bytes. n 2 32 bytes with byte addresses from 0 to n 2 30 words with byte addresses 0, 4, 8, bits of data Registers hold 32 bits of data

TU/e Processor Design 5Z03211 Memory layout: Alignment n Words are aligned i.e., what are the least 2 significant bits of a word address? this word is aligned; the others are not! address

TU/e Processor Design 5Z03212 Instructions n Load and store instructions Example: C code: A[8] = h + A[8]; MIPS code: lw $t0, 32($s3) add $t0, $s2, $t0 sw $t0, 32($s3) n Store word operation has no destination (reg) operand n Remember arithmetic operands are registers, not memory!

TU/e Processor Design 5Z03213 Our First C Example n Can we figure out the code? swap(int v[], int k); { int temp; temp = v[k] v[k] = v[k+1]; v[k+1] = temp; } swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 Explanation: index k : $5 base address of v: $4 address of v[k] is $4 + 4.$5

TU/e Processor Design 5Z03214 So far we’ve learned: n MIPS — loading words but addressing bytes — arithmetic on registers only Instruction Meaning add $s1, $s2, $s3 $s1 = $s2 + $s3 sub $s1, $s2, $s3 $s1 = $s2 – $s3 lw $s1, 100($s2) $s1 = Memory[$s2+100] sw $s1, 100($s2) Memory[$s2+100] = $s1

TU/e Processor Design 5Z03215 n Instructions, like registers and words of data, are also 32 bits long  Example: add $t0, $s1, $s2  Registers have numbers: $t0=9, $s1=17, $s2=18 n Instruction Format: Machine Language  Can you guess what the field names stand for? 6 bits 5 bits 6 bits5 bits op rs rt rd shamt funct

TU/e Processor Design 5Z03216 n Consider the load-word and store-word instructions, u What would the regularity principle have us do? u New principle: Good design demands a compromise n Introduce a new type of instruction format u I-type for data transfer instructions u other format was R-type for register Example: lw $t0, 32($s2) op rs rt 16 bit number n Where's the compromise? n Study example page Machine Language

TU/e Processor Design 5Z03217 n Instructions are bits n Programs are stored in memory — to be read or written just like data n Fetch & Execute Cycle u Instructions are fetched and put into a special register u Bits in the register "control" the subsequent actions u Fetch the “next” instruction and continue ProcessorMemory memory for data, programs, compilers, editors, etc. Stored Program Concept

TU/e Processor Design 5Z03218 Stored Program Concept memory OS Program 1 Program 2 CPU code data unused

TU/e Processor Design 5Z03219 n Decision making instructions u alter the control flow, u i.e., change the "next" instruction to be executed MIPS conditional branch instructions: bne $t0, $t1, Label beq $t0, $t1, Label Example: if (i==j) h = i + j; bne $s0, $s1, Label add $s3, $s0, $s1 Label:.... Control

TU/e Processor Design 5Z03220 MIPS unconditional branch instructions: j label Example: if (i!=j) beq $s4, $s5, Lab1 h=i+j; add $s3, $s4, $s5 else j Lab2 h=i-j;Lab1:sub $s3, $s4, $s5 Lab2:... n Can you build a simple for loop? Control

TU/e Processor Design 5Z03221 So far: n Instruction Meaning add $s1,$s2,$s3 $s1 = $s2 + $s3 sub $s1,$s2,$s3 $s1 = $s2 – $s3 lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1 bne $s4,$s5,L Next instr. is at Label if $s4 ° $s5 beq $s4,$s5,L Next instr. is at Label if $s4 = $s5 j Label Next instr. is at Label n Formats: op rs rt rdshamtfunct op rs rt 16 bit address op 26 bit address RIJRIJ

TU/e Processor Design 5Z03222 n We have: beq, bne, what about Branch-if-less-than? New instruction: if $s1 < $s2 then $t0 = 1 slt $t0, $s1, $s2 else $t0 = 0 Can use this instruction to build " blt $s1, $s2, Label " — can now build general control structures n Note that the assembler needs a register to do this, — use conventions for registers Control Flow

TU/e Processor Design 5Z03223 Used MIPS Conventions

TU/e Processor Design 5Z03224 n Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; n Solutions? Why not? u put 'typical constants' in memory and load them u create hard-wired registers (like $zero) for constants like one MIPS Instructions: addi $29, $29, 4 slti $8, $18, 10 andi $29, $29, 6 ori $29, $29, 4 n How do we make this work? 3 Constants

TU/e Processor Design 5Z03225 n We'd like to be able to load a 32 bit constant into a register Must use two instructions, new "load upper immediate" instruction lui $t0, ori filled with zeros How about larger constants? Then must get the lower order bits right, i.e., ori $t0, $t0,

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

TU/e Processor Design 5Z03227 n Not yet covered: u support for procedures u linkers, loaders, memory layout u stacks, frames, recursion u manipulating strings and pointers u interrupts and exceptions u system calls and conventions n Some of these we'll talk about later n We've focused on architectural issues u basics of MIPS assembly language and machine code u we’ll build a processor to execute these instructions Other Issues

TU/e Processor Design 5Z03228 n simple instructions all 32 bits wide n very structured, no unnecessary baggage n only three instruction formats n rely on compiler to achieve performance — what are the compiler's goals? n help compiler where we can op rs rt rdshamtfunct op rs rt 16 bit address op 26 bit address RIJRIJ Overview of MIPS

TU/e Processor Design 5Z03229 n Instructions: bne $t4,$t5,Label Next instruction is at Label if $t4  $t5 beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5 j Label Next instruction is at Label n Formats: n Addresses are not 32 bits — How do we handle this with load and store instructions? op rs rt 16 bit address op 26 bit address IJIJ Addresses in Branches and Jumps

TU/e Processor Design 5Z03230 n Instructions: bne $t4,$t5,Label Next instruction is at Label if $t4  $t5 beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5 n Formats: n Could specify a register (like lw and sw) and add it to address u use Instruction Address Register (PC = program counter) u most branches are local (principle of locality) n Jump instructions just use high order bits of PC u address boundaries of 256 MB op rs rt 16 bit address I Addresses in Branches

TU/e Processor Design 5Z03231 To summarize:

TU/e Processor Design 5Z03232 To summarize:

TU/e Processor Design 5Z03233 MIPS addressing modes summary

TU/e Processor Design 5Z03234 n Design alternative: u provide more powerful operations u goal is to reduce number of instructions executed u danger is a slower cycle time and/or a higher CPI n Sometimes referred to as “RISC vs. CISC” debate u virtually all new instruction sets since 1982 have been RISC  VAX: minimize code size, make assembly language easy instructions from 1 to 54 bytes long! n We’ll look at PowerPC and 80x86 Alternative Architectures

TU/e Processor Design 5Z03235 PowerPC n Indexed addressing  example: lw $t1,$a0+$s3 #$t1=Memory[$a0+$s3] u What do we have to do in MIPS? n Update addressing u update a register as part of load (for marching through arrays)  example: lwu $t0,4($s3) #$t0=Memory[$s3+4];$s3=$s3+4 u What do we have to do in MIPS? n Others: u load multiple/store multiple  a special counter register “ bc Loop ” decrement counter, if not 0 goto loop

TU/e Processor Design 5Z03236 A dominant architecture: x86/IA-32 Historic Highlights: n 1978: The Intel 8086 is announced (16 bit architecture) n 1980: The 8087 floating point coprocessor is added n 1982: The increases address space to 24 bits, +instructions n 1985: The extends to 32 bits, new addressing modes n : The 80486, Pentium, Pentium Pro add a few instructions (mostly designed for higher performance) n 1997: Pentium II with MMX is added n 1999: Pentium III, with 70 more SIMD instructions n 2001: Pentium IV, very deep pipeline (20 stages) results in high freq. n 2003: Pentium IV – Hyperthreading n 2005: Multi-core solutions n 2006: Adding virtualization support (AMD-V and Intel VT-x) n 2010: AVX: Advanced vector ext.: SIMD using bit registers

TU/e Processor Design 5Z03237 Historical overview

TU/e Processor Design 5Z03238 A dominant architecture: 80x86 n See your textbook for a more detailed description n Complexity: u Instructions from 1 to 17 bytes long u one operand must act as both a source and destination u one operand can come from memory u complex addressing modes e.g., “base or scaled index with 8 or 32 bit displacement” n Saving grace: u the most frequently used instructions are not too difficult to build u compilers avoid the portions of the architecture that are slow “what the 80x86 lacks in style is made up in quantity, making it beautiful from the right perspective”

TU/e Processor Design 5Z03239 n Instruction complexity is only one variable u lower instruction count vs. higher CPI / lower clock rate n Design Principles: u simplicity favors regularity u smaller is faster u good design demands compromise u make the common case fast n Instruction set architecture u a very important abstraction indeed! Summary (so far)

TU/e Processor Design 5Z03240 More complex stuff n While statement n Case/Switch statement n Procedure u leaf u non-leaf / recursive n Stack n Memory layout n Characters, Strings n Arrays versus Pointers n Starting a program u Linking object files

TU/e Processor Design 5Z03241 While statement while (save[i] == k) i=i+j; Loop: muli $t1,$s3,4 add $t1,$t1,$s6 lw $t0,0($t1) bne $t0,$s5,Exit add $s3,$s3,$s4 j Loop Exit: # calculate address of # save[i]

TU/e Processor Design 5Z03242 Case/Switch statement switch (k) { case 0: f=i+j; break; case 1: ; case 2: ; case 3: ; } 1. test if k inside calculate address of jump table location 3. fetch jump address and jump 4. code for all different cases (with labels L0-L3) address L0 address L1 address L2 address L3 Assembler Code:Data: jump table C Code (pg 129):

TU/e Processor Design 5Z03243 Compiling a leaf Procedure C code int leaf_example (int g, int h, int i, int j) { int f; f = (g+h)-(i+j); return f; } Assembler code leaf_example: save registers changed by callee code for expression ‘f =....’ (g is in $a0, h in $a1, etc.) put return value in $v0 restore saved registers jr $ra

TU/e Processor Design 5Z03244 Using a Stack $sp low address high address filled empty Save $s0 and $s1: subi $sp,$sp,8 sw $s0,4($sp) sw $s1,0($sp) Restore $s0 and $s1: lw $s0,4($sp) lw $s1,0($sp) addi $sp,$sp,8 Convention: $ti registers do not have to be saved and restored by callee They are scratch registers

TU/e Processor Design 5Z03245 Compiling a non-leaf procedure C code of ‘recursive’ factorial (pg 136) int fact (int n) { if (n<1) return (1) else return (n*fact(n-1)); } Factorial: n! = n* (n-1)! 0! = 1

TU/e Processor Design 5Z03246 Compiling a non-leaf procedure For non-leaf procedure n save arguments registers (if used) n save return address ($ra) n save callee used registers n create stack space for local arrays and structures (if any)

TU/e Processor Design 5Z03247 Compiling a non-leaf procedure Assembler code for ‘fact’ fact: subi $sp,$sp,8 # save return address sw $ra,4($sp) # and arg.register a0 sw $a0,0($sp) slti $to,$a0,1 # test for n<1 beq $t0,$zero,L1 # if n>= 1 goto L1 addi $v0,$zero,1 # return 1 addi $sp,$sp,8 # check this ! jr $ra L1: subi $a0,$a0,1 jal fact # call fact with (n-1) lw $a0,0($sp) # restore return address lw $ra,4($sp) # and a0 (in right order!) addi $sp,$sp,8 mul $v0,$a0,$v0 # return n*fact(n-1) jr $ra

TU/e Processor Design 5Z03248 How does the stack look? $sp low address high address filled 100 addi $a0,$zero,2 104 jal fact $ra = 108 $a0 = 2 $ra =... $a0 = 1 $ra =... $a0 = 0 Note: no callee regs are used Caller:

TU/e Processor Design 5Z03249 Beyond numbers: characters n Characters are often represented using the ASCII standard n ASCII = American Standard COde for Information Interchange n See table 3.15, page 142 n Note: value(a) - value(A) = 32 value(z) - value(Z) = 32

TU/e Processor Design 5Z03250 Beyond numbers: Strings n A string is a sequence of characters n Representation alternatives for “aap”: u including length field: 3’a’’a’’p’ u separate length field u delimiter at the end: ‘a’’a’’p’0 (Choice of language C !!) Discuss C procedure ‘strcpy’ void strcpy (char x[], char y[]) { int i; i=0; while ((x[i]=y[i]) != 0) /* copy and test byte */ i=i+1; }

TU/e Processor Design 5Z03251 String copy: strcpy strcpy: subi $sp,$sp,4 sw $s0,0($sp) add $s0,$zero,$zero # i=0 L1: add $t1,$a1,$s0 # address of y[i] lb $t2,0($t1) # load y[i] in $t2 add $t3,$a0,$s0 # similar address for x[i] sb $t2,0($t3) # put y[i] into x[i] addi $s0,$s0,1 bne $t2,$zero,L1 # if y[i]!=0 go to L1 lw $s0,0($sp) # restore old $s0 add1 $sp,$sp,4 jr $ra Note: strcpy is a leaf-procedure; no saving of args and return address required

TU/e Processor Design 5Z03252 Arrays versus pointers clear1 (int array[], int size) { int i; for (i=0; i<size; i=i+1) array[i]=0; } clear2 (int *array, int size) { int *p; for (p=&array[0]; p<&array[size]; p=p+1) *p=0; } Array version: Pointer version: Two programs which initialize an array to zero

TU/e Processor Design 5Z03253 Arrays versus pointers n Compare the assembly result on page 174 n Note the size of the loop body: u Array version: 7 instructions u Pointer version: 4 instructions n Pointer version much faster ! n Clever compilers perform pointer conversion themselves

TU/e Processor Design 5Z03254 Starting a program n Compile C program n Assemble n Link u insert library code u determine addresses of data and instruction labels u relocation: patch addresses n Load into memory u load text (code) u load data (global data)  initialize $sp, $gp u copy parameters to the main program onto the stack u jump to ‘start-up’ routine  copies parameters into $ai registers F call main

TU/e Processor Design 5Z03255 Starting a program C program compiler Assembly program assembler Object program (user module)Object programs (library) linker Executable loader Memory

TU/e Processor Design 5Z03256 Exercises n Make from chapter three the following exercises: u u 3.8 u 3.16 (calculate CPI for gcc only) u 3.19, 3.20