Docsity

Understanding Compilation: High-Level Languages to LC-3 Assembly at Wright State, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Understanding Compilation: High-Level Languages to LC-3 Assembly at Wright State and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter 10/11/12/13Chapter 10/11/12/

High Level Programming Languages

Variables and Operators The runtime stack Emphasis on how C-like languages are converted to LC-3 assembly

2 Wright State University, College of Engineering CEG 320/

A HighA High--Level LanguagesLevel Languages

 Gives symbolic names to values

  • don’t need to know which register or memory location

 Provides abstraction of underlying hardware

  • operations do not depend on instruction set
  • example: can write ā€œa = b * cā€, even though LC-3 doesn’t have a multiply instruction

 Provides expressiveness

  • use meaningful symbols that convey meaning
  • simple expressions for common control patterns (if-then-else)

 Enhances code readability

 Safeguards against bugs

  • can enforce rules or conditions at compile-time or run-time

 If it can be specified in a high-level language then it MUST be do-able in

assembly!

4 Wright State University, College of Engineering CEG 320/

Compiling a C ProgramCompiling a C Program

 Entire mechanism is usually

called the ā€œcompilerā€

 Preprocessor

  • macro substitution
  • conditional compilation
  • ā€œsource-levelā€ transformations  output is still C

 Compiler

  • generates object file  machine instructions

 Linker

  • combine object files (including libraries) into executable image C Source and Header Files C Preprocessor Compiler Source Code Analysis Target Code Synthesis Symbol Table Linker Executable Image Library Object Files

5 Wright State University, College of Engineering CEG 320/

CompilerCompiler

 Source Code Analysis

  • ā€œfront endā€
  • parses programs to identify its pieces  variables, expressions, statements, functions, etc.
  • depends on language (not on target machine)

 Code Generation

  • ā€œback endā€
  • generates machine code from analyzed source
  • may optimize machine code to make it run more efficiently  Consider automated HTML generation…
  • very dependent on target machine

 Symbol Table

  • map between symbolic names and items
  • like assembler, but more kinds of information

7 Wright State University, College of Engineering CEG 320/

Preprocessor DirectivesPreprocessor Directives

 #include <stdio.h>

  • Before compiling, copy contents of header file (stdio.h) into source code.
  • Header files typically contain descriptions of functions and variables needed by the program.  no restrictions -- could be any C source code

 #define STOP 0

  • Before compiling, replace all instances of the string "STOP" with the string "0"
  • Called a macro
  • Used for values that won't change during execution, but might change if the program is reused. (Must recompile.)

 Every C program must have exactly one function called main().

  • Be careful with what you #include!
  • main() determines the initial PC.

8 Wright State University, College of Engineering CEG 320/

Output with printfOutput with printf

 Variety of I/O functions in C Standard Library.

 Must include <stdio.h> to use them.

 printf: Can print arbitrary expressions, including formatted variables

printf("%d\n", startPoint - counter);

 Print multiple expressions with a single statement

 printf("%d %d\n", counter, startPoint -

counter);

 Different formatting options:

 %d decimal integer

 %x hexadecimal integer

 %c ASCII character

 %f floating-point number

10 Wright State University, College of Engineering CEG 320/

Input with scanfInput with scanf

 Many of the same formatting characters are available for user input.

 scanf("%c", &nextChar);

  • reads a single character and stores it in nextChar

 scanf("%f", &radius);

  • reads a floating point number and stores it in radius

 scanf("%d %d", &length, &width);

  • reads two decimal integers (separated by whitespace), stores the first one in length and the second in width

 Must use address-of operator (&) for variables being modified.

  • We’ll revisit pass by reference/value in a future lecture

11 Wright State University, College of Engineering CEG 320/

Data TypesData Types

 Variables are used as names for data items.

 Each variable has a type , type qualifiers, and a storage class which tells

the compiler how the data is to be interpreted (and how much space it

needs, etc.).

 int counter;

 Basic data types:

  • Integral: int (at least 16 bits) Qualifiers: signed, unsigned, long
  • Floating-point: float (at least 32 bits), double
  • Character: char (at least 8 bits)
  • Enumerated: enum hobbits {bilbo, frodo, samwise, pippen, merry}

 Storage class: automatic, static, register

 Derived data types: pointers, arrays, structures

 Exact size can vary, depending on processor

  • int is supposed to be "natural" integer size;
  • for LC-3, that's 16 bits -- 32 bits for most modern processors

13 Wright State University, College of Engineering CEG 320/

Variables and ScopeVariables and Scope

 Where are variable stored? Where can they be accessed? Why?

 All C variables are defined as being in one of two storage classes

  • Automatic storage class (on the stack, uninitialized)
  • Static storage class (in the global memory area, initialized to 0)

 Compiler infers scope from where variable is declared unless specified

  • programmer doesn't have to explicitly state (but can!)
  • automatic int x;
  • static int y;

 Global: accessed anywhere in program (default static)

  • Global variable is declared outside all blocks

 Local: only accessible in a particular region (default automatic)

  • Variable is local to the block in which it is declared
  • block defined by open and closed braces { }
  • can access variable declared in any "containing" block

14 Wright State University, College of Engineering CEG 320/

Allocating Space for VariablesAllocating Space for Variables

 Global data section

  • All global variables stored here (actually all static variables)
  • R4 points to beginning (global pointer)

 Run-time stack

  • Used for local variables
  • R6 points to top of stack (stack pointer)
  • R5 points to top frame on stack (frame pointer)
  • New frame for each block (goes away when block exited)

 Offset = distance from beginning

of storage area

  • Global: LDR R1, R4, #
  • Local: LDR R2, R5, #- 3

instructions

global data

run-time

stack

0x 0xFFFF

PC

R

R

R

16 Wright State University, College of Engineering CEG 320/

A software stackA software stack

 Implemented in memory

  • The Top Of Stack moves as new data is entered  Here R6 is the TOS register, a pointer to the Top Of Stack

17 Wright State University, College of Engineering CEG 320/

ExampleExample

 #include <stdio.h>  int itsGlobal = 0;  main()  {  int itsLocal = 1; /* local to main /  printf("Global %d Local %d\n", itsGlobal, itsLocal);  {  int itsLocal = 2; / local to this block /  itsGlobal = 4; / change global variable */  printf("Global %d Local %d\n", itsGlobal, itsLocal);  }  printf("Global %d Local %d\n", itsGlobal, itsLocal);  }

 Output

 Global 0 Local 1 Global 4 Local 2 Global 4 Local 1

19 Wright State University, College of Engineering CEG 320/

Example: Compiling to LCExample: Compiling to LC-- 33

 #include <stdio.h>  int inGlobal;  main() {  int inLocal;  int outLocalA;  int outLocalB;  inLocal = 5;  inGlobal = 3;  /* perform calculations /  outLocalA = inLocal++ & ~inGlobal;  outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);  / print results */  printf("The results are: outLocalA = %d, outLocalB = %d\n",  outLocalA, outLocalB);  } Name Type Offset Scope inGlobal int 0 global inLocal int 0 main outLocalA int - 1 main outLocalB int - 2 main

20 Wright State University, College of Engineering CEG 320/

The stack frameThe stack frame

 Local variables are stored in a stack frame associated

with the current scope

  • As we change scope, we effectively change both the top and the bottom of the stack
  • R6 is the stack pointer – holds the address of the top of the stack
  • R5 is the frame pointer – holds address of the base of the current frame.

 Symbol table ā€œoffsetā€ gives the distance from

the base of the frame.

  • A new frame is pushed on the run-time stack each time a block is entered.
  • Because stack grows downward (towards memory address x0000) the base is the highest address of the frame, and variable offsets are negative.

outLocalB

outLocalA

R5^ inLocal

R