The University of Adelaide, School of Computer Science

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.
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 2 Instructions: Language of the Computer.
The University of Adelaide, School of Computer Science
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
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.
Chapter 2 Instructions: Language of the Computer CprE 381 Computer Organization and Assembly Level Programming, Fall 2013 Zhao Zhang Iowa State University.
Chapter 2 Instructions: Language of the Computer
Computer Architecture CSCE 350
MIPS Function Continued
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.
Ch. 8 Functions.
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
CS3350B Computer Architecture Winter 2015 Lecture 4
Lecture 8: MIPS Instruction Set
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
CS 536 Spring Code generation I Lecture 20.
Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Lecture 6 Sept 16 Chapter 2 continue MIPS translating c into MIPS – examples MIPS simulator (MARS and SPIM)
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 2 Instructions: Language of the Computer.
CS224 Fall 2011 Chapter 2b Computer Organization CS224 Fall 2011 Chapter 2 b With thanks to M.J. Irwin, D. Patterson, and J. Hennessy for some lecture.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Lecture 4: MIPS Instruction Set
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
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 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 12 Procedure Calling.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
Chapter 2 Instructions: Language of the Computer.
Computer Architecture & Operations I
Lecture 5: Procedure Calls
Computer Architecture & Operations I
Lecture 6: Assembly Programs
The University of Adelaide, School of Computer Science
Instruction Sets Chapter 2
The University of Adelaide, School of Computer Science
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Lecture 5: Procedure Calls
Chapter 2 Instructions: Language of the Computer part 2
MIPS function continued
Computer Instructions
Lecture 6: Assembly Programs
Computer Architecture
Presentation transcript:

The University of Adelaide, School of Computer Science 11 April 2017 Lecture 6 Sept 16 Chapter 2 continue MIPS translating c into MIPS – examples MIPS simulator (MARS and SPIM) Chapter 2 — Instructions: Language of the Computer

Finding the Maximum in an array The University of Adelaide, School of Computer Science 11 April 2017 Finding the Maximum in an array Array A is stored in memory beginning at the address given in $s1. Array length is given in $s2. Find the largest integer in the list and copy it into $t0. Solution Scan the list, holding the largest element identified thus far in $t0.   Chapter 2 — Instructions: Language of the Computer 2

Finding the Maximum in an array The University of Adelaide, School of Computer Science 11 April 2017 Finding the Maximum in an array lw $t0,0($s1) # initialize maximum to A[0] addi $t1,$zero,0 # initialize index i to 0 loop: add $t1,$t1,1 # increment index i by 1 beq $t1,$s2,done # if all elements examined, quit sll $t2,$t2,2 # compute 4i in $t2 add $t2,$t2,$s1 # form address of A[i] in $t2 lw $t3,0($t2) # load value of A[i] into $t3 slt $t4,$t0,$t3 # maximum < A[i]? beq $t4,$zero,loop # if not, repeat with no change addi $t0,$t3,0 # if so, A[i] is the new maximum j loop # change completed; now repeat done: # continuation of the program   Chapter 2 — Instructions: Language of the Computer 3

The University of Adelaide, School of Computer Science 11 April 2017 SPIM – A simulator for MIPS Assembly language Home page of SPIM: http://www.cs.wisc.edu/~larus/spim.html A useful document: http://www.cs.wisc.edu/HP_AppA.pdf Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 SPIM simulator windows Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Tutorials on SPIM There are many good tutorials on SPIM. Here are two introductory ones: http://www.cs.umd.edu/class/fall2001/cmsc411/projects/spim/ http://users.ece.gatech.edu/~sudha/2030/temp/spim/spim-tutorial.html Please read (watch) them once and refer to them when you need help. Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 SPIM simulator – how to run? We will implement the code written earlier (finding the max element in an array) using the SPIM simulator. Code is shown below: Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Creating the code and the text segments in SPIM .text .globl __start __start: la $s1, array # initialize array lw $t0, ($s1) la $t6, count lw $s2, ($t6) la is a pseudo-instruction (load address). We use this to input the starting address and the size of the array into registers $s1 and $t6. Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Include the code to be executed addi $t1,$zero, 0 # initialize index i to 0 loop: add $t1,$t1,1 # increment index i by 1 beq $t1,$s2,done # if all elements examined, quit add $t2,$t1,$t1 # compute 2i in $t2 add $t2,$t2,$t2 # compute 4i in $t2 add $t2,$t2,$s1 # form address of A[i] in $t2 lw $t3,0($t2) # load value of A[i] into $t3 slt $t4,$t0,$t3 # maximum < A[i]? beq $t4,$zero,loop # if not, repeat with no change addi $t0,$t3,0 # if so, A[i] is the new maximum j loop # change completed; now repeat done: Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Input to the program – Data Segment .data array: .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13 count: .word 15 endl: .asciiz "\n" ans2: .asciiz "\nmax = " Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Output the result Input array: 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 System calls for output la $a0,ans2 li $v0,4 syscall # print "\nmax = " move $a0,$t0 li $v0,1 syscall # print max la $a0,endl # system call to print li $v0,4 # out a newline syscall li $v0,10 syscall # end Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Output read from simulators SPIM Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Output read from simulators MARS Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Procedure Calling Steps required Place parameters (arguments) 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 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Register Usage $a0 – $a3: arguments (reg’s 4 – 7) $v0, $v1: result values (reg’s 2 and 3) $t0 – $t9: temporaries Can be overwritten by callee $s0 – $s7: saved 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

Procedure Call Instructions The University of Adelaide, School of Computer Science 11 April 2017 Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel Address of following instruction put in $ra Jumps to target address Procedure return: jump register jr $ra Copies $ra to program counter Can also be used for computed jumps e.g., for case/switch statements Chapter 2 — Instructions: Language of the Computer

Leaf Procedure Example The University of Adelaide, School of Computer Science 11 April 2017 Leaf Procedure Example c code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } Arguments g, …, j in $a0, …, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 Chapter 2 — Instructions: Language of the Computer

Leaf Procedure Example The University of Adelaide, School of Computer Science 11 April 2017 Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Non-Leaf Procedures Procedures that call other procedures are known as non-leaf procedures. For nested call, caller needs to save on the stack: 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

Non-Leaf Procedure Example The University of Adelaide, School of Computer Science 11 April 2017 Non-Leaf Procedure Example C code: int fact (int n) { if (n < 1) return f; else return n * fact(n-1); } Argument n in $a0 Result in $v0 Chapter 2 — Instructions: Language of the Computer

Non-Leaf Procedure Example The University of Adelaide, School of Computer Science 11 April 2017 Non-Leaf Procedure Example MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 As we saw in the previous slide, the program correctly computes 12! As 12! = 479001600. But when tried for n = 13, the output is: 1932053504 This is clearly wrong! What happened? How to fix the program? Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Local Data on the Stack Local data allocated by callee e.g., C automatic variables Procedure frame (activation record) Used by some compilers to manage stack storage Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Memory Layout Text: program code Static data: global variables e.g., static variables in C, constant arrays and strings $gp initialized to address allowing ±offsets into this segment Dynamic data: heap E.g., malloc in C, new in Java Stack: automatic storage Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Character Data Byte-encoded character sets ASCII: 128 characters 95 graphic, 33 control Latin-1: 256 characters ASCII, +96 more graphic characters Unicode: 32-bit character set Used in Java, C++ wide characters, … Most of the world’s alphabets, plus symbols UTF-8, UTF-16: variable-length encodings §2.9 Communicating with People Chapter 2 — Instructions: Language of the Computer

Byte/Halfword Operations The University of Adelaide, School of Computer Science 11 April 2017 Byte/Halfword Operations Could use bitwise operations MIPS byte/halfword load/store String processing is a common case lb rt, offset(rs) lh rt, offset(rs) Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) Zero extend to 32 bits in rt sb rt, offset(rs) sh rt, offset(rs) Store just rightmost byte/halfword Chapter 2 — Instructions: Language of the Computer

Target Addressing Example The University of Adelaide, School of Computer Science 11 April 2017 Target Addressing Example Loop code from earlier example Assume Loop at location 80000 Loop: sll $t1, $s3, 2 80000 19 9 4 add $t1, $t1, $s6 80004 22 32 lw $t0, 0($t1) 80008 35 8 bne $t0, $s5, Exit 80012 5 21 2 addi $s3, $s3, 1 80016 1 j Loop 80020 20000 Exit: … 80024 Chapter 2 — Instructions: Language of the Computer

Addressing Mode Summary The University of Adelaide, School of Computer Science 11 April 2017 Addressing Mode Summary Chapter 2 — Instructions: Language of the Computer

Translation and Startup The University of Adelaide, School of Computer Science 11 April 2017 Translation and Startup Many compilers produce object modules directly §2.12 Translating and Starting a Program Static linking Chapter 2 — Instructions: Language of the Computer

Assembler Pseudoinstructions The University of Adelaide, School of Computer Science 11 April 2017 Assembler Pseudoinstructions Most assembler instructions represent machine instructions one-to-one Pseudoinstructions: not supported in instruction set, but assembler translates into equivalent move $t0, $t1 → add $t0, $zero, $t1 blt $t0, $t1, L → slt $at, $t0, $t1 bne $at, $zero, L $at (register 1): assembler temporary Chapter 2 — Instructions: Language of the Computer

Producing an Object Module The University of Adelaide, School of Computer Science 11 April 2017 Producing an Object Module Assembler (or compiler) translates program into machine instructions Provides information for building a complete program from the pieces Header: described contents of object module Text segment: translated instructions Static data segment: data allocated for the life of the program Relocation info: for contents that depend on absolute location of loaded program Symbol table: global definitions and external refs Debug info: for associating with source code Chapter 2 — Instructions: Language of the Computer

Linking Object Modules The University of Adelaide, School of Computer Science 11 April 2017 Linking Object Modules Produces an executable image 1. Merges segments 2. Resolve labels (determine their addresses) 3. Patch location-dependent and external refs Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Loading a Program Load from image file on disk into memory 1. Read header to determine segment sizes 2. Create virtual address space 3. Copy text and initialized data into memory Or set page table entries so they can be faulted in 4. Set up arguments on stack 5. Initialize registers (including $sp, $fp, $gp) 6. Jump to startup routine Copies arguments to $a0, … and calls main When main returns, do exit syscall Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Dynamic Linking Only link/load library procedure when it is called Requires procedure code to be relocatable Avoids image bloat caused by static linking of all (transitively) referenced libraries Automatically picks up new library versions Chapter 2 — Instructions: Language of the Computer

ARM & MIPS Similarities The University of Adelaide, School of Computer Science 11 April 2017 ARM & MIPS Similarities ARM: the most popular embedded core Similar basic set of instructions to MIPS §2.16 Real Stuff: ARM Instructions ARM MIPS Date announced 1985 Instruction size 32 bits Address space 32-bit flat Data alignment Aligned Data addressing modes 9 3 Registers 15 × 32-bit 31 × 32-bit Input/output Memory mapped Chapter 2 — Instructions: Language of the Computer

Compare and Branch in ARM The University of Adelaide, School of Computer Science 11 April 2017 Compare and Branch in ARM Uses condition codes for result of an arithmetic/logical instruction Negative, zero, carry, overflow Compare instructions to set condition codes without keeping the result Each instruction can be conditional Top 4 bits of instruction word: condition value Can avoid branches over single instructions Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Instruction Encoding Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Fallacies Powerful instruction  higher performance Fewer instructions required But complex instructions are hard to implement May slow down all instructions, including simple ones Compilers are good at making fast code from simple instructions Use assembly code for high performance But modern compilers are better at dealing with modern processors More lines of code  more errors and less productivity §2.18 Fallacies and Pitfalls Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Concluding Remarks Design principles 1. Simplicity favors regularity 2. Smaller is faster 3. Make the common case fast 4. Good design demands good compromises Layers of software/hardware Compiler, assembler, hardware MIPS: typical of RISC ISAs §2.19 Concluding Remarks Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 11 April 2017 Concluding Remarks Measure MIPS instruction executions in benchmark programs Consider making the common case fast Consider compromises Instruction class MIPS examples SPEC2006 Int SPEC2006 FP Arithmetic add, sub, addi 16% 48% Data transfer lw, sw, lb, lbu, lh, lhu, sb, lui 35% 36% Logical and, or, nor, andi, ori, sll, srl 12% 4% Cond. Branch beq, bne, slt, slti, sltiu 34% 8% Jump j, jr, jal 2% 0% Chapter 2 — Instructions: Language of the Computer