Você está na página 1de 36

ELE62041 |ec02-6-|anguage-| .

1
8ae|d Nooshabad|
ELE62041
H|croprocessors and |nterfac|ng
Lecture 2: 6-Language Rev|ew - 1
http:||webct.edtec.unsw.edu.au|
Harch, 200
8ae|d Nooshabad|
sae|dunsw.edu.au
ELE62041 |ec02-6-|anguage-| .2
8ae|d Nooshabad|
0verv|ew
6 8yntax
|mportant T|db|ts |n 6
Po|nters
Pass|ng Arguments to Funct|ons
ELE62041 |ec02-6-|anguage-| .3
8ae|d Nooshabad|
Rev|ew: what |s 8ubject about?
||0 system Processor
6omp||er
0perat|ng
8ystem
(w|ndows XP}
App||cat|on (Netscape}
0|g|ta| 0es|gn
6|rcu|t 0es|gn
|nstruct|on 8et
Arch|tecture
6oord|nat|on of many levels of abstraction
0atapath & 6ontro|
trans|stors
Hemory
hardware
8oftware
Assemb|er
ELE62041
ELE62041 |ec02-6-|anguage-| .4
8ae|d Nooshabad|
Rev|ew: Programm|ng Leve|s of Representat|on
h|gh Leve| Language
Program (e.g., 6}
Assemb|y Language
Program (e.g. ARH}
Hach|ne Language
Program (ARH}
Corlro| 3|gra| 3pec|l|cal|or
ompiler
Assembler
Machine lnterpretation
temp = v[kj;
v[kj = v[k+1j;
v[k+1j = temp;
|dr r0 , [r2, #0j
|dr r1 , [r2, #4j
str r1 , [r2, #0j
str r0 , [r2, #4j
1110 0101 1001 0010 0000 0000 0000 0000
1110 0101 1001 0010 0000 0000 0000 0100
1110 0101 1000 0010 0001 0000 0000 0000
1110 0101 1000 0010 0001 0000 0000 0100

ALU0P[0:3j <= |nstReg[9:11j & HA8K


C2011
ELE62041 |ec02-6-|anguage-| .5
8ae|d Nooshabad|
Rev|ew: what w||| You |earn |n ELE62041?
Learn b|g |deas |n H|croprocessors & |nterfac|ng
5 6|ass|c components of a 6omputer
Pr|nc|p|e of abstract|on, used to bu||d systems as |ayers
0ata can be anyth|ng (|ntegers, f|oat|ng po|nt, characters}:
a program determ|nes what |t |s
8tored program concept: |nstruct|ons just data
Pr|nc|p|e of stack and stack frames
6omp||at|on vs. |nterpretat|on thru system |ayers
Pr|nc|p|e of Loca||ty, exp|o|ted v|a a memory h|erarchy
(cache}
ELE62041 |ec02-6-|anguage-| .
8ae|d Nooshabad|
Rev|ew: 5 6|ass|c 6omponents of a 6omputer
Control
Datapath
Memory
Processor
Input
Output
Control
Datapath
Memory
Processor
Input
Output
Network/Bus
& #eg|slers
ELE62041 |ec02-6-|anguage-| .7
8ae|d Nooshabad|
S
e
c
o
n
d
a
r
y

S
e
r
I
a
I

P
o
r
t

P
r
I
m
a
r
y

S
e
r
I
a
I

P
o
r
t

VA V8 VC
E
x
p
a
n
s
I
o
n

C
o
n
n
e
c
t
o
r

1
Memory Ior
VIrtexE
(U9, U10,
U18, U19)
FIash
ROM
(U25)
XIIInx
VIrtexE
(U16)
Spartan
XL (U4)
Ethernet
(U7)
System Memory
SRAM ModuIes
(U11U14, U21U24)
AtmeI
CPU
(U17)
]TAC
oot
Power
Ethernet
E
x
p
a
n
s
I
o
n

C
o
n
n
e
c
t
o
r

2


S8
S8

SA
SA
VS
VS
P
o
w
e
r


LCD ModuIe
Power 1
Power 2

P
o
w
e
r

Cround Link
P
o
w
e
r

0 1 2 3
7 4 5 6
LEDs
Spartan VS Terminals
Spartan SA Terminals
Spartan S Terminals

O
n
J
O
f
f

S1 S2 S3
RESET
Switches Connector
S7 S8 S9 S10
S6 S5
LEDs Connector
L
E
D
s

S4




Rev|ew: ELE62041 08LHU hardware
08LHU |s
A oard for 21
st
6entury
The 8tate-of-the-Art 0eve|opment
oard
08LHU Features:
0es|gned by the Un|vers|ty of
Hanchester w|th |ots of co||aborat|on
from UN8w
An ARH H|crocontro||er
w|th 2 H of F|ash and up to 4 H of
8RAH Hemory
2 X|||nx FPCAs for extended
|nterfac|ng and spec|a||sed co-
processors
0pt|ona| Ethernet ch|p
L60 Hodu|e
Lots of uncomm|tted 8w|tches and
LE0s
Term|na| connector to FPCAs
Two P6s
ELE62041 |ec02-6-|anguage-| .8
8ae|d Nooshabad|
Rev|ew: 08LHU hardware |ock 0|agram
Two Pr|nted c|rcu|t oards
icrocont roller us
Peripheral us (Port s A and )
& oard Expansion oard
FIash ROM
(U25)
SerIaI Ports
System Memory
SRAM ModuIes
(U11U14,
U21U24)
OptIonaI
Ethernet
(U7)
Power
SuppIy
LEDs
(D8D15)
8oot SeIect
SwItch (U1)
LCD ModuIe
UncommItted
SwItches
UncommItted
LEDs
S8 SA VS VC V8 VA
OptIonaI
Memory
(U9, U10,
U18, U19)
XIIInx
VIrtexE
(U16)
XIIInx
SpartanXL
(U4)
ExpansIon
Connector 2
ExpansIon
Connector 1
AtmeI CPU
(U17)
|| 0ela||s ard C|rcu|l 0|agrars |rc|uded or lre
Corpar|or C0-#
ELE62041 |ec02-6-|anguage-| .9
8ae|d Nooshabad|
0u|ck 8urvey
how many of you have exper|ence w|th:
Java?
6++?
6?
|mportant: You w||| not |earn how to code |n 6
|n th|s one |ecture! You'|| st||| need some sort
of 6 reference for th|s course.
ELE62041 |ec02-6-|anguage-| .10
8ae|d Nooshabad|
6omp||at|on (#1|3}
6 comp||ers take 6 and convert |t |nto an
arch|tecture spec|f|c mach|ne code (str|ng of
1s and 0s}.
Un||ke Java wh|ch converts to arch|tecture
|ndependent code.
Un||ke haske|||8cheme, Hathemat|ca, Hat|ab
env|ronments wh|ch |nterpret the code.
ut how |s |t arch|tecture spec|f|c?
You'|| know the answer to th|s by the end of next
week.
ELE62041 |ec02-6-|anguage-| .11
8ae|d Nooshabad|
6omp||at|on (#2|3}
Advantages of 6-sty|e comp||at|on:
Creat run-t|me performance: genera||y much faster
than haske|| or Java for comparab|e code
(because |t opt|m|zes for a g|ven arch|tecture}
0K comp||at|on t|me: enhancements |n
comp||at|on procedure (Makefiles} a||ow on|y
mod|f|ed f||es to be recomp||ed
ELE62041 |ec02-6-|anguage-| .12
8ae|d Nooshabad|
6omp||at|on (#3|3}
0|sadvantages of 6-sty|e comp||at|on:
A|| comp||ed f||es (|nc|ud|ng the executab|e} are
arch|tecture spec|f|c, depend|ng on both the 6PU
type and the operat|ng system.
Executab|e must be rebu||t on each new system.
- 6a||ed "port|ng your code" to a new arch|tecture.
The "changeFcomp||eFrun [repeatj" |terat|on
cyc|e |s s|ow
ELE62041 |ec02-6-|anguage-| .13
8ae|d Nooshabad|
oredUN8w
or|ng Lectures and or|ng LecturersUN8w!?
A|| Lecturers you have had so far have been bor|ng!
... | am a |ecturer too. 8o | must be bor|ng as we||!
| know many bor|ng th|ngs. Hany very, very bor|ng th|ngs. |
must be bor|ng. | must bore. | am a |ecturer.
ut.
|n another sense, to bore |s to d|g, to probe under the surface,
to uncover that wh|ch has been h|dden, to v|ew that wh|ch has
not prev|ous|y been seen...
Fror Crar|es Yao|or: Forrs
ELE62041 |ec02-6-|anguage-| .14
8ae|d Nooshabad|
6 8yntax
include <stdio.h>
int main (void) {
unsigned int exp = 1;
int k;
/* Compute 2 to the 31st.*/
for (k=0; k<31; k++) {
exp = exp * 2;
}
...
return 0;
}
ra|r |s
ca||ed
oy 3
Replaces current line by
contents of specified file;
"< > means look in
system area, " " " user
area.
Declare
before use;
each
sequence of
variable
declarations
must follow a
left brace.
/*. */ indicate commnets.
gcc accepts "// comments
but they aren't legal C.
ELE62041 |ec02-6-|anguage-| .15
8ae|d Nooshabad|
6 8yntax: Cenera|
Very s|m||ar to Java, but w|th a few m|nor but |mportant
d|fferences
header f||es (.h} conta|n funct|on dec|arat|ons, just ||ke |n
6++.
.c f||es conta|n the actua| code.
Smain () |s ca||ed by 08
Smain can have arguments (more on th|s |ater}:
int main (int argc, char *argv[])
|n no argument correct form |s:
int main (void)
6omment your code:
on|y |* *| works
|| doesn't work |n 6
gcc accepts ||
ELE62041 |ec02-6-|anguage-| .1
8ae|d Nooshabad|
6 8yntax: 0ec|arat|ons
A|| dec|arat|ons must go at the beg|nn|ng of a
6 b|ock, before ass|gn|ng any va|ues.
Examp|es of |ncorrect dec|arat|ons:
int c = 0;
c = c + 1;
d = 23; /* error */
char d;
for (int i = 0; i < 10; i++)
ELE62041 |ec02-6-|anguage-| .17
8ae|d Nooshabad|
6 8yntax: 8tructs
6 uses struct |nstead of c|asses, but they're
very s|m||ar.
8amp|e dec|arat|on:
struct alpha {
int a;
char b;
};
To create an |nstance of th|s struct:
struct alpha inst1;
Read up on more struct spec|f|cs |n a 6
reference.
ELE62041 |ec02-6-|anguage-| .18
8ae|d Nooshabad|
True or Fa|se?
what eva|uates to FAL8E |n 6?
0 (|nteger}
NULL (po|nter: more on th|s |ater}
what eva|uates to TRUE |n 6?
everyth|ng e|se.
No such th|ng as a oo|ean type |n 6.
ELE62041 |ec02-6-|anguage-| .19
8ae|d Nooshabad|
Address vs. Va|ue (#1|3}
6ons|der memory to be a s|ng|e huge array:
Each ce|| of the array has an address assoc|ated
w|th |t.
Each ce|| a|so stores some va|ue.
0on't confuse the address referr|ng to a
memory |ocat|on w|th the va|ue stored |n that
|ocat|on.
23 12
... ...
101 102 103 101 105 ...
ELE62041 |ec02-6-|anguage-| .20
8ae|d Nooshabad|
Address vs. var|ab|e (#2|3}
21
5
32
90
80
81
88
92
ddresses
a
o
c
d
var|ao|es
var|ao|e ddress
var|ao|e va|ue
1 Word = 1 8yles = 32 o|ls
ELE62041 |ec02-6-|anguage-| .21
8ae|d Nooshabad|
Address vs var|ab|e (#3|3}
21
a
80
o 81
5
88
32
90
c
d 92
ddresses
var|ao|es
var|ao|e ddress
var|ao|e va|ue
81
plr 101
1 Word = 1 8yles = 32 o|ls
ELE62041 |ec02-6-|anguage-| .22
8ae|d Nooshabad|
Po|nters |n 6 (#1|7}
An address refers to a part|cu|ar memory |ocat|on. |n
other words, |t points to a memory |ocat|on.
Po|nter: h|gh Leve| Language (|n th|s case 6} way of
represent|ng a memory address.
23 12
... ...
101 102 103 101 105 ...
x
y
ocal|or (address)
rare
p
101
Hore spec|f|ca||y, a 6 var|ab|e can conta|n a po|nter to
someth|ng e|se. |t actua||y stores the memory address that
someth|ng e|se |s stored at.
ELE62041 |ec02-6-|anguage-| .23
8ae|d Nooshabad|
Po|nters |n 6 (#2|7}
why use po|nters?
|f we want to pass a huge struct or
array, |t's eas|er to pass a po|nter
than the who|e th|ng.
|n genera|, po|nters a||ow c|eaner,
more compact code.
8o what are the drawbacks?
Po|nters are probab|y the s|ng|e
|argest source of bugs |n
software, so be carefu| anyt|me
you dea| w|th them.
21
a
o 5
32
90
c
d
3lruc #1
15 a
o 55
11
88
c
d
3lruc #2
3lruc #r
80
9
ELE62041 |ec02-6-|anguage-| .24
8ae|d Nooshabad|
Po|nters |n 6 (#3|7}
how to create a po|nter:
operator: get address of a var|ab|e
int *p,
int x;
p ? x ?
x = 3;
p ? x 3
p = x;
p x 3
how get a va|ue po|nted to?
* dereference operator": get va|ue po|nted to
printf(p points to %d\n,*p);
ole lre ' gels used 2
d|llererl Ways |r lr|s
exarp|e. lr lre
dec|aral|or lo |rd|cale lral
p |s go|rg lo oe a po|rler,
ard |r lre printf lo gel
lre va|ue po|rled lo oy p.
ELE62041 |ec02-6-|anguage-| .25
8ae|d Nooshabad|
Po|nters |n 6 (#4|7}
how to change a var|ab|e po|nted to?
Use dereference * operator on |eft of =
p x 5
*p = 5
p x 3
ELE62041 |ec02-6-|anguage-| .2
8ae|d Nooshabad|
Po|nters |n 6 (#5|7}
To dec|are a po|nter, just precede
the var|ab|e name w|th a "*"
Examp|es:
int *a;
int b;
int *ptr, var, var2;
warn|ng: |n the th|rd examp|e
above, the var|ab|e ptr |s a
po|nter to an |nteger, wh||e var
and var2 are actua| |nteger
var|ab|es. The aster|sk on|y
app||es to one var|ab|e.
a
80
o 81
88 plr
var 92
var2
101
?
?
?
?
?
Po|rlers
1 Word = 1 8yles
ELE62041 |ec02-6-|anguage-| .27
8ae|d Nooshabad|
Po|nters |n 6 (#|7}
H|x|ng int and char
po|nters
Examp|es:
int *a;
char *b;
int *ptr, var;
char *chptr, ch;
a
80
o 81
88 plr
var 92
crplr
101
?
?
?
?
?
Po|rlers
1 Word = 1 8yles
cr 100,101,102, 103
?
|rl 3|ze: 1 Word = 1 8yles = 32 o|ls
crar 3|ze: 1 8yle = 8 o|ls
ELE62041 |ec02-6-|anguage-| .28
8ae|d Nooshabad|
Po|nters |n 6 (#7|7}
Not|ce that a po|nter can on|y po|nt to one type
(int, char, a struct, etc.}.
An int* cannot po|nt to a character, or v|ce
versa.
why not?
8afety: Po|nters are known to cause prob|ems to
care|ess programmers, so ||m|t what a po|nter can
do.
Svoid * |s a type that can po|nt to anyth|ng
(gener|c po|nter}
Use spar|ng|y to he|p avo|d program bugs!
ELE62041 |ec02-6-|anguage-| .29
8ae|d Nooshabad|
Arguments to Funct|ons
Arguments can be:
passed by va|ue: Hake a copy of the or|g|na| argument
(doesn't rea||y affect types such as |ntegers}.
int p
:
addOne (p)
passed by reference: Pass a po|nter, so the ca||ed funct|on
makes mod|f|cat|ons to the or|g|na| struct.
int *p
:
addOne (p)
Pass|ng by reference can be dangerous, so be carefu|.
ELE62041 |ec02-6-|anguage-| .30
8ae|d Nooshabad|
Po|nters and Pass|ng Arguments (#1|2}
Java and 6 pass an "by va|ue"
procedure|funct|on gets a copy of the parameter, so
chang|ng the copy cannot change the or|g|na|
void addOne (int x) {
x = x + 1;
}
int y = 3;
addOne(y);
Wy is stiII = 3
ELE62041 |ec02-6-|anguage-| .31
8ae|d Nooshabad|
Po|nters and Pass|ng Arguments(#2|2}
how to get a funct|on to change a va|ue?
void addOne (int *p) {
*p = *p + 1;
}
int y = 3;
addOne(y);
Wy is now = 4
ELE62041 |ec02-6-|anguage-| .32
8ae|d Nooshabad|
Arguments to Funct|ons: Examp|e
int a =1, b=1 *alpha = a, *beta = b;
void point_less (int a, int b) {
a = 0;
b = 0;
}
void point_full (int *a, int *b) {
*a = 0;
*b = 0;
}
point_less(a,b);
/* After calling point_less, and b are unchanged */
point_full(alpha,beta);
/* After calling point_full,a and b are changed */
1
1
80
81
80
81
88
92
9
o
a|pra
oela
a
1
1
0
0
0
0
80
81
ELE62041 |ec02-6-|anguage-| .33
8ae|d Nooshabad|
Peer |nstruct|on 0uest|on
void main(); {
int *p, x=5, y; // init
y = *(p = x) + 10;
int z;
flip-sign(p);
printf("x=%d,y=%d,p=%d\n",x,y,p);
}
flip-sign(int *n){*n = -(*n)}
how many errors?
rrors
1
2
3
4
5
6
7
8
9
(1)0
ELE62041 |ec02-6-|anguage-| .34
8ae|d Nooshabad|
Peer |nstruct|on Answer
void main(); {
int *p, x=5, y; // init
y = *(p = x) + 10;
int z;
flip-sign(p);
printf("x=%d,y=%d,p=%d\n",x,y,*p);
}
flip-sign(int *n){*n = -(*n);}
how many errors? | get 7.
rrors
1
2
3
4
5
6
7
8
9
(1)0
ELE62041 |ec02-6-|anguage-| .35
8ae|d Nooshabad|
References:
N|ck Par|ante: 8tanford 68 Educat|on L|brary
(http:||cs||brary.stanford.edu|}; A 6o||ect|on of very usefu| mater|a|
|nc|ud|ng:
Essent|a| 6: (http:||cs||brary.stanford.edu|101|} A re|at|ve|y qu|ck, 45
page d|scuss|on of most of the pract|ca| aspects of programm|ng |n
6.
|nky Po|nter V|deo: (http:||cs||brary.stanford.edu||104} 8|||y but
memorab|e 3 m|nute an|mated v|deo demonstrat|ng the bas|c
structure, techn|ques, and p|tfa||s of us|ng po|nters.
Po|nter as|cs: (http:||cs||brary.stanford.edu|10} The compan|on text
for the |nky v|deo.
Po|nters and Hemory: (http:||cs||brary.stanford.edu|102} A 31 page
exp|anat|on of everyth|ng you ever wanted to know about po|nters
and memory.
L|nked L|st as|cs: (http:||cs||brary.stanford.edu|103} A 2 page
|ntroduct|on to the techn|ques and code for bu||d|ng ||nked ||sts |n 6.
ELE62041 |ec02-6-|anguage-| .3
8ae|d Nooshabad|
Th|ngs to Remember
A|| dec|arat|ons go at the beg|nn|ng of each
funct|on.
0n|y 0 and NULL eva|uate to FAL8E.
A|| data |s |n memory. Each memory |ocat|on
has an address to use to refer to |t and a va|ue
stored |n |t.
A po|nter |s a h|gh Leve| Language vers|on of
the address.
* "fo||ows" a po|nter to |ts va|ue
& gets the address of a va|ue

Você também pode gostar