程序代写代做代考 assembly C compiler assembler OSU CSE 2421

OSU CSE 2421
Required Reading:
Computer Systems: A Programmer’s Perspective, 3rd Edition,
Chapter 1 thru Section 1.3
Pointers on C,
Chapter 5 thru Section 5.1.3, 5.3 through the end of the chapter
J.E.Jones

OSU CSE 2421
Libraries
(e.g. printf() code)
Assembler
hello.c
Type in program source code (file.c) using an editor of your choice; plain text
% gcc –o hello hello.c
Source code
.c + .h = .i which is the ultimate source code – i.e., #includes expanded and #defines replaced, comments removed
Preprocessor
hello.i
Compiler
.i → .s which is assembler source code
Assembly Code (hello.s)
.s → .o which is an object file; fragments of machine code with unresolved symbols, i.e., some addresses not yet known (vars/subrs).
Object Code (hello.o)
Link Editor/ Linker
.o + library links a.out (default name); resolves symbols, generates an executable
Executable Code
hello
% hello [executes the program]
J.E.Jones

OSU CSE 2421
• Basic Data Types
• Constants
• Variables
• Identifiers
• Keywords
• Operator Precedence and Associativity
• Basic I/O (covered in a separate set of slides)
• Control structures (if, while, for, etc.)
(covered in a separate set of slides)
Can you associate one of the 4 programming language categories to each of these???
J.E.Jones

OSU CSE 2421
• •
Note that variables of type char are guaranteed to always be one byte. All others can differ depending upon the processor being used.

Integer Types
• char – smallest addressable unit, *always* 8 bits (1 byte); each byte has its own

Floating Point Types – (these are usually “inexact”, we’ll see why later)
• float – single precision (about 6 decimal digits of precision), (4 bytes)
• double – double precision (about 15 decimal digits of precision) (8 bytes)
• long double – about 30 decimal digits of precision (only C99 and after) (16 bytes) • double is constant default value; for 4 bytes values use ‘f’ suffix
address
• short – not used as much; typically is 16 bits (2 bytes)
• int – default type for an integer constant value; typically is 32 bits (4 bytes)
• long – do you really need it?; typically is 64 bits (8 bytes)
• long long – at least 64 bits, sometimes 128 bits, (only supported in C99 and after)
There is no fixed or maximum size for a type in C (except for char; otherwise, size depends on implementation), but the following relationships must hold:
• sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)<=sizeof(long long) • sizeof(float) <= sizeof(double) <= sizeof(long double) • True size of data types is dependent upon the size of the processor being used. J.E.Jones OSU CSE 2421 • Beside the basic types, there is a conceptually infinite class of derived types constructed from the fundamental types in the following ways: • arrays of objects (variables or derived types) of a given type; • pointers to objects of a given type; • structures containing a sequence of objects (variables or derived types) of various types; • unions capable of containing any of one of several objects of various types. • In general these methods of constructing data types (variables or derived types) can be applied recursively • An array of pointers to some type • An array of characters (i.e. a string) • Structures that contain pointers • And so on. J.E.Jones OSU CSE 2421 char ‘A’, ‘b’, 0x42 (hexadecimal), 127 unsigned char ‘©’, 255u 123, -1, 2147483647, 040 (octal), 0xab (hexadecimal) 123u, 2107433648, 040U (octal), 0xc2 (hexadecimal) 123L, 0x1FFFl (hexadecimal) 123ul, 0777UL (octal) 1.23F, 3.14e+0f 1.23, 2.718281828 1.23L, 9.99E-9L int unsigned int long unsigned long float double long double • Special characters • • • Not convenient to type on a keyboard Use single quotes (e.g. ‘\n’) Looks like two characters but is really only one \a \b \f \n \r \t \v alert (bell) character backspace formfeed newline \\ backslash \? question mark carriage return horizontal tab vertical tab \’ single quote \" double quote \ooo octal number \xhh hexadecimal number J.E.Jones OSU CSE 2421 • 2 ways (both legal) • Put the const keyword after the type keyword, or before the type keyword • Note: The compiler treats these as variables to which any assignment is invalid. • This means the declared const must be initialized with its (constant) value as part of the declaration, because the compiler will not allow statements which make assignments to it later! Treated as a read-only variable. • For program readability, pick one of the two ways and use it exclusively. Be consistent! • Examples: float const PI = 3.141593f; (f is used after value because double is default) const float PI = 3.141593f; • Convention is to use uppercase for declared constants, also for those with #define (see below). • Symbolic constants (with the #define directive - below) can be used anywhere a literal constant can be used, but constants defined with the const keyword can only be used where variables can be used. More on this later (with examples). • We will say more about constants as function parameters, pointers to constants, and constant pointers later. J.E.Jones OSU CSE 2421 • A name that substitutes for a value that cannot be changed • Can be used to define: • Constant • Statement • Mathematical expression • Uses a preprocessor directive: #define
is a text string with no white space;
is any text string (so it can be a mathematical expression) • for example, (3.1415927 * r * r)
• REMINDER: No semi-colon is used for preprocessor directives. • Coding convention is to use all capital letters for the name:
• #define AREA (3.141593 * r * r)
• #define AREA (PI*r*r)
• What might happen if parentheses aren’t included in these define statements?
• What if these statements used addition/subtraction rather than multiplication?
• Can be used any place you would use the actual value
• All occurrences are replaced by the preprocessor before the program is compiled by the compiler.
• Examples:
• The use of EXIT_SUCCESS in hello.c code
• #define PI 3.141593
• #define TRUE 1
J.E.Jones

OSU CSE 2421
• Purpose: define a variable (can also be a constant) before it is used.
• Format: type identifier (, identifier) ; Note: the parentheses here indicate any
number of identifiers, each preceded by a comma
• Initial value: can be assigned, but is not required (unless it is a constant)
• inti,j=5,k;
• char code, category;
• inti=123;
• const float PI = 3.1415926535f;
• double const PI = 3.1415926535;
• Type conversion: aka, type casting
• Directing the compiler to use a variable as a different type than the one used
in the declaration.
• Casting “larger” types to “smaller” types is dangerous (truncation occurs)
and should be done with extreme caution!!!
• To cast a variable to a different type explicitly, use: (type) identifier
• inti=65;
• char ch; /* range -128 to 127 */
• ch=(char)i; /*Whatisthevalueofch?*/
• What happens if we change the initial value of i to 165?
J.E.Jones

OSU CSE 2421
• Identifier Naming Rules: names for variables, constants, types and functions. • Can use a-z, A-Z, 0-9, and _ (i.e., alphanumeric, digits, and underscore) • No other characters can be used
• Case sensitive
• The first character must be a letter or _ (Usually don’t use _ , though, because it is used for operating system purposes).
• Keywords are reserved words, and may not be used as identifiers • (See the following slide for C keywords)
• No guarantee that any value past the 31st character will be recognized. (i.e. will let you use more characters, but no guarantee that it will parse it.)
• Identifier Naming Style (the grader will enforce these)
• Separate words with ‘_’ (this is the original style in C) OR capitalize the first
character of each word after the first (e.g., char_count or charCount) • Use all UPPERCASE for symbolic constants or macro (code chunk)
definitions.
• Be consistent. Be consistent. Be consistent.
• Be meaningful: Write “self-documenting code”; i.e., identifiers should give a
clear idea of what a variable, constant, type or function is being used for. • Sample Identifiers
• i0, j1, student_name, studentName, student_score, studentScore…
J.E.Jones

OSU CSE 2421
char US_Social_Security_Identification_LastName[50]; char US_Social_Security_Identification_FirstName[50];
Declares 2 50-character arrays
Symbol table for identifier names might only be 32 long (strings must be NULL terminated in C)
12345678911111111112222222222333 01234567890123456789012
US_Social_Security_Identificati\ 0
J.E.Jones

OSU CSE 2421
• Purpose: reserves a word or identifier to have a particular meaning
• The meanings of keywords — and, indeed, the meaning of the notion of
keyword — differs widely from language to language.
• You shouldn’t use them for any other purpose in a C program. They are
allowed, of course, within double quotation marks (as part of a string to be assigned or printed, for example; this is not using an identifier, actually).
You can make a copy of this chart as a reference on the midterm
J.E.Jones