0% found this document useful (0 votes)
7 views60 pages

Slot03 04 BasicComputation

Uploaded by

tranquockhanh933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views60 pages

Slot03 04 BasicComputation

Uploaded by

tranquockhanh933
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Introduction to PFC # Review

• Computer program
A set of instructions that computer hardware will execute
• Issues for a program/software
Usability, Correctness, Maintainability, Portability
• Computer software
A set of related programs
• Steps to develop a software
Requirement collecting, Analysis, Design, Implementing, Testing, Deploying, Maintaining
• Data
Specific values that describe something
• Information
Mean of data
• Fundamental Data Units
Bit, Nibble, Byte, KB, MB, GB, TB
• Data Representation
Number systems: 2, 10, 8, 16
• Program Instructions
<opcode, operand1, operand 2>
• Programming Languages
Machine language, Assembly, High-level languages
[PFC] Basic Computation 1
Basic Computation

PhD. Vo Hong Khanh ([email protected])

[PFC] Basic Computation 2


1 What is datatype?

2 Understand variables and contansts

3 Declare variables and contansts

4 Understand basic memory operations

5 Use basic operations

6 Express operations on data

7 Understand precedence

8 Understand casting

9 Understand input and output

[PFC] Basic Computation 3


Introduction
• A task that hardware must • Values can be changed when • Fixed values that can’t be changed
perform on data the program execute when the program executes
Instruction Variables Constants

Data Data operations Numerical data


• Can be variables, constants • 2 basic operations on data are • Numerical data can participate
• Stored in main memory (RAM) READ and WRITE in expressions
[PFC] Basic Computation 4
Variables

[PFC] Basic Computation 5


Variables and Data Types
• A variable is a name referencing ...
to a memory location (address)
0000 1001
− Holds binary data C71FA8 a
1100 0011
− 2 basic operations: set value,
get value. 1010 1100
− When the program is compiled, 1100 1100
the compiler will determine the C71FA4 b
1000 0110
position where the variable is 0010 0011
allocated.
• Questions C71FA3 1110 1010 c
− Where is variable?
0110 0100
→ Address C71FA1 d
0011 1110
− How many bytes does it
occupy? C71FA0 1111 0001 e
→ Data type ...

[PFC] Basic Computation 6


Data Types
• Typed languages, such as C, subdivide the universe of data
values into sets of distinct type.
• A data type defines:
−How the values are stored
−How the operations on those values are performed.
• Typed languages defined some primitive data types.
• C has 4 primitive data types:
Type Length Range
int Word -32,768 to 32,767 (16 bit)
(length of CPU register) -2,147,483,648 to 2,147,483,647 (32 bit)
char 1 byte -128 to 127
float 4 bytes 3,4 * 10-38 to 3,4 * 1038
double 8 bytes 1,7 * 10-308 to 1,7 * 10308
[PFC] Basic Computation 7
Variables
Where are variables stored and how many bytes do they occupy?

c:6356751 'A'
i:6356744 1

l:6356740 1000
• The operator & will get the address of a variable or code.
• The operator sizeof(var/type) return the size (number f:6356736 0.5
of byte) occupied by a variable/type
d:6356728 12.809
[PFC] Basic Computation 8
Do Yourself Now

[PFC] Basic Computation 9


[PFC] Basic Computation 10
Data Types

[PFC] Basic Computation 11


Qualifiers
• We can qualify the int data type so that it contains
a minimum number of bits.
• Qualifiers:
−short : at least 16 bits
−long : at least 32 bits
−long long : at least 64 bits
• Standard C does not specify that a long double
must occupy a minimum number of bits, only that
it occupies no less bits than a double.

[PFC] Basic Computation 12


Integral Values
• C stores integral values in equivalent binary form.
• Non-Negative Values:
− Intel uses this little-endian ordering. Each CPU family has it’s
own way to store data.
− Motorola uses big-endian ordering.
So, compilers must have a
suitable way for copying
data from this variable to
other. And due to this
103 09 Value is 103 03
stored:
reason also, each
102 F5 102 E1 compiler can run well
09F5E103
101 E1 101 F5 only on a specific
100 03 100 09 family of CPU only. A
supplier may supply some
Least significant Most significant
versions of their compiler
is stored in is stored in
lowest byte lowest byte
for some CPU families.
[PFC] Basic Computation 13
Negative and Positive Values
• Computers store negative integers using encoding schemes:
− Two's complement notation.
− One's complement notation.
− Sign magnitude notation.
• All of these schemes represent non-negative integers identically.
• The most popular scheme is two's complement.
• To obtain the two's complement of an integer, we:
− Flip the bits (1-complement)
− Add one → 2-complement
• For example:

[PFC] Basic Computation 14


Exercises
What is the two's complement notation of
8 bits or
1100 0001 1111 1111 1100 0001
• -63____________________________ 16 bits?

1111 1111 0010 0101


• -219___________________________

Convert the following binary notation to decimal:


193 -11
• 1111 0101______________________
187 -69
• 1011 1011______________________
positive/
negative?
[PFC] Basic Computation 15
Unsigned Integers
• We can use all of the bits available to store the
value of a variable.
• With unsigned variables, there is no need for a
negative-value encoding scheme.
Type Size Min Max – 32 bit Max – 16 bit

unsigned short ≥ 16 bits 0 65,535

unsigned int 1 word 0 4,294,967,295 65,535

unsigned long ≥ 32 bits 0 4,294,967,295

unsigned long long ≥ 64 bits 0 18,446,744,073,709,551,615

[PFC] Basic Computation 16


Cultural Symbols (Characters)
• We store cultural symbols using an integral data type.
• We store a symbol by storing the integer associated with
the symbol.
• Over 60 encoding sequences have already been defined.

We use the ASCII encoding sequence throughout this course


[PFC] Basic Computation 17
The ASCII table for characters

[PFC] Basic Computation 18


Exercises – The ASCII table
What is the encoding for
• '0' _______________________________________
48
97
• 'a' _______________________________________
• 'A' _______________________________________
65

Convert the following binary notation to an character:


'm'
• 0110 1101 ________________________________
'M'
• 0100 1101 ________________________________

Convert the following decimal notation to an character:


'z'
• 122 _____________________________________
'#'
• 35 ______________________________________

[PFC] Basic Computation 19


[PFC] Basic Computation 20
Floating-Point Data
• Computers store floating-point data using two separate
components:
− An exponent
− A mantissa
1.2345 = 1.2345 * 100 Give your comment
123.45 = 1.2345 * 102 about position of the
1234.5 = 1.2345 * 103 point mark and it’s
0.0012345 = 1.2345 * 10-3 mantissa

Limits on float and double data type in the IEEE standards:

[PFC] Basic Computation 21


IEEE 754, 32 bits float

[PFC] Basic Computation 22


[PFC] Basic Computation 23
IEEE 754, 64 bits double

[PFC] Basic Computation 24


Declaration

[PFC] Basic Computation 25


Variable Declarations in C
data_type identifier [= initial value];
For example:
char section;
int numberOfClasses;
double cashFare = 2.25;
Naming Conventions: Name is one word only
−Must not be a C reserved word.
−Some compilers allow more than 31 characters,
while others do not. To be safe, we avoid using
more than 31 characters.
First char The rest
Letter or '_' Letters / digits / '_'
[PFC] Basic Computation 26
Exercises
• Which of the following is an invalid identifier?
whale giraffe's 4me2 _how_do_you_do
camel_back digt3register senecac.on.ca

• Select a descriptive identifier for and write a complete


declaration for:
shelf_of_books
−A shelf of books________________________________
CashRegister
−A cash register_________________________________
partTimeStudent
−A part_time student_____________________________
group_programs
−A group of programs ____________________________

[PFC] Basic Computation 27


Some operations on variables
• Assign a constant value to a variable.
• Assign the value of another variable to a variable.
• Output the value of a variable.
• Input a fresh value into a variable's memory location.
constant
AnotherVar
anotherVar
Monitor
Keyboard variable
File
File
Netwwork
Network
[PFC] Basic Computation 28
1. What is a variable?
2. What is a data type?
3. Characteristics of a data type are ... and ...
4. The size of the int data type is ... Bytes.
5. Choose the wrong declarations:
a) int n=10;
b) char c1, c2='A';
c) int m=19; k=2;
d) char c3; int t;
e) float f1; f2=5.1;
6. Explain little-endian ordering and big-endian ordering.
[PFC] Basic Computation 29
1. What is a variable?
2. What is a data type?
3. Characteristics of a data type are ... and ...
4. The size of the int data type is ... Bytes.
5. Choose the wrong declarations:
a) int n=10;
b) char c1, c2='A';
c) int m=19; k=2;
d) char c3; int t;
e) float f1; f2=5.1;
6. Explain little-endian ordering and big-endian ordering.
[PFC] Basic Computation 30
Constants

[PFC] Basic Computation 31


Literals
• Constant values are specified directly in the source code.
• They can be
−Character literals (constant characters)
−String literals(constant strings)
−Number literals (constant numbers)

[PFC] Basic Computation 32


Literals: Characters, Strings
4 ways for representing a character literal
Enclose the character single quotes 'A'
Decimal ASCII code of the character 65 for 'A'
Octal ASCII code of the character 0101 for 'A'
Hexadecimal ASCII code of the character 0x41 for 'A'

Assign value to a variable:


The operator =

[PFC] Basic Computation 33


Literals: Escape Sequences
Pre-defined literals for special actions

[PFC] Basic Computation 34


Escape Sequences - Example
Error!
Why?

Modify
Change \ to \\ then run it

[PFC] Basic Computation 35


Literals: Numbers
The compiler will convert directly numeric literals (constants)
to binary numbers and put them in the executable file.
How long of binary constants?
→ They depend on their data types specified by programmers.
• Default:
▪ Integral value → int
▪ Real number → double
• Specifying data type of constants: Suffixes after numbers.

[PFC] Basic Computation 36


Named Constants
Use the pre-processor (pre-compiled directive) Compiler will
#define or the keyword const allocate
memory
location for
constants
that are
declared
using the
keyword
const

[PFC] Basic Computation 37


Named Constants - Notes
Attention when the directive #define is used:
• The compiler will not allocate memory block for values but all pre-defined
names in the source code will be replaced by
their values before the translation performs
(The MACRO REPLACEMENT)
• A name is call as a MACRO.

RUN &
RUN
Error

[PFC] Basic Computation 38


3 ways to specify a constant in a source program:
use a literal, use the keyword __________,
constant and
specify a macro using the directive __________
#define

[PFC] Basic Computation 39


Input and Output

[PFC] Basic Computation 40


Input/Output Variables
Data inputted from keyboard are ASCII codes
Data printed out to monitor are ASCII codes
3
Binary code
of 3 for
Monitor is 3 calculating
Convert 0011 0011
a character
device 0000 0011
Convert 0011 0011
ASCII code
of the digit 0011 0011
3 (is 51 – 3
00110011)

Keyboard is 3
a character
device Conversion rules are pre-defined in C

[PFC] Basic Computation 41


Conversion Specifiers

[PFC] Basic Computation 42


Example

scanf("%d%d", &n, &m) →


scanf("%d%d", 4210784, 2293620)
Means that get keys pressed then change
them to decimal integers and store them
to memory locations 4210784, 2293620.
[PFC] Basic Computation 43
Input and Output statements
Input a value to a variable:
scanf("input format", &var1, &var2,...)

Output the value of a variable:


printf("output format", var1, var2,...)

[PFC] Basic Computation 44


1. Explain means of parameters of the
scanf(...) and the printf(...) functions.

2. Use words "left" and "right". The


assignment x=y; will copy the value
in the ... side to the ... side.

[PFC] Basic Computation 45


Exercises
1. Develop a C program in which 2 integers, 2 float numbers
and 2 double numbers are declared. Ask user for values of
them then print out values and addresses of them. Write
down the memory map of the program.
2. Run the following program: Why user do not have
a chance to stroke the
ENTER key before the
program terminate?

Modify and re-run:


getchar();
getchar();
[PFC] Basic Computation 46
Expressions

[PFC] Basic Computation 47


Expressions
Expression is a valid association of constants,
variables, operators and functions and returns
one result only.
• Examples
32 – x + y/6 16.5 + 4/sqrt(15)*17 – 8
45 > 5*x y = 17 + 6*5/9 – z*z
• Hardware for calculating expressions: ALU
• Operations that can be supported by ALU:
−Arithmetic
−Relational
−Logic operations
[PFC] Basic Computation 48
Arithmetic Operators
Op. Syntax Description Example
Leaves the variable, constant or
+ +x y = +x ;  y = x;
expression unchanged
- -x Reverses the sign of the variable y = -x;
+ x+y Add/substract values of two z = x + y; t = x - y;
- x-y operands
z = x - y;
x*y Multiplies values of two operands
* / z = 10 / 3; → 3
x/y Get the quotient of a division
z = 10.0 / 3; → 3.333
Get remainder of a integral 17 % 3 → 2
% x%y
division 15.0 % 3 → ERROR
++ ++x --x Increase/decrease the value of a
-- x++ x-- variable (prefix/postfix operators) Demo in the next slide.

[PFC] Basic Computation 49


++, -- operators (prefix/postfix)

[PFC] Basic Computation 50


Arithmetic Operators - Example

RUN &
RUN
Error

Integral division only


n is integer → OK
x is double → Error

[PFC] Basic Computation 51


Relational Operators
Operator Relational Operators Action For comparing values
< Less than Return
<= Less than or equal o 0 for false
>= Greater than or equal o 1 for true
> Greater than
== Equal
!= Difference (not equal)

[PFC] Basic Computation 52


Logical Operators
Operator Logical Operators Action Association of conditions
&& AND Return
|| OR o 0 for false
! NOT o 1 for true

[PFC] Basic Computation 53


Bitwise Operators
Operator Relational Operators Action

&, |, ^ AND, OR, XOR operators will act


on a pair of bits at the same position in 2 operands
<< Left shift bits of the operand (operands unchanged)

>> Right shift operator makes the sign bits of the operand is
preserved (operands unchanged) How many byte (bits)
~ Inverse bits of the operand does operand occupy?
n=12: 0000 0000 0000 1100 n&m
m= 8: 0000 0000 0000 1000 0000 0000 0000 1100
k=–1: 1111 1111 1111 1111 0000 0000 0000 1000
0000 0000 0000 1000 →
→88
n|m
0000 0000 0000 1100
0000 0000 0000 1000
12
→ 12
0000 0000 0000 1100 →
n^m
0000 0000 0000 1100
0000 0000 0000 1000
→4
0000 0000 0000 0100 → 4
[PFC] Basic Computation 54
Bitwise Operators
Operator Relational Operators Action

&, |, ^ AND, OR, XOR operators will act


on a pair of bits at the same position in 2 operands
<< Left shift bits of the operand (operands unchanged)

>> Right shift operator makes the sign bits of the operand is
preserved (operands unchanged) How many byte (bits)
~ Inverse bits of the operand does operand occupy?
n=12: 0000 0000 0000 1100 n << 1 0
m= 8: 0000 0000 0000 1000 0000 0000 0000 1100 → 24
k=–1: 1111 1111 1111 1111
n << 2 00
0000 0000 0000 1100 → 48

0
0000 0000 0000 1100 → 6
n >> 1
11
1111 1111 1111 1111 → -1
k >> 2
[PFC] Basic Computation 55
Bitwise Operators
Operator Relational Operators Action

&, |, ^ AND, OR, XOR operators will act


on a pair of bits at the same position in 2 operands
<< Left shift bits of the operand (operands unchanged)

>> Right shift operator makes the sign bits of the operand is
preserved (operands unchanged) How many byte (bits)
~ Inverse bits of the operand does operand occupy?
n=12: 0000 0000 0000 1100
~t
m= 8: 0000 0000 0000 1000
0000 0000 0000 0010
k=–1: 1111 1111 1111 1111
1111 1111 1111 1101 → -3
0000 0000 0000 0011 → 3

[PFC] Basic Computation 56


Assignments Operators
value
Variable_name = expression;
lvalue Assignment lvalue
(Left value) operator (Left value)

a = b = c = 10; int a = int b = int c = 10;

Operator Shorthand Longhand Meaning


+= age += 4 age = age + 4 Add 4 to age
-= age -= 4 age = age – 4 Subtract 4 to age
*= age *= 4 age = age * 4 Multiply 4 to age
/= age /= 4 age = age / 4 Divide age by 4
%= age %= 4 age = age % 4 Remainder after age / 4
[PFC] Basic Computation 57
Precedence and Associativity
Precedence Operator Description Assocciativity
1 () Parentheses Left to Right
2 ++ -- (post) Increment, Decrement Left to Right
3 ++ -- (pre) Increment, Decrement Right to Left
+ - & !
4 Positive, Negative, And Bit, Not Right to Left
(all unary)
5 (data type) Casting Right to Left
6 * / % Multiplication, Division, Modulus Left to Right
7 + - Addition, Subtraction Left to Right
Less than, Less than or equal to,
8 < <= > >= Left to Right
Greater than, Greater than or equal to
9 == != Equal to, Not Equal to Left to Right
10 && Conditional AND Left to Right
11 || Conditional OR Left to Right
12 ?: Ternary Conditional Right to Left
= += -=
13 Assignment Operators Right to Left
*= /= %=
[PFC] Basic Computation 58
Casting
• The ALU does not perform operations on operands of differing data type directly.
• If a binary expression contains operands of differing type, a C compiler changes
the data type of one of the operands to match the other.
• Data type hierarchy: double, float, long, int , char.
Explicit Types of Implicit
datatype var1 = (datatype)var2;
Typecasting Typecasting Typecasting

int n = 3; chart

long t = 123;
short
double x = 5.3;
int
3*n + 620*t – 3*x
(int*int)+(int*long)-(int*double) long

int + long - double


float
long - double
double double

[PFC] Basic Computation 59


• SE1604 slot 5 xg slide này

[PFC] Basic Computation 60

You might also like