MIPS ISA-II: Procedure Calls & Program Assembly

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.
The University of Adelaide, School of Computer Science
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.
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.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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
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.
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
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.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
©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/
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.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
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.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
 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.
Chapter 2 — Instructions: Language of the Computer — 1 Branch Addressing Branch instructions specify – Opcode, two registers, target address Most branch.
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.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Instruction Set Architecture Chapter 3 – P & H. Introduction Instruction set architecture interface between programmer and CPU Good ISA makes program.
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.
C OMPUTER O RGANIZATION AND D ESIGN The Hardware/Software Interface 5 th Edition Chapter 2 Instructions: Language of the Computer.
Computer Architecture & Operations I
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
Computer Architecture Instruction Set Architecture
The University of Adelaide, School of Computer Science
Computer Architecture Instruction Set Architecture
The University of Adelaide, School of Computer Science
Morgan Kaufmann Publishers
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CS170 Computer Organization and Architecture I
Henk Corporaal TUEindhoven 2010
MIPS Instructions.
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Chapter 2 Instructions: Language of the Computer part 2
Computer Organization and Design Assembly & Compilation
Computer Architecture
Lecture 6: Assembly Programs
Computer Architecture
10/6: Lecture Topics C Brainteaser More on Procedure Call
Where is all the knowledge we lost with information? T. S. Eliot
Program Assembly.
Presentation transcript:

MIPS ISA-II: Procedure Calls & Program Assembly

Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding

Reading Reading 2.8, 2.12 Appendix A: A1 - A.6 (7.1-7.6 in online text) Practice Problems: 10, 14,23 Goals Understand the binary encoding of complete program executables How can procedures be independently compiled and linked (e.g., libraries)? What makes up an executable? How do libraries become part of the executable? What is the role of the ISA in encoding programs? What constitutes the hardware/software interface

Procedure Calls Basic functionality What is so hard about this? Transfer of parameters & control to procedure Transfer of results & control back to the calling program Support for nested procedures What is so hard about this? Consider independently compiled code modules Where are the inputs? Where should I place the outputs? Recall: What do you need to know when you write procedures in C?

Specifics Where do we pass data Nested procedures Preferably registers  make the common case fast Memory as an overflow area Nested procedures The stack, $fp, $sp and $ra Saving and restoring machine state Set of rules that developers/compilers abide by Which registers can am I permitted to use with no consequence? Caller and callee save conventions for MIPS

Basic Parameter Passing .data arg1: .word 22, 20, 16, 4 arg2: .word 33,34,45,8 .text addi $t0, $0, 4 move $t3, $0 move $t1, $0 move $t2, $0 loop: beq $t0, $0, exit addi $t0, $t0, -1 lw $a0, arg1($t1) lw $a1, arg2($t2) jal func add $t3, $t3, $v0 addi $t1, $t1, 4 addi $t2, $t2, 4 j loop func: sub $v0, $a0, $a1 jr $ra exit: --- Register usage What about nested calls? What about excess arguments? PC + 4 $31 PC $31

Leaf Procedure Example The University of Adelaide, School of Computer Science 22 June 2018 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 are passed in $a0, …, $a3 f in $s0 (we need to save $s0 on stack – we will see why later) Results are returned in $v0, $v1 $a0 $a1 $a2 $a3 result registers $v0 $v1 argument registers procedure Chapter 2 — Instructions: Language of the Computer

Procedure Call Instructions The University of Adelaide, School of Computer Science 22 June 2018 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 Example: Chapter 2 — Instructions: Language of the Computer

Leaf Procedure Example The University of Adelaide, School of Computer Science 22 June 2018 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

Procedure Call Mechanics High Address System Wide Memory Map $fp $sp Old Stack Frame stack $sp $fp arg registers dynamic data return address $gp static data New Stack Frame Saved registers text PC local variables reserved $sp compiler compiler ISA addressing HW Low Address

Example of the Stack Frame Call Sequence 1. place excess arguments arg 1 2. save caller save registers arg 2 $fp ($a0-$a3, $t0-$t9) .. 3. jal callee saved registers $s0-$s9 4. allocate stack frame 5. save callee save registers ($s0-$s9, $fp, $ra) caller saved registers $a0-$a3 $t0-$t9 6 set frame pointer Return local variables 1. place function argument in $v0 2. restore callee save registers .. 3. restore $fp $fp 4. pop frame $sp $ra 5. jr $31

Policy of Use Conventions

Summary: Register Usage The University of Adelaide, School of Computer Science 22 June 2018 Summary: 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

The University of Adelaide, School of Computer Science 22 June 2018 Non-Leaf Procedures Procedures that call other 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 22 June 2018 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

Template for a Procedure Allocate stack frame (decrement stack pointer) Save any registers (callee save registers) Procedure body (remember some arguments may be on the stack!) Restore registers (callee save registers) Pop stack frame (increment stack pointer) Return (jr $ra)

Non-Leaf Procedure Example The University of Adelaide, School of Computer Science 22 June 2018 Non-Leaf Procedure Example int fact (int n) { callee save if (n < 1) return f; else return n * fact(n - 1); restore } Chapter 2 — Instructions: Language of the Computer

Non-Leaf Procedure Example The University of Adelaide, School of Computer Science 22 June 2018 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 Callee save Termination Check Leaf Node Recursive call Intermediate Node Chapter 2 — Instructions: Language of the Computer

Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions Review memory organization Memory (data movement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding

The Complete Picture Reading: 2.12, A2, A3, A4, A5 C program compiler Assembly assembler Object module Object library linker executable loader memory

The Assembler Create a binary encoding of all native instructions Translation of all pseudo-instructions Computation of all branch offsets and jump addresses Symbol table for unresolved (library) references Create an object file with all pertinent information Header (information) Text segment Data segment Relocation information Symbol table Example:

Assembly Process One pass vs. two pass assembly Effect of fixed vs. variable length instructions Time, space and one pass assembly Local labels, global labels, external labels and the symbol table What does mean when a symbol is unresolved? Absolute addresses and re-location

Example .data What changes when you relocate code? L1: .word 0x44,22,33,55 # array .text .globl main main: la $t0, L1 li $t1, 4 add $t2, $t2, $zero loop: lw $t3, 0($t0) add $t2, $t2, $t3 addi $t0, $t0, 4 addi $t1, $t1, -1 bne $t1, $zero, loop bgt $t2, $0, then move $s0, $t2 j exit then: move $s1, $t2 exit: li $v0, 10 syscall What changes when you relocate code? 00400000] 3c081001 lui $8, 4097 [L1]         [00400004] 34090004 ori $9, $0, 4             [00400008] 01405020 add $10, $10, $0          [0040000c] 8d0b0000 lw $11, 0($8)             [00400010] 014b5020 add $10, $10, $11         [00400014] 21080004 addi $8, $8, 4            [00400018] 2129ffff   addi $9, $9, -1           [0040001c] 1520fffc   bne $9, $0, -16 [loop-0x0040001c] [00400020] 000a082a slt $1, $0, $10           [00400024] 14200003 bne $1, $0, 12 [then-0x00400024] [00400028] 000a8021 addu $16, $0, $10         [0040002c] 0810000d j 0x00400034 [exit]       [00400030] 000a8821 addu $17, $0, $10        [00400034] 3402000a ori $2, $0, 10           [00400038] 0000000c syscall Assembly Program Native Instructions Assembled Binary

Linker & Loader Linker Loader “Links” independently compiled modules Determines “real” addresses Updates the executables with real addresses Loader As the name implies Specifics are operating system dependent

Linking Program A Program B Assembly A Assembly B cross reference header text static data Assembly A Assembly B reloc cross reference symbol table labels debug Why do we need independent compilation? What are the issues with respect to independent compilation? references across files (can be to data or code!) absolute addresses and relocation Study: Example on pg. 127

Example: # separate file .text 0x20040004 addi $4, $0, 4 0x20050005 jal func_add done 0x0340200a 0x0000000c .text .globl func_add func_add: add $2, $4, $5 0x00851020 jr $31 0x03e00008 0x00400000 0x20040004 0x00400004 0x20050005 0x00400008 ? 0x0040000c 0x3402000a 0x00400010 0x0000000c 0x00400014 0x008551020 0x00400018 0x03e00008 Ans: 0x0c100005

The University of Adelaide, School of Computer Science 22 June 2018 Loading a Program Load from image file on disk into memory 1. Read header to determine segment sizes 2. Create virtual address space (later) 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 22 June 2018 Dynamic Linking Static Linking All labels are resolved at link time Link all procedures that may be called by the program Size of executables? 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

The University of Adelaide, School of Computer Science 22 June 2018 Lazy Linkage Indirection table Stub: Loads routine ID, Jump to linker/loader Linker/loader code Dynamically mapped code Chapter 2 — Instructions: Language of the Computer

Standard Formats: ELF Executable and Linking Format (ELF) ELF header Program Header Table (optional) Section 1 Section 2 Section Header Table (required) ELF header Program Header Table (required) Segment 1 Segment 2 Section Header Table (optional) Linking View Execution View See wikipedia.org

Standard Object File Formats There are other formats associated with different operating systems ISA neutral format for building executables Tools Source  assembly  object  executable  process image

The Computing Model Revisited Register File (Programmer Visible State) Memory Interface stack 0x00 0x01 0x02 0x03 Processor Internal Buses 0x1F Dynamic Data Data segment (static) Text Segment Programmer Invisible State Reserved 0xFFFFFFFF Program Counter Instruction register Arithmetic Logic Unit (ALU) Memory Map Kernel registers Program Execution and the von Neumann model

Instruction Set Architectures (ISA) Instruction set architectures are characterized by several features Operations Types, precision, size Organization of internal storage Stack machine Accumulator General Purpose Registers (GPR) Memory addressing Operand location and addressing

Instruction Set Architectures Memory abstractions Segments, virtual address spaces (more later) Memory mapped I/O (later) Control flow Condition codes Types of control transfers – conditional vs. unconditiional ISA design is the result of many tradeoffs Decisions determine hardware implementation Impact on time, space, and energy Check out ISAs for PowerPC, ARM, x86, SPARC, etc.

ARM & MIPS Similarities The University of Adelaide, School of Computer Science 22 June 2018 ARM & MIPS Similarities ARM: the most popular embedded core Similar basic set of instructions to MIPS 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 22 June 2018 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 ALU $0 $1 $31 CPU/Core Z V C N Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 Instruction Encoding Differences? Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 The Intel x86 ISA Evolution with backward compatibility 8080 (1974): 8-bit microprocessor Accumulator, plus 3 index-register pairs 8086 (1978): 16-bit extension to 8080 Complex instruction set (CISC) 8087 (1980): floating-point coprocessor Adds FP instructions and register stack 80286 (1982): 24-bit addresses, MMU Segmented memory mapping and protection 80386 (1985): 32-bit extension (now IA-32) Additional addressing modes and operations Paged memory mapping as well as segments Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 The Intel x86 ISA Further evolution… i486 (1989): pipelined, on-chip caches and FPU Pentium (1993): superscalar, 64-bit datapath Later versions added MMX (Multi-Media eXtension) instructions The infamous FDIV bug Pentium Pro (1995), Pentium II (1997) New microarchitecture (see Colwell, The Pentium Chronicles) Pentium III (1999) Added SSE (Streaming SIMD Extensions) and associated registers Pentium 4 (2001) New microarchitecture Added SSE2 instructions Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 The Intel x86 ISA And further… AMD64 (2003): extended architecture to 64 bits EM64T – Extended Memory 64 Technology (2004) AMD64 adopted by Intel (with refinements) Added SSE3 instructions Intel Core (2006) Added SSE4 instructions, virtual machine support AMD64 (announced 2007): SSE5 instructions Intel Advanced Vector Extension (AVX announced 2008) If Intel didn’t extend with compatibility, its competitors would! Technical elegance ≠ market success Commonly thought of as a Complex Instruction Set Architecture (CISC) Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 Basic x86 Registers Chapter 2 — Instructions: Language of the Computer

Basic x86 Addressing Modes The University of Adelaide, School of Computer Science 22 June 2018 Basic x86 Addressing Modes Two operands per instruction Source/dest operand Second source operand Register Immediate Memory Memory addressing modes Address in register Address = Rbase + displacement Address = Rbase + 2scale × Rindex (scale = 0, 1, 2, or 3) Address = Rbase + 2scale × Rindex + displacement Chapter 2 — Instructions: Language of the Computer

x86 Instruction Encoding The University of Adelaide, School of Computer Science 22 June 2018 x86 Instruction Encoding Variable length encoding Postfix bytes specify addressing mode Prefix bytes modify operation Operand length, repetition, locking, … Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 Implementing IA-32 Complex instruction set makes implementation difficult Hardware translates instructions to simpler microoperations Simple instructions: 1–1 Complex instructions: 1–many Microengine similar to RISC Market share makes this economically viable Comparable performance to RISC Compilers avoid complex instructions Better code density Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 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 Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 22 June 2018 Fallacies Backward compatibility  instruction set does not change But they do accrete more instructions x86 instruction set Chapter 2 — Instructions: Language of the Computer

Summary Instruction complexity is only one variable Design Principles: 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 indeed!

Study Guide Compute number of bytes to encode a SPIM program What does it mean for a code segment to be relocatable? Identify addresses that need to be modified when a program is relocated. Given the new start address modify the necessary addresses Given the assembly of an independently compiled procedure, ensure that it follows the MIPS calling conventions, modifying it if necessary

Study Guide (cont.) Given a SPIM program with nested procedures, ensure that you know what registers are stored in the stack as a consequence of a call Encode/disassemble jal and jr instructions Computation of jal encodings for independently compiled modules How can I make procedure calls faster? Hint: What about a call is it that takes time? How are independently compiled modules linked into a single executable? (assuming one calls a procedure located in another)

Glossary Argument registers Caller save registers Callee save registers Disassembly Frame pointer Independent compilation Labels: local, global, external Linker/loader Linking: static vs. dynamic vs. lazy Native instructions Nested procedures Object file One/two pass assembly Procedure invocation Pseudo instructions Relocatable code Stack frame Stack pointer Symbol table Unresolved symbol