Professional Documents
Culture Documents
ECE 150, Section 001, Fall 2017 Leftovers
ECE 150, Section 001, Fall 2017 Leftovers
Leftovers
1 / 44
Selection: Ternery and Switch
?:
switch
Implementation
2 / 44
Selection: Ternery and Switch
3 / 44
Selection: Ternery and Switch
?:
switch
Implementation
4 / 44
Ternary Selection
5 / 44
Selection: Ternery and Switch
?:
switch
Implementation
6 / 44
else if else if else if . . .
I What if we want x == A or x == B or x == C?
if (x == A) {
...
} else if (x == B) {
...
} else if (x == C) {
...
}
7 / 44
switch () { . . . }
switch (expression) {
case A:
...
break;
case B:
...
break;
...
case Z:
...
break;
default:
...
break;
}
I Question: Why “break” after “default”?
8 / 44
switch () { . . . }
switch (expression) {
2017-10-26
...
break;
case B:
switch ...
break;
...
switch () { . . . } case Z:
...
break;
default:
...
break;
}
I Question: Why “break” after “default”?
switch (inputChar) {
case ’a’:
case ’A’:
cout << "A is for APL and Ada and ALGOL" << endl;
break;
...
default:
cout << "Try entering a letter next time ..." << endl;
break;
}
9 / 44
Selection: Ternery and Switch
?:
switch
Implementation
10 / 44
Inefficient Implementation
I Convert to “if else if else if ... ”
switch(X) { if ((X) == A) {
case A: code for A;
code for A; }
break; else if ((X) == B) {
case B: code for B;
code for B; }
break; ...
... else {
default: code for default;
code for default; }
break;
}
I Question: Why is this inefficient?
11 / 44
Inefficient Implementation
I Convert to “if else if else if ... ”
switch(X) { if ((X) == A) {
switch case A:
code for A;
break;
}
code for A;
else if ((X) == B) {
case B: code for B;
Inefficient Implementation code for B;
break;
...
}
...
else {
default: code for default;
code for default; }
break;
}
I Question: Why is this inefficient?
13 / 44
Assembly Code for Dense Target Example
...
Instruction Space 602 Code for Case 62
Address Code ...
... 616 JMP 627
429 LD 902, R1 617 Code for Case 63
430 JLZ R1,623 ...
431 SUBi R1,63,R2 622 JMP 627
432 JGZ R2,623 623 Code for default case
433 LDi 913, R2 ...
434 ADD R2,R1,R2 626 JMP 627
435 LD R2,R3 627 Code after switch
436 JMP R3 ...
437 Code for Case 0 Data Space
... Address Data
463 JMP 627
...
464 Code for Case 1 902 -
... ...
479 JMP 627 913 437
480 Code for Case 2 914 464
... 915 480
496 JMP 627 ...
... 975 602
976 617
14 / 44
Assembly Code for Dense Target Example
...
Instruction Space 602 Code for Case 62
2017-10-26
... 616 JMP 627
429 LD 902, R1 617 Code for Case 63
430 JLZ R1,623 ...
431 SUBi R1,63,R2 622 JMP 627
626
627
Code for default case
...
JMP 627
Code after switch
• LDi <value>, Ri: Load immediate: rather than treating the “value” as a
memory address, simply treat it as a value to be loaded into Ri.
• LDi <value>, Ri: Ri ← <value>
• LD Ri, Rj: Load indirect: rather than load from a specified memory
address, consider the contents of a register as a memory address and load
from that location
• LD Ri,Rj: Rj ← data at memory location contained in Ri
• JMPi: Jump indirect: rather than jump to a specified memory address,
consider the contents of a register as memory address and jump to that
location
• JMPi Ri: PC ← data in Ri
• Memory location of “x” is 902
• Memory location of jump table is 902–976
•
• This pseudo-assembly code is to give you a sense of what’s going on
• You are not expected to understand it in depth at this stage of the course.
Efficient Implementation 2: Sparse Target
15 / 44
Hash Functions
In the sparse case, the compiler will hash the switch expression to
a significally smaller range, which will then be looked up with a
jump table.
16 / 44
Hash Functions
Hash Functions In the sparse case, the compiler will hash the switch expression to
a significally smaller range, which will then be looked up with a
jump table.
• Hash(x) maps to the jump table, but the jump table must include
not only the jump address, but what value of “x” for which it
contains the address, since there will be many input “x” values that
are not valid cases.
• Hash collisions must also be accounted for.
• The concept of hashing will come up again somewhat in this course,
a lot in ECE 250, Algorithms and Data Structures, and is very
widely used in programming.
• Hashing is also used in other ECE areas. For example, various
security protocols use special cryptographic hash functions.
Selection: Ternery and Switch
?:
switch
Implementation
17 / 44
Selection: Ternery and Switch
?:
switch
Implementation
18 / 44
for( ; ; ): Formalization of common usage pattern
Let’s take a closer look at a common use of while():
initialize some variables
while (test condition) {
do stuff;
do more stuff;
...
change values that might affect the test condition;
}
for(;;) is a syntactic variant of while() that formalizes
I initialization
I change
20 / 44
Translating for(;;) to while()
A;
for(A;B;C) { while(B) {
body body
of of
loop loop
} C;
}
21 / 44
for( ; ; ) — Important Nuance I
i++;
}
22 / 44
for( ; ; ) — Important Nuance II
23 / 44
Selection: Ternery and Switch
?:
switch
Implementation
24 / 44
do { } while()
do {
body
of
loop
} while (condition);
Example: Leftovers-keyboard.cpp
25 / 44
Translate “do { } while()” to “while()”
int firstTime = 1;
do { while (firstTime || condition) {
body body
of of
loop loop
} while (condition); firstTime = 0;
}
26 / 44
What if I want to exit in the middle of a loop?
DON’T DO THIS!
27 / 44
Selection: Ternery and Switch
28 / 44
Selection: Ternery and Switch
?:
switch
Implementation
29 / 44
String: Array of char
I String Literal is a null-terminated array of char.
s1[0] = ’a’;
s1[1] = ’b’;
s1[2] = ’c’;
s1[3] = (char)0;
cout << "s1: " << s1 << "\ns2: " << s2 << endl;
See Leftovers-simplestring.cpp
30 / 44
Selection: Ternery and Switch
?:
switch
Implementation
31 / 44
Multi-Dimensional Arrays
See Leftovers-twodimensional.cpp
32 / 44
Command-line Input
See Leftovers-commandlinearguments.cpp
33 / 44
Selection: Ternery and Switch
?:
switch
Implementation
34 / 44
Array as an Address
Recall, from Leftovers-arrayOps.cpp:
int a[5];
...
cout << "Element 0: " << a[0];
cout << "Element 0: " << *a;
...
cout << "Element 3: " << a[3];
cout << "Element 3: " << *(a+3);
I Question: what is a?
I Is it a variable?
I Does it contain a value?
I If so, is that value changable?
I What kind of value?
I Answer: an address or a reference.
I A value that identifies a location in memory.
35 / 44
Syntatic Details
See Leftovers-addressGames.cpp
36 / 44
Explanation via Memory-Layout
1: unsigned x;
...
2: cout << x;
3: cout << &x;
4: cout << *x;
5: cout << **x;
37 / 44
Explanation via Memory-Layout
1: unsigned x;
38 / 44
Relationship to Arrays
39 / 44
Dynamic Memory-Allocation I
I Two ways of allocation memory “dynamically.”
I Way 1:
I Used to be illegal.
I Supported by more recent versions of compiler.
I Does not allow initialization.
Won’t work
40 / 44
Dynamic Memory-Allocation II
I Way 2:
in size = 10;
char *a; // A pointer to char
a = new char[size];
41 / 44
Example – Allocated vs. not
unsigned *a;
cout << a[5]; // ILLEGAL access!
// May crash program
a = new unsigned[10];
cout << a[5]; // legal access
See Leftovers-dynamicmemoryallocation.cpp
42 / 44
Command-Line Arguments, Revisited
43 / 44
Image Credits. . .
To come. . .
44 / 44