Docsity

Understanding High-Level Languages and Compilation vs Interpretation, Study notes of Programming Languages

Partial preview of the text

Download Understanding High-Level Languages and Compilation vs Interpretation and more Study notes Programming Languages in PDF only on Docsity!

CS 350

Computer Organization & Assembly

Language Programming

Spring 2009

Notes 17, 2009–04–

1

Chapter 11: Intro C

Programming

C: A High-Level Language

• 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 3

C: A High-Level Language

• 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

Compilation vs. Interpretation

• Consider the following algorithm:

Get W from the keyboard. X = W + W Y = X + X Z = Y + Y Print Z to screen. 7

Compiling a C Pgm

• Compilation includes

preprocessor and link editing steps

• Preprocessor

- Macro substitution,

conditional compil- ation, source-level transformations

- Output is still C

Compiling a C Program

• Compiler

- Generates object file

  • (^) Machine instructions

• Linker

- Combine object files (including libraries) into

executable image 9

Compiler

• Source Code^ Analysis ("front end")

- Parses programs to identify its pieces.

  • (^) Variables, expressions, statements, declarations, functions, etc.

- Typechecking and other validity tests

- Depends on language (not on target machine)

Preprocessor 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 13

Preprocessor Directives

• #define^ STOP^0

- A^ macro (short for "macro-instruction")

- Before compiling, replace all instances of the

string "STOP" with the string "0"

- Used for values that won't change during

execution, but might change if the program is reused.

  • (^) Must recompile to get effect of new value.

Comments

• Comments^ begin with^ /*^ and end with^ */

- Can span multiple lines

- Cannot have a comment within a comment

- Comments are not recognized within a string

  • (^) Example: "ab/cd/e" would be printed as ab/cd/e

• As before, use comments to help reader

learn the non-obvious

15

main Function

• Every C program must have a function

called main().

• It gets executed when program is run.

• The code for the function goes within

brackets:

main() { /* code goes here */ }

Input and Output

• scanf("%d",^ &startPoint);

- String contains formatting directions for

looking at input.

- This call says to read a decimal integer and

assign it to the variable startPoint. (Don't worry about the & yet.) 19

More About Output

• Can print arbitrary expressions, not just

variables

printf("%d\n", a+b);

• Can print multiple expressions with a

single statement

printf("%d %d\n", a+b, c);

More About Output

• Different formatting options:

•!^ %d^ decimal integer

•!^ %x^ hexadecimal integer

•!^ %c^ ASCII character

•!^ %f^ floating-point number

21

Examples

• This code:

printf("%d is a prime number.\n", 43); printf("43 plus 59 in decimal is %d.\n", 43+59); printf("43 + 59 in hex is %x.\n", 43+59); printf("43+59 as a character is %c.\n", 43+59);

produces the output:

43 is a prime number. 43 plus 59 in decimal is 102. 43 + 59 in hex is 66. 43+59 as a character is f.

Compiling and Linking

• Various compilers available

- cc, gcc

- includes preprocessor, compiler, and linker

• Lots and lots of options!

- level of optimization, debugging

- preprocessor, linker options

- intermediate files — object (.o), assembler

(.s), preprocessor (.i), etc. 25

Remaining Chapters

• A^ more detailed look at many C features.

- Variables and declarations, operators, control

structures, functions, data structures, I/O

• Emphasis on how C is converted to LC-

assembly language.

- Also see C Reference in^ Appendix D.

Chapter 12: Variables and

Operators

27

Basic C Elements

• Variables

- named, typed data items

• Operators

- predefined actions performed on data items

- combined with variables to form expressions,

statements

- Rules and usage Implementation using LC-

Examples

• Some legal variables:

- i,^ mph,^ miles_per_hr,^ _green,

abcdefghijklmnopqrstuvwxyz78901, abcdefghijklmnopqrstuvwxyz78901zzzz

• Some illegal variables:

- 10sdigit,^ miles-per-hr,

ten'sdigit, done?, double

Reserved keyword

Same identifier

31

Literals

• Integers

- 123 ,^ -123,^ 0x123^ (hex)

• Floating point

- 6.023,^ 6e-1^ (6"10-#),^ 6.02e23^ (6.02"^10 $%)

• Characters:

- Any char between quotes; e.g.^ 'c'

- Special characters, e.g.^ '\n'^ (newline)

- Hex codes^ '\xA'^ (0xA,^ ASCII 10)

Local Scope

• The^ scope^ of a variable is the part of the

program it can be used in.

- In C,^ the compiler infers scope from where

variable is declared.

- Local scope: Function parameters, variables

declared inside a function or block.

  • (^) Block of code: Appears between { and }
  • (^) A variable is local to the block it is declared in; can also be used in blocks contained by the defining block (unless redefined). 33

Example of Local Scope

#include <stdio.h> main() { int x = 5; printf("x = %d\n", x); /* x = 5 / if (x < 0) { printf("x = %d\m", x); / x = 5 / } else { char x = 'c'; printf("x = %c\n", x); / x = c */ } }

Operators

• Programmers manipulate variables using

the operators provided by the high-level

language.

• Variables and operators combine to form

expressions and statements which

denote the work to be done by the

program.

37

Operators

• Each operator may correspond to many

machine instructions.

- Example:^ The multiply operator (*) typically

requires multiple LC-3 ADD instructions.

Expression

• Any combination of variables, constants,

operators, and function calls

- Every expr. has a type, derived from the types

of its components (according to typing rules)

- Examples:

• counter >= STOP

• x + sqrt(y)

• x & z + 3 || 9 – w— % 6

39

Statement

• Expresses a complete unit of work

- executed in sequential order

• Simple statement ends with semicolon

- z = x * y; /* assign product to z */

- y = y + 1; /* after multiplication */

- ; /* null statement */

• Compound statement groups simple

statements using braces.