Date post: | 21-Dec-2015 |
Category: |
Documents |
View: | 221 times |
Download: | 3 times |
04/18/23 CSE1303 Part B lecture notes 1
Interpreters, Interpreters, compilers and compilers and
assembly languageassembly language
Lecture B07Lecture B07
Lecture notes section B07Lecture notes section B07
04/18/23 CSE1303 Part B lecture notes 2
Last timeLast time
PointersPointers Aggregation of dataAggregation of data
arraysarrays structsstructs
CharactersCharacters StringsStrings
04/18/23 CSE1303 Part B lecture notes 3
In this lectureIn this lecture
InterpretersInterpreters Machine languageMachine language Assembly languageAssembly language
structurestructure CompilersCompilers
what a compiler doeswhat a compiler does Stages of code generationStages of code generation
compiling, assembling, linkingcompiling, assembling, linking compiling multi-file programscompiling multi-file programs
04/18/23 CSE1303 Part B lecture notes 4
InterpretersInterpreters
InterpreterInterpreter is a program which can is a program which can understand and understand and run one at a time run one at a time source code statements written in a source code statements written in a languagelanguage
Shell Shell (/bin/sh(/bin/sh in Unix) interprets and in Unix) interprets and runs command lineruns command line loop:loop: read command line from user (“read command line from user (“fetchfetch”)”) examine command line (“examine command line (“decodedecode”)”) perform appropriate commands (“perform appropriate commands (“executeexecute”)”) repeat looprepeat loop
04/18/23 CSE1303 Part B lecture notes 5
InterpretersInterpreters Advantages of interpretersAdvantages of interpreters
easy to writeeasy to write flexibleflexible
• easy to add extra functionalityeasy to add extra functionality easy to debug interpreted programeasy to debug interpreted program
• breakpoints and single-steppingbreakpoints and single-stepping
Problems with interpretersProblems with interpreters slowslow
• 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
inefficientinefficient• must allocate memory for both the interpreter code must allocate memory for both the interpreter code
and the program that it is to runand the program that it is to run
04/18/23 CSE1303 Part B lecture notes 6
InterpretersInterpreters
What language is the interpreter What language is the interpreter written in?written in?
04/18/23 CSE1303 Part B lecture notes 7
Machine languageMachine language
Ultimately, programs are run by the Ultimately, programs are run by the computer’s Central Processing Unit computer’s Central Processing Unit or (CPU). or (CPU). programs are translated to a language programs are translated to a language
composed of small, easily performed composed of small, easily performed instructions the CPU is able to executeinstructions the CPU is able to execute
this language is called this language is called machine machine languagelanguage
each differenteach different CPU type usually CPU type usually requires a different machine language requires a different machine language
machine language programs are stored machine language programs are stored in the memory unit as patterns of bitsin the memory unit as patterns of bits
04/18/23 CSE1303 Part B lecture notes 8
Machine LanguageMachine Language
Machine language bit patterns to Machine language bit patterns to compute the factorial of a numbercompute the factorial of a number
This example for a “MIPS” R2000 CPUThis example for a “MIPS” R2000 CPU 00110100000000100000000000000001 00110100000000100000000000000001
00101000100000010000000000000010 00101000100000010000000000000010 00010100001000000000000000000100 00010100001000000000000000000100 01110000010001000001000000000010 01110000010001000001000000000010 00100000100001001111111111111111 00100000100001001111111111111111 00001000000100000000000000001010 00001000000100000000000000001010 0000001111100000000000000000100000000011111000000000000000001000
04/18/23 CSE1303 Part B lecture notes 9
Assembly languageAssembly language Problems with machine languageProblems with machine language
very difficult to write or even readvery difficult to write or even read Need a compromiseNeed a compromise
a language that humans can cope with that:a language that humans can cope with that:• supports supports commentscomments, , variable namesvariable names, line , line labelslabels, etc, etc• has has human-readablehuman-readable versions of machine instructions versions of machine instructions
but is easily converted to machine languagebut is easily converted to machine language• usually a 1-to-1 relationship between each language usually a 1-to-1 relationship between each language
instruction and its equivalent machine instructioninstruction and its equivalent machine instruction• ideally, this conversion done by a computer program ideally, this conversion done by a computer program
that won’t make mistakesthat won’t make mistakes languages of this kind called languages of this kind called assembly languagesassembly languages
• many thousands of these exist but broadly similarmany thousands of these exist but broadly similar
04/18/23 CSE1303 Part B lecture notes 10
MIPS assembly languageMIPS assembly language
Function to calculate a factorial:Function to calculate a factorial:
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n <= 1 # end if n <= 1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
04/18/23 CSE1303 Part B lecture notes 11
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n<=1 # end if n<=1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
labellabel: identifies a line of the program…: identifies a line of the program…
looploop::
… … so that other lines can refer to it.so that other lines can refer to it.
looploop
04/18/23 CSE1303 Part B lecture notes 12
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n<=1 # end if n<=1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1
end: jr $ra end: jr $ra # Return.# Return.
j loopj loop
instructioninstruction: one per line (this one actually : one per line (this one actually decreases a value by adding minus one )decreases a value by adding minus one )
addi $a0, $a0, -1addi $a0, $a0, -1
04/18/23 CSE1303 Part B lecture notes 13
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n<=1 # end if n<=1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
operationoperation: human readable version of : human readable version of instruction “instruction “opcode”opcode” (operation code). Describes (operation code). Describes the kind of instruction ( here:- “the kind of instruction ( here:- “bbranch if ranch if lless or ess or eequal” )qual” )
bleble
04/18/23 CSE1303 Part B lecture notes 14
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n<=1 # end if n<=1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
registerregister: one of 32 fast storage : one of 32 fast storage areas inside CPU. Denoted by areas inside CPU. Denoted by ‘$’ then short name (or ‘$’ then short name (or number)number)
$$rara
collectively these called operands
immediate immediate (literal) value: (literal) value: here equals the here equals the number 1number 1
11
04/18/23 CSE1303 Part B lecture notes 15
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res=1# Res=1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n<=1 # end if n<=1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
assembler directiveassembler directive: starts with a dot ‘.’ : starts with a dot ‘.’ char Command for the translator (not the char Command for the translator (not the CPU). Here defines start of text (or code) CPU). Here defines start of text (or code) segment.segment.
..texttext
04/18/23 CSE1303 Part B lecture notes 16
MIPS assembly languageMIPS assembly language
# Computes factorial of number (n) in $a0# and returns result (“Res”) in $v0# and returns result (“Res”) in $v0 .text.textfact: ori $v0, $zero, 1 fact: ori $v0, $zero, 1 # Res = 1# Res = 1loop: ble $a0, 1, end loop: ble $a0, 1, end # end if n <= 1# end if n <= 1 mul $v0, $v0, $a0mul $v0, $v0, $a0 # Res=Res*n # Res=Res*n addi $a0, $a0, -1 addi $a0, $a0, -1 # n=n-1# n=n-1 j loopj loop
end: jr $ra end: jr $ra # Return.# Return.
# Res = 1
commentcomment: starts at : starts at #, extends to end of #, extends to end of
lineline
# Computes factorial of number (n) in $a0
04/18/23 CSE1303 Part B lecture notes 17
Assembly languageAssembly language
Why learn assembly language?Why learn assembly language? convenient convenient halfway stophalfway stop between high- between high-
level language (e.g., C) and machine level language (e.g., C) and machine languagelanguage• break down problem of running C programs break down problem of running C programs
into two smaller problems: (C into two smaller problems: (C assembly, assembly, assembly assembly machine language) machine language)
sometimes necessary to use this sometimes necessary to use this low-low-level programming languagelevel programming language when timing when timing is critical or when memory size is limitedis critical or when memory size is limited• e.g. device drivers or embedded computers e.g. device drivers or embedded computers
04/18/23 CSE1303 Part B lecture notes 18
Assembly languageAssembly language
Programs already exist which Programs already exist which already turn C into machine already turn C into machine languagelanguage compilerscompilers
Most people don’t write low-level Most people don’t write low-level device driversdevice drivers
So, So, why learn assembly languagewhy learn assembly language??
04/18/23 CSE1303 Part B lecture notes 19
Assembly languageAssembly language
Why learn assembly language?Why learn assembly language? to understand to understand how compilers workhow compilers work to understand how to write to understand how to write effective Ceffective C
(and other languages)(and other languages)• efficientefficient• correctcorrect• portableportable
to become a more to become a more valuablevaluable programmerprogrammer
04/18/23 CSE1303 Part B lecture notes 20
CompilersCompilers
CompilerCompiler is a set of tools (programs) is a set of tools (programs) which which convert higher-levelconvert higher-level codecode (e.g., C) (e.g., C) toto machine languagemachine language translatortranslator
• C to assembly languageC to assembly language assemblerassembler
• assembly language to object codeassembly language to object code linkerlinker
• object code to executable programobject code to executable program
Most compilers (e.g., Borland C++, GCC) Most compilers (e.g., Borland C++, GCC) can perform any or all of the above stepscan perform any or all of the above steps options can halt processing at any stageoptions can halt processing at any stage
04/18/23 CSE1303 Part B lecture notes 21
CompilersCompilers
C code (.c)C code (.c)
assembly assembly language (.s)language (.s)
object code object code (.o)(.o)
executableexecutable
translation translation performed by performed by a translatora translator
assembling assembling performed by performed by
an assembleran assembler
linking linking performed by performed by
a linkera linker
04/18/23 CSE1303 Part B lecture notes 22
CompilersCompilers Some programs may Some programs may refer torefer to variables or variables or
functions defined functions defined elsewhereelsewhere e.g., e.g., printfprintf or or strlenstrlen functions functions
Object codeObject code is code compiled as far as it is code compiled as far as it can be without resolving these referencescan be without resolving these references mainly machine language, but with additional mainly machine language, but with additional
data identifying symbols that still need data identifying symbols that still need resolvingresolving
LinkingLinking is combining object modules and is combining object modules and resolving these references so that they resolving these references so that they refer to the correct memory addressesrefer to the correct memory addresses produces pure machine language executableproduces pure machine language executable
04/18/23 CSE1303 Part B lecture notes 23
CompilersCompilers
Programs may be constructed from many Programs may be constructed from many source filessource files some programs are millions of lines long, too some programs are millions of lines long, too
long for one filelong for one file Separate files can be compiled to the object Separate files can be compiled to the object
code stage independently of the otherscode stage independently of the others if one file is changed, other files don’t need if one file is changed, other files don’t need
recompilingrecompiling The multiple object files are then linked The multiple object files are then linked
simultaneously to form executable codesimultaneously to form executable code including libraries written by OS vendorincluding libraries written by OS vendor
04/18/23 CSE1303 Part B lecture notes 24
CompilersCompilers
/* Ref. to foo, defined elsewhere. *//* Ref. to foo, defined elsewhere. */extern int foo;extern int foo;/* Prototype for function func. *//* Prototype for function func. */void func(void);void func(void);
/* main calls func, defined in two.c. *//* main calls func, defined in two.c. */int main() {int main() { /* ... */ foo++; func(); /* ... *//* ... */ foo++; func(); /* ... */}}
one.cone.c#include <stdio.h>#include <stdio.h>/* Definition of foo is here. *//* Definition of foo is here. */int foo;int foo;static int secret; static int secret; /* private to two.c *//* private to two.c */
/* Function called from another file. *//* Function called from another file. */void func(void) {void func(void) { printf( /* ... */ );printf( /* ... */ );}}
two.ctwo.c
gcc -c two.c -o two.ogcc -c two.c -o two.ogcc -c one.c -o one.ogcc -c one.c -o one.o
-c-c option stops option stops at object code at object code
stagestage
gcc one.o two.o -o abcgcc one.o two.o -o abctwo.otwo.o
..01.. foo ..11.. func ..01.. foo ..11.. func ..11.. printf ..01....11.. printf ..01..
librarylibrary..00.. printf ..11.. ..00.. printf ..11.. strlen ..11.. strlen ..11.. lotslots ... ...
abcabc011010100100100100110 011010100100100100110 110010011000011010010110010011000011010010
one.oone.o..01.. foo ..11.. main ..01.. foo ..11.. main ..00.. func ..10....00.. func ..10..
04/18/23 CSE1303 Part B lecture notes 25
CompilersCompilers
To write multi-file C programsTo write multi-file C programs if file if file one.cone.c needs needs to call functionto call function in in two.ctwo.c, put , put function prototypefunction prototype in in one.cone.c• void func(void);void func(void);
compiler now knows enough about compiler now knows enough about function to compile function to compile one.cone.c
linker will locate the function during linker will locate the function during linkinglinking
04/18/23 CSE1303 Part B lecture notes 26
CompilersCompilers
To write multi-file C programsTo write multi-file C programs if file if file one.cone.c needs needs to refer to global to refer to global
variablevariable in in two.ctwo.c, declare it in , declare it in one.cone.c with with externextern modifier modifier• extern int foo;extern int foo;
compiler tags variable as being compiler tags variable as being outside file, and does not try to make outside file, and does not try to make another variable with the same name another variable with the same name in in one.cone.c
linker will associate linker will associate one.cone.c’s variable ’s variable with with two.ctwo.c’s variable during linking’s variable during linking
04/18/23 CSE1303 Part B lecture notes 27
CompilersCompilers
To write multi-file C programsTo write multi-file C programs if file if file two.ctwo.c wants to keep a global variable wants to keep a global variable
or function or function privateprivate (inaccessible outside the (inaccessible outside the file), declare it with the file), declare it with the staticstatic modifier modifier• static int secret;static int secret;• static void mine(char* x);static void mine(char* x);
compiler will remove all references to the compiler will remove all references to the variable or function in object filevariable or function in object file
other files cannot refer to it, even with other files cannot refer to it, even with externextern modifier modifier
files in same program may each have files in same program may each have staticstatic variable/function of the same name variable/function of the same name
04/18/23 CSE1303 Part B lecture notes 28
CompilersCompilers
To compile multi-file C programsTo compile multi-file C programs compile compile eacheach source file to the source file to the object codeobject code
stagestage• with GCC, use with GCC, use -c-c option option• gcc gcc -c-c file1.c -o file1.o file1.c -o file1.o• creates object code called creates object code called file1.ofile1.o
link alllink all object files together object files together• with GCC, no option needed provided files with GCC, no option needed provided files
have have .o.o suffix suffix• gcc file1.o file2.o file3.o -o finalgcc file1.o file2.o file3.o -o final• creates executable file called creates executable file called finalfinal• 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 gcc file1.o file2.o file3.o -lm-lm -o final -o final
04/18/23 CSE1303 Part B lecture notes 29
CompilersCompilers
To compile multi-file C programsTo compile multi-file C programs if all files are small, let GCC compile if all files are small, let GCC compile
all of them at once to executable fileall 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 typenot efficient but easier to type
04/18/23 CSE1303 Part B lecture notes 30
Covered in this lectureCovered in this lecture
InterpretersInterpreters Machine languageMachine language Assembly languageAssembly language
structurestructure CompilersCompilers
what a compiler doeswhat a compiler does Stages of code generationStages of code generation
compiling, assembling, linkingcompiling, assembling, linking compiling multi-file programscompiling multi-file programs
04/18/23 CSE1303 Part B lecture notes 31
Going furtherGoing further
Object file formatObject file format getting your hands dirty with object getting your hands dirty with object
codecode makemake
a tool that makes managing multi-file a tool that makes managing multi-file programs easyprograms easy
uses file date stamps to determine uses file date stamps to determine which files need updatingwhich files need updating
man makeman make (manual page) (manual page)
04/18/23 CSE1303 Part B lecture notes 32
Next timeNext time
MIPS architectureMIPS architecture registersregisters memorymemory
Running MIPS programsRunning MIPS programs fetch-execute cyclefetch-execute cycle SPIM simulatorSPIM simulator
Reading:Reading:
lecture notes section B08lecture notes section B08