⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cintro.doc

📁 一个简单的编译器
💻 DOC
📖 第 1 页 / 共 5 页
字号:
               LOAD R1,10
               LOAD R2,10
   
            The  translation  from  neumonics  to  instruction  values   is
         performed by  a  program  called  an  ASSEMBLER.  In  addition  to
         performing this translation, the assembler also allows LABEL names
         to be assigned to addresses. The labels may be  referred  to  from
         within other assembly  language  statements  instead  of  absolute
         addresses.
   
            When written in assembly language, our "count  to  10"  program
         would look something line this:
   
                   LOAD    R1,0
           LOOP:   ADD     R1,1
                   LOAD    R2,10
                   SUB     R2,R1
                   JMPNZ   LOOP
                   HALT
   
            As  you  can  see,  the  above  program  would  be  much   more
         understandable than a series of  numbers,  but  it  is  still  not
         obvious to someone other that the author what the  intent  of  the
         program is until he has followed through the loop, and  determined
         what is accomplished by each instruction.
   Intro to MICRO-C                                                 Page: 6


            Imagine that you have just started a  new  job  as  a  computer
         programmer, and your  manager  hands  you  a  listing  of  several
         hundred pages, each of which is full of  assembly  language  lines
         looking like the example  above,  and  says  "The  'SCAN'  command
         causes corruption of the database search parameters. This is  VERY
         important, could you stay and fix it tonight". You would have many
         hours  (days?)  ahead  of  you  trying  to   determine   what   is
         accomplished by each portion of the program. Now, imagine that the
         assembly language looked more like this:
   
           ;
           ; Simple demonstration program to count from 0 to 9
           ;
                   LOAD    R1,0        ; Begin with count of zero
           ; Execute this loop once for each count
           COUNT:  ADD     R1,1        ; Increment count
                   LOAD    R2,10       ; Loop termination value
                   SUB     R2,R1       ; Test R1, (result destroys R2)
                   JMPNZ   COUNT       ; Repeat until we reach 10
           ; We have reached 10 - All done
                   HALT                ; Stop processing
   
            The text statements following the ';' characters in  the  above
         example are called COMMENTS. They are completely  ignored  by  the
         assembler, but are very useful to anyone attempting to  understand
         the program.
   
      2.3 High Level Languages
   
            As you can see in the  preceeding  section,  assembly  language
         programming offers much of  an  improvement  over  programming  by
         direct instruction  values,  while  retaining  the  capability  to
         control  EXACTLY  the  individual  operations  the  program   will
         instruct the CPU to perform. Also, since the assembly language for
         a particular CPU is defined by the manufacturer, you can  be  sure
         that using it will allow you to take advantage  of  EVERY  feature
         and capability that has been designed  into  that  particular  CPU
         architecture.
   
            A good assembly language programmer can produce highly efficent
         and compact programs because of this power. For  this  reason  you
         will often see assembly  language  used  for  very  time  or  size
         intensive applications.
   Intro to MICRO-C                                                 Page: 7


            It would seem that assembly language would be the ideal  method
         of doing all you programming. There are however, several drawbacks
         to using assembly language:
   
         1) Efficent use of assembly language often requires a  "different"
            way of  looking  at  a  problem  and  strong  "logical"  mental
            dicipline.
            ** Not everyone is a good assembly language programmer **
   
         2) Assembly language source files are big.
            ** It takes much codeing to perform even simple operations **
            ** Significant time is spent entering source text **
            ** Greater chance of error during design and entry **
   
         3) Poorly documented assembly language is undecipherable.
            ** It is hard to maintain **
   
         4) Each assembly language is different and incompatible.
            ** Programs will run on only one type of CPU **
            ** Programmers have difficulty working on other CPUs **
   
            To help solve these problems,  there  are  a  number  of  "high
         level"  programming  languages  available.  The  main   difference
         between  assembly  and  high  level  languages  is  that  assembly
         language produces only  one  CPU  instruction  for  each  language
         "statement",  while  high  level  languages   can   produce   many
         instructions for each "statement".
   
            High level languages attempt to provide a method of programming
         by expressing ideas, rather than by directing the CPU  to  perform
         each individual operation. When using a high level  language,  you
         are freed from the task of keeping track of  register  and  memory
         usage, and can concentrate  on  expressing  the  algorithms  which
         accomplish the goal of the program.
   
            Here are some "high  level"  versions  of  our  "count  to  10"
         program:
   
   
       Basic:      100 FOR I=0 TO 10:NEXT I
   
       Fortran:        DO 100 i=0,10
                   100 CONTINUE
   
       Forth:      11 0 DO LOOP
   
       'C':        for(i=0; i <= 10; ++i);
   Intro to MICRO-C                                                 Page: 8


      2.4 Interpreters VS Compilers
   
            There  are   two   basic   types   of   high   level   language
         implementations, INTERPRETERS and COMPILERS.
   
            An INTERPRETER is a program which reads  your  source  program,
         and performs the actions indicated by  its  statements.  The  main
         advantages to this approach are:
   
         1) FAST DEVELOPMENT:  Interpreters  often  include  complete  text
            editors, which make it easy to  edit  and  debug  your  program
            without leaving the interpreter. Also,  since  the  program  is
            interpreted  directly,  there  is  no  waiting  to  compile  or
            assemble it before you can try out a new change.
   
         2) EASY DEBUGGING:  Since  the  interpreter  is  actually  another
            program, it will usually allow you to stop your program in  the
            middle of execution, examine/modify  variables,  trace  program
            flow, display callback stacks, etc. This makes  for  very  easy
            debugging. Also, a good interpreter will perform  very  through
            checking of your program as it interpretes,  thus  finding  and
            reporting design errors which might otherwise show up  only  as
            erratic and inconsistant program operation.
   
            And of course, there are drawbacks to interpreting:
   
         1) SLOW EXECUTION: The interpreter has to process  each  statement
            in your program and determine what action is  to  be  performed
            every time it encounters that statement. Many hundreds or  even
            thousands of instructions are executed to accomplish  this  FOR
            EACH STATEMENT.
   
         2) USES MEMORY: A good interpreter is a  fairly  complex  program,
            and therefore occupies a substantial portion of system  memory,
            meaning that less is available for your program & variables.
   
         3) DIFFICULTY OF USE: Once you are finished debugging,  you  would
            like to  make  your  program,  as  easy  to  use  as  possible.
            Unfortunatly, when using an interpreter,  you  always  have  to
            load and execute the interpreter before loading  and  executing
            your program.
   
            These disadvantages are so severe that interpreters are  rarely
         used for serious programs which are to be  used  frequently  by  a
         number of people. They are however, excellent learning  tools  for
         the novice computer user.
   Intro to MICRO-C                                                 Page: 9


            A COMPILER is a program which reads your  source  program,  and
         translates its statements into CPU INSTRUCTIONS which perform  the
         specified function. Instead of actually executing your program, it
         converts it to a form which can later be directly executed by  the
         CPU. Its main advantages are:
   
         1) FAST EXECUTION: Since the program will be executed directly  by
            the CPU, it will run much faster that  the  equivalent  program
            being translated by an interpreter.
   
         2) LESS MEMORY: Although a compiler is a very complex program, and
            uses lots of memory when it runs,  it  only  runs  once,  after
            which your program is executed by itself directly by  the  CPU.
            This means that the amount of memory required by  the  compiler
            does not affect the amount of memory which is available for use
            by your program when it runs.
   
         3) EASE OF USE: Since your program executes  by  itself,  you  can
            load and execute it directly from the operating system  command
            prompt.
   
            The main disadvantages of compilers over interpreters are:
   
         1) LONGER DEVELOPMENT: Many "traditional" compilers  require  that
            you prepare your source program using a  seperate  editor,  and
            then save it to a disk  file,  and  submit  that  file  to  the
            compiler. Every time you do this, you  have  to  wait  for  the
            compiler to finish before you can even try your  program.  NOTE
            some compiler vendors are  now  providing  integrated  editors,
            eliminating the "save and exit" step, however you may not  like
            the editor they have chosen for you.
   
         2) MORE  DIFFICULT  DEBUGGING:  Since  your  program  executes  by
            itself, you have to run a standard system debugger  to  monitor
            its execution. This will usually  be  somewhat  less  intuitive
            than an interpreters built in  debugging  features.  NOTE  some
            compiler  vendors  provide  a  "debug"  option  which  includes
            debugging information in the program, and  a  special  debugger
            which provides debugging facilities equal  to  or  better  than
            those available from most interpreters.
   
      2.5 Object Modules & Linking
   
            Most assemblers and compilers available today support  the  use
         of a LINKER. The linker is a program which  will  combine  several
         previously compiled (or assembled) programs called OBJECT  MODULES
         into a single larger executable  program.  This  helps  speed  the
         development process by eliminating  the  need  to  re-compile  the
         entire program when you have changed only one module.
   Intro to MICRO-C                                                 Page: 10


      2.6 Compiler Libraries
   
            Modern compilers promote  the  use  of  STRUCTURED  PROGRAMMING
         techniques, which make programs easier to debug and maintain. I do
         not propose to get into a  discussion  of  structured  programming
         methods, but the main idea is to divide the  program  into  simple
         parts, each of which performs a clearly defined function.
   
            Such functions often perform common algorithms required by many
         programs, and  hence  are  made  into  compiler  LIBRARIES.  These
         libraries are simply collections of small  useful  programs  which
         may be used from within your programs without you having to  write
         them. Most compiler manufacturers  provide  such  a  "library"  of
         functions which they  believe  to  be  commonly  needed,  and  the
         development tools necessary to link them with your programs.
   
      2.7 Portability
   
            One BIG advantage of high level languages is the fact that once
         a program is written and running on one CPU, you can  usually  get
         it  running  on  another  completely  different  CPU  with  little
         difficulty. This is because although the CPUs are  different,  the
         HIGH LEVEL LANGUAGE IS NOT CPU DEPENDANT AND REMAINS THE SAME. All
         you have to do is to re-compile your  program,  using  a  compiler
         which produces code for the new CPU.
   
            Actually, it usually takes a bit more effort than that, because
         the language or library functions may  differ  slightly  from  one
         implementation to another.
   
            This concept of PORTABILITY is one of the strong points of  the
         'C' language, and you will see it  mentioned  from  time  to  time
         throughout  this  manual.  In  addition  to  consistant   compiler
         language implementation, 'C' benefits from very "standard" library
         function definitions which are followed by most vendors.
   Intro to MICRO-C                                                 Page: 11


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -