Mimir:Draft4 Appendix

From Openitware
Jump to: navigation, search

Back to Table of Contents

Appendix for code examples

Chapter 2 Code examples

  • 1
                    ; zstr_count:
                    ; Counts a zero-terminated ASCII string to determine its size
                    ; in:   eax = start address of the zero terminated string
                    ; out:  ecx = count = the length of the string

                    zstr_count:                   ; Entry point
00000030 B9FFFFFFFF      mov  ecx, -1              ; Init the loop counter, pre-decrement
                                                  ;  to compensate for the increment
00000035 41              inc  ecx                  ; Add 1 to the loop counter
00000036 803C0800        cmp  byte [eax + ecx], 0  ; Compare the value at the string's
                                                  ;  [starting memory address Plus the
                                                  ;  loop offset], to zero
0000003A 75F9            jne  .loop                ; If the memory value is not zero,
                                                  ;  then jump to the label called '.loop',
                                                  ;  otherwise continue to the next line
                                                  ; We don't do a final increment,
                                                  ;  because even though the count is base 1,
                                                  ;  we do not include the zero terminator in the
                                                  ;  string's length
0000003C C3              ret                       ; Return to the calling program
  • 2
(Phase 1) The Front End
       (Step 1) Lexical Analysis: This is where the high-level language text is read in and divided into tokens, each of which corresponds to a symbol in the    programming language, for instance a keyword, identifier, or symbol name.
       (Step 2) Syntax Analysis or Parsing: Please refer to the Parsing section as it goes into greater detail.
       (Step 3) Type-Checking: This step analyses the syntax tree of the language to check if the program violates certain consistency requirements. For example if a  variable is used but not declared or if it is used but in a context that does not make sense for the given type of the variable, such that if you attempted to use a boolean value as a function pointer. 
   (Phase 2) The Middle End
       (Step 4) Intermediate Code Generation: The program then takes what the front end has built from this point and generates a simple machine independent intermediate language. 
   (Phase 3) The Back End
       (Step 5) Register Allocation: The back end starts by taking the intermediate language that the middle end created and allocates the symbolic variable names to numbers each of which corresponds to a register in the machines architecture code.
       (Step 6) Machine Code Generation: This is where the intermediate language created in the middle end gets translated into assembly language for the specified machine architecture.
       (Step 7) Assembly and Linking: The assembly-language code is translated into binary representation and addresses of variables, function, etc., are determined.