Cs 61C L10 Start.1 Patterson Spring 99 ©UCB CS61C Starting a Program Lecture 10 February 19, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html.

Slides:



Advertisements
Similar presentations
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Advertisements

MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Computer Architecture CSCE 350
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
1 Computer Architecture MIPS Simulator and Assembly language.
CS61C L11 Linker © UC Regents 1 CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson
8.
Compiling, Assembling, Loading, Linking (CALL) I (1) Fall 2005 Lecture 09: Program Translation.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Instructor: Justin Hsia 7/08/2013Summer Lecture #81 CS 61C: Great Ideas in Computer Architecture Running a Program.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
CS61C L18 Running a Program I (1) Garcia, Spring 2007 © UCB Gaze-controlled UI!  Researchers at Stanford have designed a system that allows a user to.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) Researchers.
CS61C L18 Running a Program I (1) Garcia, Spring 2007 © UCB Router Security Hole!  Huge security hole has been found – if you have a home router, crackers.
CS 61C L13 CALL (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #13: CALL Andy Carle.
CS61C L18 Running a Program I (1) Garcia, Fall 2006 © UCB Security hole finder  Google’s just-released new /* Code Search */, which allows you to specify.
14:332:331 Computer Architecture and Assembly Language Spring 06 Week 4: Addressing Mode, Assembler, Linker [Adapted from Dave Patterson’s UCB CS152 slides.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L18 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
CS61C L18 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) I (1) Garcia © UCB Lecturer PSOE Dan Garcia
CS 61C L19 Running a Program aka Compiling, Assembling, Loading, Linking (CALL) II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia
MIPS Assembler Programming
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
1 CS 430 Computer Architecture Clap if you like pizza! Pointless Poll.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 19 – Running a Program II (Compiling, Assembling, Linking, Loading) I’m.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 16 – Running a Program (Compiling, Assembling, Linking, Loading) Highlights:
CS61C L13 CALL I (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #13 – Compiling,
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CS61C L18 Running a Program I (1) Garcia, Fall 2006 © UCB Berkeley beat-down  #10 Cal bears ate the OU ducks They’ve scored 35, 42, 42, 49, 41.
Computer Architecture and Design – ECEN 350 Part 4 [Some slides adapted from M. Irwin, D. Paterson and others]
CS61C L13 Compiling, Linking, and Loading (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 – Running.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 18 – Running a Program I (Compiling, Assembling, Linking, Loading) Researchers.
CDA 3101 Fall 2013 Introduction to Computer Organization Pointers & Arrays MIPS Programs 16 September 2013.
Spr 2015, Feb 9... ELEC / Lecture 4 1 ELEC / Computer Architecture and Design Spring 2015 Compiling and Executing Programs.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 18 – Running a Program I (Compiling, Assembling, Linking, Loading)
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
CS 61C L3.2.2 Floating Point 2 (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Floating Point II & Linking Kurt Meinz.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 16 – Running a Program (Compiling, Assembling, Linking, Loading) Research shows laptops.
8.
CENG 311 Starting a Program. Review (1/2) °IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with +/- infinity, Not-a-Number (Nan),
The Assembly Process Computer Organization and Assembly Language: Module 10.
CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading) 1 Instructors: Nick Weaver & Vladimir.
CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading) 1 Instructors: Nicholas Weaver & Vladimir Stojanovic
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 110 Computer Architecture Lecture 7: Running a Program - CALL (Compiling, Assembling, Linking, and Loading) Instructor: Sören Schwertfeger
Lecture 3 Translation.
faculty “re-imagine” ugrad education
Computer Architecture & Operations I
IT 251 Computer Organization and Architecture
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Instructor: Justin Hsia
Topic 2e High-Level languages and Systems Software
There is one handout today at the front and back of the room!
Green Subscription Based PC Announced
ECE232: Hardware Organization and Design
The University of Adelaide, School of Computer Science
Review.
Computer Organization and Design Assembly & Compilation
10/6: Lecture Topics C Brainteaser More on Procedure Call
Program Assembly.
Presentation transcript:

cs 61C L10 Start.1 Patterson Spring 99 ©UCB CS61C Starting a Program Lecture 10 February 19, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html

cs 61C L10 Start.2 Patterson Spring 99 ©UCB Review: New MIPS arithmetic instructions °ExampleMeaningComments °mult $2,$3Hi, Lo = $2 x $364-bit signed product °multu$2,$3Hi, Lo = $2 x $364-bit unsigned product °div $2,$3Lo = $2 ÷ $3,Lo = quotient, Hi = rem °divu $2,$3Lo = $2 ÷ $3,Unsigned quotient, rem. °mfhi $1$1 = HiUsed to get copy of Hi °mflo $1$1 = LoUsed to get copy of Lo °add.s $0,$1,$2 $f0=$f1+$f2Fl. Pt. Add (single) °add.d $0,$2,$4 $f0=$f2+$f4Fl. Pt. Add (double) °sub.s $0,$1,$2 $f0=$f1-$f2Fl. Pt. Subtract (single) °sub.d $0,$2,$4 $f0=$f2-$f4Fl. Pt. Subtract (double) °mul.s $0,$1,$2 $f0=$f1x$f2Fl. Pt. Multiply (single) °mul.d $0,$2,$4 $f0=$f2x$f4Fl. Pt. Multiply (double) °div.s $0,$1,$2 $f0=$f1÷$f2Fl. Pt. Divide (single) °div.d $0,$2,$4 $f0=$f2÷$f4Fl. Pt. Divide (double) °c.X.s $0,$1 flag1= $f0 X $f1Fl. Pt.Compare (single) °c.X.d $0,$2 flag1= $f0 X $f2Fl. Pt.Compare (double) X is eq, lt, le; bc1t, bc1f tests flag

cs 61C L10 Start.3 Patterson Spring 99 ©UCB Review 2/2 °IEEE 754 Floating Point standard: accuracy first class citizen °Multiply product 2n bits; Divide produces both n-bit quotient and n-bit remainder °Computer numbers have limited size  limited precision underflow: too small for Fl. Pt. (bigger negative exponent than can represent) overflow: too big for Fl. Pt. or integer (bigger positive exponent than can represent, or bigger integer than fits in word) Programmers beware!

cs 61C L10 Start.4 Patterson Spring 99 ©UCB Outline °Floating Point Questions and Answers °Compiling, Assembling a Program °Administrivia, “Computers in the News” °Linking, Loading a Program °An Example °Conclusion

cs 61C L10 Start.5 Patterson Spring 99 ©UCB Floating Point Questions: Negative 0? °What is result of this program? main() { float i,j; i = 1.0; j = 0.0; printf("%3.1f\n",i/j); printf("%3.1f\n",-i/j); printf("%3.1f\n",j/(-i/j)); if (j/(-i/j) == 0.0) printf("0.0 == -0.0\n"); else printf("0.0 != -0.0\n"); printf("%3.1f\n",i/j-i/j); printf("%3.1f\n",(i/j-i/j)+i); } °Output: Inf -Inf == -0.0 NaN

cs 61C L10 Start.6 Patterson Spring 99 ©UCB Big Idea: Levels of Abstaction (Unix/DOS) C program: foo.c/foo.txt Assembly program: foo.s/ foo.asm Executable(mach lang pgm): a.out/foo.exe Compiler Assembler Linker Loader Memory Object(mach lang module): foo.o/foo.obj lib.o/lib.obj

cs 61C L10 Start.7 Patterson Spring 99 ©UCB Assembler °Follow Directions °Replace Pseudoinstructions °Find list of labels, addresses °Produce machine language °Creates Object File

cs 61C L10 Start.8 Patterson Spring 99 ©UCB Example Assembly Program: Directives.text.align2.globlmain main: subu $sp,$sp,32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw$t0, 28($sp) ble$t0,100, loop la$a0, str lw$a1, 24($sp) jal printf move $v0, $0 lw$ra, 20($sp) addiu $sp,$sp,32 j$ra.data.align0 str:.asciiz"The sum from is %d\n"

cs 61C L10 Start.9 Patterson Spring 99 ©UCB Assembler Directives (p. A-51 to A-53) °Tell assembler how to translate program but do not produce machine instructions.text (addr) : Subsequent items put in user text segment (starting at addr ).align n : Align the next data on a 2 n byte boundary; align 2  next word boundary.globl sym : declares sym global and can be referenced from other files.data (addr) : Subsequent items put in user data segment (starting at addr ).asciiz str : Store the string str in memory and null-terminate it

cs 61C L10 Start.10 Patterson Spring 99 ©UCB Example Assembly Program: PseudoInstr.text.align2.globlmain main: subu $sp,$sp,32 sw $ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw $t0, 28($sp) ble $t0,100,loop la $a0, str lw $a1, 24($sp) jal printf move $v0, $0 lw $ra, 20($sp) addiu $sp,$sp, 32 j $ra.data.align0 str:.asciiz"The sum from is %d\n"

cs 61C L10 Start.11 Patterson Spring 99 ©UCB Pseudoinstruction replacements °Asm. treats common variations of machine language instructions as if real instructions subu $sp,$sp,32addiu $sp,$sp,-32 sd $a0, 32($sp) sw $a0, 32($sp) sw $a1, 36($sp) mul $t7,$t6,$t6mul $t6,$t6 mflo $t7 addu $t0,$t6,1addiu $t0,$t6,1 ble $t0,100,loopslti $at,$t0,101 bne $at,$0,loop la $a0, strlui $at,left(str) ori $a0,$at,right(str) move $v0, $0add $v0,$0,$0

cs 61C L10 Start.12 Patterson Spring 99 ©UCB Example Assembly Program: Labels.text.align2.globlmain main: subu $sp,$sp,32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw$t0, 28($sp) ble$t0,100, loop la$a0, str lw$a1, 24($sp) jal printf move $v0, $0 lw$ra, 20($sp) addiu $sp,$sp,32 j$ra.data.align0 str:.asciiz"The sum from is %d\n"

cs 61C L10 Start.13 Patterson Spring 99 ©UCB Addresses °Main job of Assembler: 1) Find list of labels and their addresses 2) Produce machine language °Puts label and memory address of that instruction in Symbol Table °Can use a name before it is defined; forward reference e.g., str in example Note: Must declare before use in C °Uses Symbol Table in 2nd pass to produce machine code, including addresses

cs 61C L10 Start.14 Patterson Spring 99 ©UCB Administrivia °Readings: (3.9) A.2, A.3, A.4, 3.10, 3.11 °5th homework: Due 2/24 7PM Exercises 4.21, 4.25, 4.28 °3rd Project/5th Lab: MIPS Simulator Due Wed. 3/3 7PM; deadline Thurs 8AM Twice / semester 24-hour extension Everything (but midterm) may be done in pairs now °Solutions : ~cs61c/solutions directory. Access it using cs61c account. Not www! °Midterm conflict time: Mon 3/15 6-9PM

cs 61C L10 Start.15 Patterson Spring 99 ©UCB “Computers in the News” °“Microsoft Denies Rumors That It Is Crafting a Language Like Java”, NY Times, 2/15/99 “Microsoft Corp. is playing down a report in ‘PC Week’ that it has been briefing software developers on a new programming language (code-named ‘Cool’) that would be similar to Sun's Java software.” [Java SW not binaries] “.... Cross platform means that a developer can write one version of a program in Java, which can then run on Windows, Macintosh, Unix and other software operating systems.” “... A compiler is a programming tool that converts high-level languages, like C++ or Java, into the machine code of ones and zeros that computers understand.”

cs 61C L10 Start.16 Patterson Spring 99 ©UCB Big Idea: Levels of Abstaction (Unix/DOS) C program: foo.c/foo.txt Assembly program: foo.s/ foo.asm Executable(mach lang pgm): a.out/foo.exe Compiler Assembler Linker Loader Memory Object(mach lang module): foo.o/foo.obj lib.o/lib.obj

cs 61C L10 Start.17 Patterson Spring 99 ©UCB Object File Format (output of Assembler) °object file header: size and position of the other pieces of the object file °text segment: the machine code °data segment: binary representation of the data in the source file °relocation information: identifies instructions and data words that depend on absolute addresses °symbol table: remaining labels not defined, such as external references °debugging information: help debugger map source file to instructions, data

cs 61C L10 Start.18 Patterson Spring 99 ©UCB Link Editor or Linker °Enable Separate Compilation of files Changes to one file do not require recompilation of whole program -Windows NT source is >30 M lines of code! And Growing! Called a module Link Editor name from editing the “links” in jump and link instructions °Job of Linker: resolve external labels in object code and place modules and data into memory

cs 61C L10 Start.19 Patterson Spring 99 ©UCB Steps of Linker 1) Resolve references across files 2) If some labels unresolved, search for library routine references (e.g., printf in example) If finds reference, it extracts routine’s code and inserts into program text 3) Determine memory address of code modules and adjust absolute addresses (using relocation information) °Produces file in same format, but no relocation information or unresolved references

cs 61C L10 Start.20 Patterson Spring 99 ©UCB C memory allocation seen by the Program Text Text Segment Static data Data Segment Heap data Stack Stack Segment $sp $gp 0x7fff ffff Reserved 0x x x

cs 61C L10 Start.21 Patterson Spring 99 ©UCB Absolute Addresses in MIPS °Which instructions need relocation editing? °J-format: jump, jump and link j/jalxxxxx °Loads and stores to variables in static area, relative to global pointer lw/sw$gp$xaddress °What about conditional branches? beq/bne$rs$rtaddress °PC-relative addressing preserved even if code moves

cs 61C L10 Start.22 Patterson Spring 99 ©UCB Big Idea: Levels of Abstaction (Unix/DOS) C program: foo.c/foo.txt Assembly program: foo.s/ foo.asm Executable(mach lang pgm): a.out/foo.exe Compiler Assembler Linker Loader Memory Object(mach lang module): foo.o/foo.obj

cs 61C L10 Start.23 Patterson Spring 99 ©UCB Loading °If links without error can be run °Before run, executable program stored on disk °UNIX systems, Operating System Kernel loads the program from disks

cs 61C L10 Start.24 Patterson Spring 99 ©UCB Steps 1-5 (out of 6) for loading program °Reads executable file’s header to determine size of text and data segments °Creates new address space for program large enough to hold text and data segments, along with a stack segment °Copies instructions and data from executable file into the new address space °Copies arguments passed to the program onto the stack °Initializes machine registers. Most registers cleared, but stack pointer assigned address of 1st free stack location

cs 61C L10 Start.25 Patterson Spring 99 ©UCB Steps 6 (out of 6) for loading program °Jumps to start-up routine that copies program’s arguments from stack to registers and calls program’s main routine If main routine returns, start-up routine terminates program with the exit system call

cs 61C L10 Start.26 Patterson Spring 99 ©UCB Example: C  Asm  Obj  Exe  Run #include int main (int argc, char *argv[]) { int i; int sum = 0; for (i = 0; i <= 100; i = i + 1) sum = sum + i * i; printf ("The sum from is %d\n", sum); }

cs 61C L10 Start.27 Patterson Spring 99 ©UCB Example: C  Asm  Obj  Exe  Run.text.align2.globlmain main: subu $sp,$sp,32 sw$ra, 20($sp) sd$a0, 32($sp) sw$0, 24($sp) sw$0, 28($sp) loop: lw$t6, 28($sp) mul$t7, $t6,$t6 lw$t8, 24($sp) addu $t9,$t8,$t7 sw$t9, 24($sp) addu $t0, $t6, 1 sw$t0, 28($sp) ble$t0,100, loop la$a0, str lw$a1, 24($sp) jal printf move $v0, $0 lw$ra, 20($sp) addiu $sp,$sp,32 j$ra.data.align0 str:.asciiz"The sum from is %d\n"

cs 61C L10 Start.28 Patterson Spring 99 ©UCB Symbol Table Entries °Label Address °main: °loop: °str: °printf:

cs 61C L10 Start.29 Patterson Spring 99 ©UCB Example: C  Asm  Obj  Exe  Run 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, loop 40 lui$4, l.str 44 addiu $4,$4,r.str 48 lw$5,24($29) 4c jalprintf 50 add$2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Remove pseudoinstructions, assign addresses

cs 61C L10 Start.30 Patterson Spring 99 ©UCB Symbol Table Entries °Symbol Table Label Address main:0x loop:0x str:0x printf: 0x000003b0 °Relocation Information AddressInstr. TypeDependency 0x cjalprintf

cs 61C L10 Start.31 Patterson Spring 99 ©UCB Example: C  Asm  Obj  Exe  Run 00 addiu $29,$29, sw$31,20($29) 08 sw$4, 32($29) 0c sw$5, 36($29) 10 sw $0, 24($29) 14 sw $0, 28($29) 18 lw $14, 28($29) 1c multu $14, $14 20 mflo $15 24 lw $24, 24($29) 28 addu $25,$24,$15 2c sw $25, 24($29) 30 addiu $8,$14, 1 34 sw$8,28($29) 38 slti$1,$8, 101 3c bne$1,$0, lui$4, addiu $4,$4, lw $5,24($29) 4c jal add $2, $0, $0 54 lw $31,20($29) 58 addiu $29,$29,32 5c jr $31 Edit Addresses: start at 0x

cs 61C L10 Start.32 Patterson Spring 99 ©UCB Example: C  Asm  Obj  Exe  Run 0x x x x00400c x x x x00401c x x x x00402c x x x x00403c x x x x00404c x x x x00405c

cs 61C L10 Start.33 Patterson Spring 99 ©UCB “And in Conclusion..” 1/1 °Stored Program concept mean instructions just like data, so can take data from storage, and keep transforming it until load registers and jump to routine to begin execution Compiler  Assembler  Linker (  Loader  °Assembler does 2 passes to resolve addresses, handling internal forward references °Linker enables separate compilation, libraries that need not be compiled, and resolves remaining addresses °Next: Reviewing C, Pointers, Procedures