24/06/2015CSE1303 Part B lecture notes 1 Interpreters, compilers and assembly language Lecture B07 Lecture notes section B07.

Slides:



Advertisements
Similar presentations
Instruction Set Design
Advertisements

Programming Languages and Paradigms The C Programming Language.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
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.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
Lecture 8: MIPS Instruction Set
The Assembly Language Level
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++
Systems Software.
Programming Types of Testing.
Lab6 – Debug Assembly Language Lab
1 Lecture-2 CSIT-120 Spring 2001 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
16/13/2015 3:30 AM6/13/2015 3:30 AM6/13/2015 3:30 AMIntroduction to Software Development What is a computer? A computer system contains: Central Processing.
Software Language Levels Machine Language (Binary) Assembly Language –Assembler converts Assembly into machine High Level Languages (C, Perl, Shell)
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
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.
1 Lecture-2 CS-120 Fall 2000 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Guide To UNIX Using Linux Third Edition
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Computer Science 210 Computer Organization The Instruction Execution Cycle.
Chapter 2 Software Tools and Assembly Language Syntax.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
“C” Programming Language What is language ? Language is medium of communication. If two persons want to communicate with each other, they have to use.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Enabling the ARM Learning in INDIA ARM DEVELOPMENT TOOL SETUP.
MIPS coding. SPIM Some links can be found such as:
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
What have mr aldred’s dirty clothes got to do with the cpu
ITEC 352 Lecture 12 ISA(3). Review Buses Memory ALU Registers Process of compiling.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Blackfin Array Handling Part 1 Making an array of Zeros void MakeZeroASM(int foo[ ], int N);
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Representation of Data - Instructions Start of the lesson: Open this PowerPoint from the A451 page – Representation of Data/ Instructions How confident.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
1 The user’s view  A user is a person employing the computer to do useful work  Examples of useful work include spreadsheets word processing developing.
Lecture 3 Translation.
Component 1.6.
Assembler, Compiler, MIPS simulator
Machine dependent Assembler Features
Component 1.6.
Lecture 6: Assembly Programs
Operating System Interface between a user and the computer hardware
A bit of C programming Lecture 3 Uli Raich.
ACOE301: Computer Architecture II Labs
Chapter 3 Machine Language and Assembly Language.
Chapter 3 Machine Language and Assembly Language.
Choice of Programming Language
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
CSCE Fall 2013 Prof. Jennifer L. Welch.
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
CSCE 121: Simple Computer Model Spring 2015
Computer Organization and Design Assembly & Compilation
CSCE Fall 2012 Prof. Jennifer L. Welch.
Lecture 6: Assembly Programs
Lecture 4: Instruction Set Design/Pipelining
Program Assembly.
Presentation transcript:

24/06/2015CSE1303 Part B lecture notes 1 Interpreters, compilers and assembly language Lecture B07 Lecture notes section B07

24/06/2015CSE1303 Part B lecture notes 2 Last time  Pointers  Aggregation of data  arrays  structs  Characters  Strings

24/06/2015CSE1303 Part B lecture notes 3 In this lecture  Interpreters  Machine language  Assembly language  structure  Compilers  what a compiler does  Stages of code generation  compiling, assembling, linking  compiling multi-file programs

24/06/2015CSE1303 Part B lecture notes 4 Interpreters  Interpreter is a program which can understand and run one at a time source code statements written in a language  Shell (/bin/sh in Unix) interprets and runs command line  loop:  read command line from user (“fetch”)  examine command line (“decode”)  perform appropriate commands (“execute”)  repeat loop

24/06/2015CSE1303 Part B lecture notes 5 Interpreters  Advantages of interpreters  easy to write  flexible easy to add extra functionalityeasy to add extra functionality  easy to debug interpreted program breakpoints and single-steppingbreakpoints and single-stepping  Problems with interpreters  slow every line must be interpreted every time it is runevery line must be interpreted every time it is run fetch-decode-execute must be performed in softwarefetch-decode-execute must be performed in software  inefficient must allocate memory for both the interpreter code and the program that it is to runmust allocate memory for both the interpreter code and the program that it is to run

24/06/2015CSE1303 Part B lecture notes 6 Interpreters  What language is the interpreter written in?

24/06/2015CSE1303 Part B lecture notes 7 Machine language  Ultimately, programs are run by the computer’s Central Processing Unit or (CPU).  programs are translated to a language composed of small, easily performed instructions the CPU is able to execute  this language is called machine language  each different CPU type usually requires a different machine language  machine language programs are stored in the memory unit as patterns of bits

24/06/2015CSE1303 Part B lecture notes 8 Machine Language  Machine language bit patterns to compute the factorial of a number  This example for a “MIPS” R2000 CPU 

24/06/2015CSE1303 Part B lecture notes 9 Assembly language  Problems with machine language  very difficult to write or even read  Need a compromise  a language that humans can cope with that: supports comments, variable names, line labels, etcsupports comments, variable names, line labels, etc has human-readable versions of machine instructionshas human-readable versions of machine instructions  but is easily converted to machine language usually a 1-to-1 relationship between each language instruction and its equivalent machine instructionusually a 1-to-1 relationship between each language instruction and its equivalent machine instruction ideally, this conversion done by a computer program that won’t make mistakesideally, this conversion done by a computer program that won’t make mistakes  languages of this kind called assembly languages many thousands of these exist but broadly similarmany thousands of these exist but broadly similar

24/06/2015CSE1303 Part B lecture notes 10 MIPS assembly language  Function to calculate a factorial: # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n <= 1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return.

24/06/2015CSE1303 Part B lecture notes 11 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n<=1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return. label: identifies a line of the program… loop : … so that other lines can refer to it. loop

24/06/2015CSE1303 Part B lecture notes 12 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n<=1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 end: jr $ra # Return. j loop instruction: one per line (this one actually decreases a value by adding minus one ) addi $a0, $a0, -1

24/06/2015CSE1303 Part B lecture notes 13 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n<=1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return. operation: human readable version of instruction “opcode” (operation code). Describes the kind of instruction ( here:- “branch if less or equal” ) ble

24/06/2015CSE1303 Part B lecture notes 14 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n<=1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return. register: one of 32 fast storage areas inside CPU. Denoted by ‘$’ then short name (or number) $ra collectively these called operands immediate (literal) value: here equals the number 1 1

24/06/2015CSE1303 Part B lecture notes 15 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res=1 loop: ble $a0, 1, end # end if n<=1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return. assembler directive: starts with a dot ‘.’ char Command for the translator (not the CPU). Here defines start of text (or code) segment..text

24/06/2015CSE1303 Part B lecture notes 16 MIPS assembly language # Computes factorial of number (n) in $a0 # and returns result (“Res”) in $v0.text.text fact: ori $v0, $zero, 1 # Res = 1 loop: ble $a0, 1, end # end if n <= 1 mul $v0, $v0, $a0 # Res=Res*n mul $v0, $v0, $a0 # Res=Res*n addi $a0, $a0, -1 # n=n-1 addi $a0, $a0, -1 # n=n-1 j loop j loop end: jr $ra # Return. # Res = 1 comment: starts at #, extends to end of line # Computes factorial of number (n) in $a0

24/06/2015CSE1303 Part B lecture notes 17 Assembly language  Why learn assembly language?  convenient halfway stop between high- level language (e.g., C) and machine language break down problem of running C programs into two smaller problems: (C  assembly, assembly  machine language)break down problem of running C programs into two smaller problems: (C  assembly, assembly  machine language)  sometimes necessary to use this low- level programming language when timing is critical or when memory size is limited e.g. device drivers or embedded computerse.g. device drivers or embedded computers

24/06/2015CSE1303 Part B lecture notes 18 Assembly language  Programs already exist which already turn C into machine language  compilers  Most people don’t write low-level device drivers  So, why learn assembly language?

24/06/2015CSE1303 Part B lecture notes 19 Assembly language  Why learn assembly language?  to understand how compilers work  to understand how to write effective C (and other languages) efficientefficient correctcorrect portableportable  to become a more valuable programmer

24/06/2015CSE1303 Part B lecture notes 20 Compilers  Compiler is a set of tools (programs) which convert higher-level code (e.g., C) to machine language  translator C to assembly languageC to assembly language  assembler assembly language to object codeassembly language to object code  linker object code to executable programobject code to executable program  Most compilers (e.g., Borland C++, GCC) can perform any or all of the above steps  options can halt processing at any stage

24/06/2015CSE1303 Part B lecture notes 21 Compilers C code (.c) assembly language (.s) object code (.o) executable translation performed by a translator assembling performed by an assembler linking performed by a linker

24/06/2015CSE1303 Part B lecture notes 22 Compilers  Some programs may refer to variables or functions defined elsewhere  e.g., printf or strlen functions  Object code is code compiled as far as it can be without resolving these references  mainly machine language, but with additional data identifying symbols that still need resolving  Linking is combining object modules and resolving these references so that they refer to the correct memory addresses  produces pure machine language executable

24/06/2015CSE1303 Part B lecture notes 23 Compilers  Programs may be constructed from many source files  some programs are millions of lines long, too long for one file  Separate files can be compiled to the object code stage independently of the others  if one file is changed, other files don’t need recompiling  The multiple object files are then linked simultaneously to form executable code  including libraries written by OS vendor

24/06/2015CSE1303 Part B lecture notes 24 Compilers /* Ref. to foo, defined elsewhere. */ extern int foo; /* Prototype for function func. */ void func(void); /* main calls func, defined in two.c. */ int main() { /*... */ foo++; func(); /*... */ /*... */ foo++; func(); /*... */}one.c #include #include /* Definition of foo is here. */ int foo; static int secret; /* private to two.c */ /* Function called from another file. */ void func(void) { printf( /*... */ ); printf( /*... */ );}two.c gcc -c two.c -o two.o gcc -c one.c -o one.o -c option stops at object code stage gcc one.o two.o -o abc two.o foo func printf library printf strlen lots... abc one.o foo main func..10..

24/06/2015CSE1303 Part B lecture notes 25 Compilers  To write multi-file C programs  if file one.c needs to call function in two.c, put function prototype in one.c void func(void);void func(void);  compiler now knows enough about function to compile one.c  linker will locate the function during linking

24/06/2015CSE1303 Part B lecture notes 26 Compilers  To write multi-file C programs  if file one.c needs to refer to global variable in two.c, declare it in one.c with extern modifier extern int foo;extern int foo;  compiler tags variable as being outside file, and does not try to make another variable with the same name in one.c  linker will associate one.c ’s variable with two.c ’s variable during linking

24/06/2015CSE1303 Part B lecture notes 27 Compilers  To write multi-file C programs  if file two.c wants to keep a global variable or function private (inaccessible outside the file), declare it with the static modifier static int secret;static int secret; static void mine(char* x);static void mine(char* x);  compiler will remove all references to the variable or function in object file  other files cannot refer to it, even with extern modifier  files in same program may each have static variable/function of the same name

24/06/2015CSE1303 Part B lecture notes 28 Compilers  To compile multi-file C programs  compile each source file to the object code stage with GCC, use -c optionwith GCC, use -c option gcc -c file1.c -o file1.ogcc -c file1.c -o file1.o creates object code called file1.ocreates object code called file1.o  link all object files together with GCC, no option needed provided files have.o suffixwith GCC, no option needed provided files have.o suffix gcc file1.o file2.o file3.o -o finalgcc file1.o file2.o file3.o -o final creates executable file called finalcreates executable file called final link with libraries too at this stage (here, math lib)link with libraries too at this stage (here, math lib) gcc file1.o file2.o file3.o -lm -o finalgcc file1.o file2.o file3.o -lm -o final

24/06/2015CSE1303 Part B lecture notes 29 Compilers  To compile multi-file C programs  if all files are small, let GCC compile all of them at once to executable file gcc file1.c file2.c file3.c -o finalgcc file1.c file2.c file3.c -o final  not efficient but easier to type

24/06/2015CSE1303 Part B lecture notes 30 Covered in this lecture  Interpreters  Machine language  Assembly language  structure  Compilers  what a compiler does  Stages of code generation  compiling, assembling, linking  compiling multi-file programs

24/06/2015CSE1303 Part B lecture notes 31 Going further  Object file format  getting your hands dirty with object code  make  a tool that makes managing multi-file programs easy  uses file date stamps to determine which files need updating  man make (manual page)

24/06/2015CSE1303 Part B lecture notes 32 Next time  MIPS architecture  registers  memory  Running MIPS programs  fetch-execute cycle  SPIM simulator Reading: lecture notes section B08