Sie sind auf Seite 1von 42

CprE

288 Introduc0on to Embedded Systems


Exam 1 Review

Instructors:
Dr. Phillip Jones
Dr. Zhao Zhang

hGp://class.ece.iastate.edu/cpre288

Overview of Todays Lecture


Announcements
Exam 1 Review

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

C programming, ques0ons specic to the AVR ATmega128 processor


Memory Mapped I/O
Memory Layout
Interrupts

Open notes, no electronic devices except calculators



Covers material covered in lectures
No ques0ons about UARTs
No ques0ons about ADC

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

Know your Operators


Do you know the dierence between these two sets of
operators?
&
|
~
^

&&
||
!

hGp://class.ece.iastate.edu/cpre288

10

Know your Operators


char a = 20, b = 10, r = 5;
// math
r = a +
r = a
r = a *
r = a \
r = a %

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

Know how to use Operator Precedence


Precedence Operator

Can you use this table?

++ -()

[]
.
->
++ -+-

!~
(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

Know your Declara0ons


Do these declara0ons make sense?
void main() {
char x = 5, y = 10;
char z;
char array1[10];
char array2[] = {1, 2, 3};
char array3[5] = {1, 2, 3};
char *str = Hello!;
int i = 7;
int *ptr = &i;
int **pp = &ptr;
char *p;
}

hGp://class.ece.iastate.edu/cpre288

13

Know your Declara0ons


Do these declara0ons make sense?
struct House {
unsigned long
unsigned char
unsigned char
unsigned char
unsigned long
};

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

Access members of structs and unions


Bit-eld dened struct
Know the dierence between a struct and union

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

Memory Mapped I/O


Concept of Memory Mapped I/O
Device registers can be accessed from a program as if
accessing memory (i.e. devices registers are
mapped to memory addresses)
Allow sending commands and checking status of a
devices by just reading and wri0ng to memory
mapped loca0ons.
Polling device status vs. device ini0a0ng an interrupt.

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.

What ac0ons are taken when an interrupt occurs?


Normal program execu0on is paused
ISR execu0on begins
Normal program execu0on resumes axer ISR completes
hGp://class.ece.iastate.edu/cpre288

22

Wri0ng the Body of a func0on


There will be 1 or 2 problems where you
will be asked to write the body of a
func0on to implement a dened
computa0on.

hGp://class.ece.iastate.edu/cpre288

23

Show your work


Please show intermediate steps. This will help us give par0al
credit
Write down assump0ons. In general Profs/TAs will not
answer ques0ons during the test.
They will just tell you to write down your assump0ons
The excep0on will be if a major typo is found by a
student.

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

B. What range of values can be stored in an unsigned char?


C. What range of values can be stored in a signed char?
D. What is the value stored in x axer this code runs?
int x, y, z;
x = y = z = 10;


hGp://class.ece.iastate.edu/cpre288

26

Ques0on 1 (answer)
Name

Number of Bytes sizeof()

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

int (on ATmega 128)

-32,768 to 32,767

(pointer on ATmega 128)

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

double (on ATmega 128)

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

Review Homework Problems and Answers

hGp://class.ece.iastate.edu/cpre288

42

Das könnte Ihnen auch gefallen