Professional Documents
Culture Documents
Application Scripting
Interpreted
Languages Languages Or Compiled
High-Level (Java, C#) (Perl, Python, VB)
Chapter 11 Languages
System Programming Languages
(C, C++)
Introduction to Assembly Language
Compilation
Programming in C Low-Level
(x86, PowerPC, SPARC, MIPS)
Assembly
Languages
Machine Language
(x86, PowerPC, SPARC, MIPS)
Based on slides © McGraw-Hill
Additional material © 2004/2005/2006 Lewis/Martin
Hardware
(Application-Specific Integrated
Circuits or ASICs)
CSE 240 2
Disadvantages
Starting today: we’re doing C programming • Slower and larger programs (in most cases)
• C is still common for systems programming • Can’t manipulate low-level hardware
• You’ll need it for the operating systems class (CSE380) !All operating systems have some assembly in them
• Ultimately, for a deeper understanding of any language (Java)
Verdict: assembly coding is rare today
CSE 240 3 CSE 240 4
Our Challenge C is Similar To Java Without:
All of you already know Java Objects
• We’re going to try to cover the basics quickly • No classes, objects, methods, or inheritance
• We’ll spend more time on pointers & other C-specific nastiness Exceptions
• Check all error codes explicitly
Created two decades apart Standard class library
• C: 1970s - AT&T Bell Labs • C has only a small standard library
• C++: 1980s - AT&T Bell Labs Garbage collection
• Java: 1990s - Sun Microsystems
• C requires explicit memory allocate and free
Safety
Java and C/C++ • Java has strong type checking, checks array bounds
• Syntactically similar (Java uses C syntax)
• In C, anything goes
• C lacks many of Java’s features
• Subtly different semantics
Portability
• Source: C code is less portable (but better than assembly)
• Binary: C compiles to specific machine code
CSE 240 5 CSE 240 6
Compiling a C Program C
Source and
Header Files
Compiler
Entire mechanism is usually called Source Code Analysis
the “compiler” • “Front end”
Preprocessor C Preprocessor • Parses programs to identify its pieces
• Macro substitution !Variables, expressions, statements, functions, etc.
• Conditional compilation Compiler • Depends on language (not on target machine)
• “Source-level” transformations Source Code
Analysis
Code Generation
!Output is still C Symbol Table • “Back end”
Target Code
Compiler Synthesis • Generates machine code from analyzed source
• Generates object file • May optimize machine code to make it run more efficiently
!Machine instructions • Very dependent on target machine
Library
Remaining Chapters
A more detailed look at many C features
• Variables and declarations
• Operators
•
•
Control Structures
Functions
Chapter 12
•
•
Pointers and Data Structures
I/O
Variables and
Emphasis on how C is converted to assembly language
Operators
Also see “C Reference” in Appendix D Based on slides © McGraw-Hill
Additional material © 2004/2005/2006 Lewis/Martin
CSE 240 27
Basic C Elements Data Types
Variables C has several basic data types
• Named, typed data items
int integer (at least 16 bits, commonly 32 bits)
Operators long integer (at least 32 bits)
• Predefined actions performed on data items float floating point (at least 32 bits)
• Combined with variables to form expressions, statements double floating point (commonly 64 bits)
char character (at least 8 bits)
Statements and Functions
• Group together operations 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
Signed vs unsigned:
• Default is 2’s complement signed integers
• Use “unsigned” keyword for unsigned numbers
CSE 240 29 CSE 240 30
'c'
Global variable is declared outside all blocks
'\n' /* newline */
'\xA' /* ASCII 10 (0xA) */
CSE 240 33 CSE 240 34
Example Expression
#include <stdio.h>
int itsGlobal = 0;
Any combination of variables, constants, operators,
and function calls
main() • Every expression has a type, derived from the types of its
{ components (according to C typing rules)
int itsLocal = 1; /* local to main */
printf("Global %d Local %d\n", itsGlobal, itsLocal);
{ Examples:
int itsLocal = 2; /* local to this block */
itsGlobal = 4; /* change global variable */ counter >= STOP
printf("Global %d Local %d\n", itsGlobal, itsLocal);
}
x + sqrt(y)
printf("Global %d Local %d\n", itsGlobal, itsLocal); x & z + 3 || 9 - w-- % 6
}
Output
Global 0 Local 1
Global 4 Local 2
Global 4 Local 1
CSE 240 35 CSE 240 36
Statement Operators
Expresses a complete unit of work Three things to know about each operator
• Executed in sequential order (1) Function
• What does it do?
(2) Precedence
Simple statement ends with semicolon • In which order are operators combined?
z = x * y; /* assign product to z */ • Example:
y = y + 1; /* after multiplication */ "a * b + c * d" is the same as "(a * b) + (c * d)"
because multiply (*) has a higher precedence than addition (+)
; /* null statement */ (3) Associativity
• In which order are operators of the same precedence
combined?
Compound statement formed with braces
• Example:
• Syntactically equivalent to a simple statement "a - b - c" is the same as "(a - b) - c"
{ z = x * y; y = y + 1; } because add/sub associate left-to-right
CSE 240 55
Control Structures If
Conditional if (condition)
action; F
• Making decision about which code to execute, based on condition
evaluated expression
• if
T
• if-else
• switch
action
Iteration
• Executing code multiple times, ending based on evaluated
expression
• while Condition is a C expression,
• for which evaluates to TRUE (non-zero) or FALSE (zero).
• do-while Action is a C statement,
which may be simple or compound (a block).
CSE 240 57 CSE 240 58
Chapter 14
• unsigned int x = ~0;
• Then, (x >> 10) will have 10 leading zeros
CSE 240 87
Function Example of High-Level Structure
Smaller, simpler, subcomponent of program void main()
Provides abstraction {
• Hide low-level details setup_board(); /* place pieces on board */
• Give high-level structure to program,
easier to understand overall program flow determine_sides(); /* choose black/white */
• Enables separable, independent development
/* Play game */
C functions while (no_outcome_yet()){
• Zero or multiple arguments (or parameters) passed in whites_turn();
blacks_turn(); Structure of program
• Single result returned (optional)
} is evident, even without
• Return value is always a particular type
knowing implementation.
}
In other languages, called procedures, subroutines, ...
CSE 240 89 CSE 240 90
Called function R6 R6
copy args into main main main
stack or regs allocate activation record
call function save registers
execute code
put result in AR or reg Before call During call After call
pop activation record
get result return