Embedded Systems in Silicon TD5102 MIPS Instruction Set Architecture Henk Corporaal Technical University.

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.
©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.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 8 Sept 23 Completion of Ch 2 translating procedure into MIPS role of compilers, assemblers, linking, loading etc. pitfalls, conclusions Chapter.
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.
Chapter 2 Instructions: Language of the Computer
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
The University of Adelaide, School of Computer Science
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 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.
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.
RISC Concepts, MIPS ISA and the Mini–MIPS project
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.
Processor Design 5Z032 Instructions: Language of the Computer Henk Corporaal Eindhoven University of Technology 2011.
©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,
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.
 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 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.
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.
Computer Architecture & Operations I
Lecture 6: Assembly Programs
CS2100 Computer Organisation
Lecture 4: MIPS Instruction Set
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
Henk Corporaal TUEindhoven 2010
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
COMS 361 Computer Organization
Lecture 6: Assembly Programs
Systems Architecture I
Machine Instructions.
Computer Architecture
Presentation transcript:

Embedded Systems in Silicon TD5102 MIPS Instruction Set Architecture Henk Corporaal Technical University Eindhoven DTI / NUS Singapore 2005/2006

H.C. TD51022 Topics n Instructions & MIPS instruction set n Where are the operands ? n Machine language n Assembler n Translating C statements into Assembler n More complex stuff, like: u while statement u switch statement u procedure / function (leaf and nested) u stack u linking object files

H.C. TD51023 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

H.C. TD51024 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)

H.C. TD51025 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?

H.C. TD51026 Registers vs. Memory n Arithmetic instruction 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

H.C. TD51027 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

H.C. TD51028 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 successive addresses are one byte apart bits of data

H.C. TD51029 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

H.C. TD Memory layout: Alignment Words are aligned n What are the least 2 significant bits of a word address? this word is aligned; the others are not! address

H.C. TD 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!

H.C. TD 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

H.C. TD 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

H.C. TD 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

H.C. TD 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 Machine Language

H.C. TD Stored Program Concept memory OS Program 1 Program 2 CPU code global data stack heap unused

H.C. TD 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

H.C. TD 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

H.C. TD 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

H.C. TD 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

H.C. TD Used MIPS Conventions

H.C. TD 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 u or ……. MIPS Instructions: addi $29, $29, 4 slti $8, $18, 10 andi $29, $29, 6 ori $29, $29, 4 3 Constants

H.C. TD 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,

H.C. TD 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

H.C. TD 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

H.C. TD 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

H.C. TD 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

H.C. TD To summarize:

H.C. TD To summarize:

H.C. TD MIPS addressing modes summary

H.C. TD n Things 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 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

H.C. TD Intermezzo: another approach 80x86 see intel museum: 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 “This history illustrates the impact of the “golden handcuffs” of compatibility “an architecture that is difficult to explain and impossible to love”

H.C. TD 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

H.C. TD 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

H.C. TD 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]

H.C. TD 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:

H.C. TD 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

H.C. TD 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

H.C. TD Compiling a non-leaf procedure C code of ‘recursive’ factorial: int fact (int n) { if (n<1) return (1) else return (n*fact(n-1)); } Factorial: n! = n* (n-1)! 0! = 1

H.C. TD 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)

H.C. TD 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

H.C. TD 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:

H.C. TD 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

H.C. TD 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; }

H.C. TD 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

H.C. TD 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

H.C. TD Arrays versus pointers n Compare the assembly result in the book 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

H.C. TD Starting a program n Compile and Assemble C program 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

H.C. TD Starting a program C program compiler Assembly program assembler Object program (user module)Object programs (library) linker Executable loader Memory