Unformatted text preview:

1Chapter 10/11/12/13Chapter 10/11/12/13High Level Programming LanguagesVariables and OperatorsThe runtime stackEmphasis on how C-like languages are converted to LC-3 assembly2Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyA 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 thoughLC-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!3Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCompilation vs. InterpretationCompilation vs. Interpretation Different ways of translating high-level language Interpretation– interpreter = program that executes program statements– generally one line/command at a time– limited processing– easy to debug, make changes, view intermediate results– languages: BASIC, LISP, Perl, Java, Matlab, C-shell Compilation– translates statements into machine language does not execute, but creates executable program– performs optimization over multiple statements– change requires recompilation can be harder to debug, since executed code may be different– languages: C, C++, Fortran, PascalGet W from the keyboard.X = W + WY = X + XZ = Y + YPrint Z to screen.How many arithmetic operations when interpreted? When compiled with optimization?4Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCompiling 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 imageCSource andHeader FilesC PreprocessorCompilerSource CodeAnalysisTarget CodeSynthesisSymbol TableLinkerExecutableImageLibraryObject Files5Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyCompilerCompiler 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 information6Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyA Simple C ProgramA Simple C Program#include <stdio.h>#define STOP 0/* Function: main *//* Description: counts down from user input to STOP */main(){ /* variable declarations */int counter; /* an integer to hold count values */int startPoint; /* starting point for countdown */printf("Enter a positive number: ");scanf("%d", &startPoint); /* output count down */for (counter=startPoint; counter >= STOP; counter--)printf("%d\n", counter);}27Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyPreprocessor 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.8Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyOutput 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 variablesprintf("%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 number9Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyExamples of OutputExamples of Output This code: printf("%d is a prime number.\n", 43); printf("43 plus 59 in decimal is %d.\n", 43+59); printf("43 plus 59 in hex is %x.\n", 43+59); printf("43 plus 59 as a character is %c.\n", 43+59); produces this output: 43 is a prime number. 43 + 59 in decimal is 102. 43 + 59 in hex is 66. 43 + 59 as a character is f.10Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyInput 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 lecture11Wright State Univ ersity, College of EngineeringDr. Doom, Computer Science & Eng ineeringCEG 320/520Comp. Org. & AssemblyData TypesData Types Variables are used as names for


View Full Document

Wright CEG 320 - Chapter 10/11/12/13

Download Chapter 10/11/12/13
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 10/11/12/13 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 10/11/12/13 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?