Beruflich Dokumente
Kultur Dokumente
hGp://class.ece.iastate.edu/cpre288
hGp://class.ece.iastate.edu/cpre288
Announcements
Exam
1:
Thursday
10/9,
in
class
Open
book,
open
notes,
and
calculator
allowed
75
minutes
No
electronic
devices,
except
calculator
hGp://class.ece.iastate.edu/cpre288
EXAM FORMAT
hGp://class.ece.iastate.edu/cpre288
Exam
Format
Focus
60
points
total
15%
of
your
course
grade
hGp://class.ece.iastate.edu/cpre288
EXAM TOPICS
hGp://class.ece.iastate.edu/cpre288
Exam
Prepara0on
Suggested
prepara0on
steps:
Review
the
following
slides.
You
should
have
a
deep
understanding
of
the
content
that
appears
on
them
If
not,
go
back
and
review
the
lecture
material
on
the
given
topic
Run
through
the
ques0ons
at
the
end
of
this
PowerPoint
This
set
of
slides
are
not
comprehensive
Review
all
lecture
slides
Review
homework
ques0ons,
try
to
re-do
those
ques0ons
hGp://class.ece.iastate.edu/cpre288
Keywords
char
short
int
long
oat
double
enum
struct
union
break
case
con0nue
default
do
else
for
goto
if
return
switch
while
hGp://class.ece.iastate.edu/cpre288
auto
const
extern
register
signed
sta0c
unsigned
vola0le
sizeof
typedef
void
8
Syntax
Could
you
write
the
following
statements
by
hand?
Loops
(for,
while,
do)
Write
a
for
loop
to
sum
elements
of
an
array
or
count
characters
in
a
string
Do
you
know
the
syntax
of
a
do
while
loop,
for
loop,
and
while
loop?
typedef
Could
you
write
a
typedef
deni0on
Do
you
know
what
it
means
when
you
see
a
variable
type
like
uint8_t?
Switch
statements
Do
you
know
where
the
semi-colon
and
colons
go
in
a
switch/case
statement?
Do
you
understand
how
the
control
ow
can
fall
through
a
case?
Control
ow
Do
you
understand
the
keywords
break
and
conAnue
and
their
use?
hGp://class.ece.iastate.edu/cpre288
&&
||
!
hGp://class.ece.iastate.edu/cpre288
10
operations
b;
b;
b;
b;
b;
// bitwise operators
r = a & 3;
r = a | b;
r = a ^ 0xFF;
r = ~a;
r = a >> 3;
r = b << r;
// conditional
r = (r) ? a : b;
// boolean
r = a || b;
r = a && b;
r = !a;
r = a < 20;
r = b <= 15;
r = b > 10;
r = a >= b;
// post and prefix
a++;
++a;
b--;
--b;
// assignments
r = a = b = 42;
r += a;
r -= b;
hGp://class.ece.iastate.edu/cpre288
11
++ -()
[]
.
->
++ -+-
!~
(type )
*
&
sizeof
3
4
5
6
7
8
9
10
11
12
13
*/%
+<< >>
< <=
> >=
== !=
Ternary
conditional
Direct
assignment
Assignment
by
sum
and
difference
Assignment
by
product,
quotient,
and
remainder
Assignment
by
bitwise
l eft
shift
and
right
shift
Assignment
by
bitwise
AND,
XOR,
and
OR
Comma
Right-to-Left
Right-to-left
Left-to-right
^
|
&&
||
?:
+= -=
*= /= %=
<<= >>=
&= ^= |=
15
Associativity
Left-to-right
&
14
Description
Suffix/postfix
i ncrement
and
decrement
Function
call
Array
subscripting
Element
selection
by
reference
Element
selection
through
pointer
Prefix
i ncrement
and
decrement
Unary
plus
and
minus
Logical
NOT
and
bitwise
NOT
Type
cast
Indirection
( dereference)
Address-of
Size-of
Multiplication,
division,
and
modulus
( remainder)
Addition
and
subtraction
Bitwise
l eft
shift
and
right
shift
For
relational
operators
<
and
respectively
For
relational
operators
>
and
respectively
For
relational
=
and
respectively
hGp://class.ece.iastate.edu/cpre288
Left-to-right
12
hGp://class.ece.iastate.edu/cpre288
13
value;
baths;
bedrooms;
stories;
footage;
void main() {
struct House my_home;
struct House *bob_home = &my_home;
my_home.baths =
my_home.value =
bob_home->baths
bob_home->value
1;
115000;
= 3;
= 230000;
}
hGp://class.ece.iastate.edu/cpre288
14
Data
Structures
Array
access
Pointers
Dereference
Address
operator
hGp://class.ece.iastate.edu/cpre288
15
Pointers
What
are
pointers
Rela0onship
between
pointers
array
names
Pointer
arithme0c
Students:
Be
sure
to
review
class
examples
and
homework
problems
related
to
pointers
hGp://class.ece.iastate.edu/cpre288
16
C-strings
Review
the
concept
of
C-strings
Rela0onship
between
C-strings
arrays
Understand
the
importance
of
the
NULL
byte
of
a
C-string
hGp://class.ece.iastate.edu/cpre288
17
Bitwise
opera0ons
Sepng
bits
to
1
Clearing
bits
to
0
Tes0ng
for
bits
set
to
1
Tes0ng
for
bits
cleared
to
0
Generic
systema0c
checking
example
if(
(x
&
MASK_ALL1s)
==
MASK_ALL1
&&
(~x
&
MASK_ALL0s)
==
MASK_ALL0s
&&
(x
&
MASK_ANY1s)
&&
(x
&
MASK_ANY0s)
)
hGp://class.ece.iastate.edu/cpre288
18
Variable Scope
Global
variables
Local
variables
Local
static
variables
Global
static
variables
volatile
variables
hGp://class.ece.iastate.edu/cpre288
19
hGp://class.ece.iastate.edu/cpre288
20
Memory
Layout
Stack
What
type
of
informa0on
is
placed
in
the
stack?
Typically
starts
at
the
top
of
memory
and
grows
downward
Func0ons/Procedures
Stack-Frame:
The
informa0on
placed
on
the
stack
by
a
func0on
when
it
is
called
(e.g.
local
variables,
input
parameters,
return
address)
Heap
Stores
dynamically
allocated
memory
(i.e.
allocated
using
malloc).
Typically
starts
at
a
low
address
and
grows
upward
Sta0c
Data
segment:
Region
of
memory
where
Global
variables
and
sta0c
local
variables
are
stored.
hGp://class.ece.iastate.edu/cpre288
21
Interrupts
What
is
an
interrupt?
An
event
that
occurs
from
outside
your
program
(e.g.
Keyboard
reques0ng
service,
Timer
that
0mes
out,
UART
receiving
a
byte
of
informa0on)
What
is
an
Interrupt
Service
Rou0ne
(ISR)?
Code
associated
with
a
given
interrupt
(i.e.
a
given
event),
that
executes
when
that
interrupt
occurs.
22
hGp://class.ece.iastate.edu/cpre288
23
hGp://class.ece.iastate.edu/cpre288
24
QUESTIONS
hGp://class.ece.iastate.edu/cpre288
25
Ques0on
1
A. How
many
bytes
are
each
of
the
following
types
(on
the
ATmega128)?
char,
short,
int,
long,
oat,
double
hGp://class.ece.iastate.edu/cpre288
26
Ques0on
1
(answer)
Name
Range
char
-128 to 127
signed char
-128 to 127
unsigned char
0 to 255
short
-32,768 to 32,767
unsigned short
0 to 65,535
-32,768 to 32,767
Address Space
long
-2147483648 to 2147483647
signed long
-2147483648 to 2147483647
unsigned long
0 to 4294967295
long long
-4294967295 to 4294967295
oat
1.175e-38 to 3.402e38
1.175e-38
to
3.402e38
hGp://class.ece.iastate.edu/cpre288
27
Ques0on
2
A. Analyze
the
following
code:
char
r
=
0,
s
=
1,
t
=
2;
char
*p1
=
&s;
char
*p2
=
&t;
char
**pp3
=
&p1;
*p1
=
10;
**pp3
=
15;
*p2
=
30;
*pp3
=
&r;
**pp3
=
5;
*p1
=
25;
hGp://class.ece.iastate.edu/cpre288
28
Ques0on
2
(answer)
A. Analyze
the
following
code:
char
r
=
0,
s
=
1,
t
=
2;
char
*p1
=
&s;
char
*p2
=
&t;
char
**pp3
=
&p1;
*p1
=
10;
**pp3
=
15;
*p2
=
30;
*pp3
=
&r;
**pp3
=
5;
*p1
=
25;
//
s
=
10
//
s
=
15
//
t
=
30
//
p1
=
&r
//
r
=
5
//
r
=
25
10
15
15
30
15
30
15
30
25
15
30
hGp://class.ece.iastate.edu/cpre288
29
Ques0on
3a
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
=
3)
||
(x
&
1))
{
//
do
something
}
hGp://class.ece.iastate.edu/cpre288
30
Ques0on
3a
(answer)
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
=
3)
||
(x
&
1))
{
//
do
something
}
The
statement
is
always
true.
Know
the
dierence
between
the
assignment
operator
(=)
and
the
equality
operator
(==).
The
value
on
the
lex
(x
=
3)
is
always
true,
as
the
value
of
an
assignment
is
the
value
that
was
assigned.
This
allows
programmers
to
have
compound
assignments.
hGp://class.ece.iastate.edu/cpre288
31
Ques0on
3b
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
==
3)
||
(x
&
1))
{
//
do
something
}
hGp://class.ece.iastate.edu/cpre288
32
Ques0on
3b
(answer)
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
==
3)
||
(x
&
1))
{
//
do
something
}
The
statement
is
true
if
x
is
either
equal
to
3
or
bit
0
is
set.
hGp://class.ece.iastate.edu/cpre288
33
Ques0on
4a
When
is
the
condi0on
of
the
following
if
statement
true?
if
(x
&
0x08
==
0x08)
{
//
do
something
}
hGp://class.ece.iastate.edu/cpre288
34
Ques0on
4a
(answer)
When
is
the
condi0on
of
the
following
if
statement
true?
if
(x
&
0x08
==
0x08)
{
//
do
something
}
The
statement
is
true
if
bit
0
of
x
is
1.
Operator
precedence
evaluates
the
==
operator
before
the
bitwise
AND
(&).
Assumes
TRUE
(i.e.
0x08
==
0x08)
evaluates
to
1
hGp://class.ece.iastate.edu/cpre288
35
Ques0on
4b
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
&
0x08)
==
0x08)
{
//
do
something
}
hGp://class.ece.iastate.edu/cpre288
36
Ques0on
4b
(answer)
When
is
the
condi0on
of
the
following
if
statement
true?
if
((x
&
0x08)
==
0x08)
{
//
do
something
}
The
statement
is
true
if
bit3
of
x
is
set.
x
=
0b00001000;
x
=
0b01001110;
x
=
0b00101001;
x
=
0b00000000;
x
=
0b11100000;
condi0on
is
TRUE
condi0on
is
TRUE
condi0on
is
TRUE
condi0on
is
FALSE
condi0on
is
FALSE
hGp://class.ece.iastate.edu/cpre288
37
Ques0on
6a
What
are
the
values
of
c1,
c2,
c3,
and
c4
axer
the
following
code
executes?
char
myarray[3]
=
{1,
2,
3};
char
*ptr
=
myarray;
char
c1
=
*ptr++;
char
c2
=
*ptr;
char
c3
=
myarray[0];
char
c4
=
myarray[1];
hGp://class.ece.iastate.edu/cpre288
38
Ques0on
6a
(answer)
Posix
increment
has
higher
char
myarray[3]
=
{1,
2,
3};
associa0on
precedence
than
dereference
operator.
But
char
*ptr
=
myarray;
the
increment
does
not
occur
un0l
the
next
line
c1
is
1
char
c1
=
*ptr++;
c2
is
2
char
c2
=
*ptr;
c3
is
1
char
c3
=
myarray[0];
c4
is
2
char
c4
=
myarray[1];
hGp://class.ece.iastate.edu/cpre288
39
Ques0on
6b
What
are
the
values
of
c1,
c2,
c3,
and
c4
axer
the
following
code
executes?
char
myarray[3]
=
{1,
2,
3};
char
*ptr
=
myarray;
char
c1
=
(*ptr)++;
char
c2
=
*ptr;
char
c3
=
myarray[0];
char
c4
=
myarray[1];
hGp://class.ece.iastate.edu/cpre288
40
Ques0on
6b
(answer)
char
myarray[3]
=
{1,
2,
3};
char
*ptr
=
myarray;
char
c1
=
(*ptr)++;
char
c2
=
*ptr;
char
c3
=
myarray[0];
char
c4
=
myarray[1];
c1
is
1
c2
is
2
c3
is
2
c4
is
2
hGp://class.ece.iastate.edu/cpre288
41
hGp://class.ece.iastate.edu/cpre288
42