Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "24/06/2015CSE1303 Part B lecture notes 1 Interpreters, compilers and assembly language Lecture B07 Lecture notes section B07."— Presentation transcript:

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

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

3 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

4 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

5 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

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

7 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

8 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  00110100000000100000000000000001 00101000100000010000000000000010 00010100001000000000000000000100 01110000010001000001000000000010 00100000100001001111111111111111 00001000000100000000000000001010 00000011111000000000000000001000

9 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

10 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.

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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?

19 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

20 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

21 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

22 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

23 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 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..01.. foo..11.. func..11.. printf..01.. library..00.. printf..11.. strlen..11.. lots... abc 011010100100100100110 110010011000011010010 one.o..01.. foo..11.. main..00.. func..10..

25 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

26 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

27 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

28 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

29 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

30 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

31 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)

32 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


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

Similar presentations


Ads by Google