Computer Science, Computer Architecture
Computer Science, Computer Architecture
C Programming
Alvin R. Lebeck
Some slides based on those from Daniel Sorin,
Andrew Hilton, Amir Roth, Gershon Kedem
Administrivia
Homework #1
No class Monday
Today
! Finish Data Representations
! C programming & Memory
Alvin R. Lebeck
From Sorin, Hilton, Roth
Review: Answer
What floating-point number is
0xC1580000?
1100 0001 0101 1000 0000 0000 0000 0000
31 30
23 22
Alvin R. Lebeck
From Sorin, Hilton, Roth
Exponent = 0 is denormalized
! Implicit 0. instead of 1. in mantissa
! Allows 0000.0000 to be 0
! Helps with very small numbers near 0
Alvin R. Lebeck
From Sorin, Hilton, Roth
1/0 = +
-1/0 = -
! If non zero mantissa: Not a Number (NaN)
sqrt(-42) = NaN
Alvin R. Lebeck
From Sorin, Hilton, Roth
FP Addition/Substraction
Addition/Subtraction
! Align the exponents
! Perform operation
! Renormalize the result (put into 1.M x 2E-127 form)
Alvin R. Lebeck
From Sorin, Hilton, Roth
x = 99.96 x 103
x = x + 0.07
x = 100.03 x 103
x = 10.00 x 104
Exp
11-bit
Alvin R. Lebeck
From Sorin, Hilton, Roth
Mantissa
52 - bit
Oct. Char
000
010
020
030
040
050
060
070
100
110
120
130
140
150
160
170
nul
bs
dle
can
sp
(
0
8
@
H
P
X
`
h
p
x
soh
ht
dc1
em
!
)
1
9
A
I
Q
Y
a
i
q
y
002
012
022
032
042
052
062
072
102
112
122
132
142
250
162
172
stx
nl
dc2
sub
"
*
2
:
B
J
R
Z
b
j
r
z
003
013
023
033
043
053
063
073
103
113
123
133
143
153
163
173
etx
vt
dc3
esc
#
+
3
;
C
K
S
[
c
k
s
{
004
014
024
034
044
054
064
074
104
114
124
134
144
154
164
174
eot
np
dc4
fs
$
,
4
<
D
L
T
\
d
l
t
|
005
015
025
035
045
055
065
075
105
115
125
135
145
155
165
175
enq
cr
nak
gs
%
5
=
E
M
U
]
e
m
u
}
006
016
026
036
046
056
066
076
106
116
126
136
146
156
166
176
ack
so
syn
rs
&
.
6
>
F
N
V
^
f
n
v
~
007
017
027
037
047
057
067
077
107
117
127
137
147
157
167
177
bel
si
etb
us
'
/
7
?
G
O
W
_
g
o
w
del
Unicode
Many types
UTF-8: variable length encoding backward compatible
with ASCII
! Linux
Alvin R. Lebeck
From Sorin, Hilton, Roth
10
Alvin R. Lebeck
From Sorin, Hilton, Roth
11
Alvin R. Lebeck
From Sorin, Hilton, Roth
12
Alvin R. Lebeck
From Sorin, Hilton, Roth
13
Closer to hardware
! Easier to make mistakes
! Can often result in faster code
Alvin R. Lebeck
From Sorin, Hilton, Roth
14
Alvin R. Lebeck
From Sorin, Hilton, Roth
15
Alvin R. Lebeck
From Sorin, Hilton, Roth
16
Debugging
Print debugging
! Just output information at different points in the program
! Not the most efficient, but often works.
Alvin R. Lebeck
From Sorin, Hilton, Roth
17
Constants
! Use #define C preprocessor
! E.g.,: #define MAX_SCORE 100
Operators:
! Mathematical +, -, *, /, %,
! Logical !, &&, ||, ==, !=, <, >, <=, >=
! Bitwise &, |, ~, ^ , <<, >> (well get to these next)
18
Per bit
0&0=0
0|0=0
0^0=0
0&1=0
0|1=1
0^1=1
1&0=0
1|0=1
1^0=1
1&1=1
1|1=1
1^1=0
For multiple bits, apply operation to individual bits in same position
AND
011010
101110
001010
Alvin R. Lebeck
From Sorin, Hilton, Roth
OR
011010
101110
111110
XOR
011010
101110
110100
19
Alvin R. Lebeck
From Sorin, Hilton, Roth
20
C.
xpos = val & 0x000ff;
ypos = (val & 0x0ff00) << 8;
button = (val & 0x30000) << 16;
Alvin R. Lebeck
From Sorin, Hilton, Roth
B.
xpos = val & 0x000ff;
ypos = (val & 0x0ff00) >> 8;
button = (val & 0x30000) >> 16;
D.
xpos = val & 0x000ff;
ypos = val & 0x0ff00 >> 1;
button = val & 0x30000 >> 2;
21
Alvin R. Lebeck
From Sorin, Hilton, Roth
B.
xpos = val & 0x000ff;
ypos = (val & 0x0ff00) >> 8;
button = (val & 0x30000) >> 16;
D.
xpos = val & 0x000ff;
ypos = val & 0x0ff00 >> 1;
button = val & 0x30000 >> 2;
Precedenceshifts mask first; shift
amount wrong
22
Functions
Encapsulate computation
! Reuse or clarity of code
! Cannot define functions within functions
23
Global Variables
Global variables: Accessible from any function
#include <stdio.h>
float f = 0;
void bar () {
f = 0.5;
}
int main()
{
f =0.31234;
bar();
printf(The value is %f \n, f);
}
Output is:
The value is 0.5
Alvin R. Lebeck
From Sorin, Hilton, Roth
24
Memory Partitions
2n-1
Stack
Typical
Address
Space
Data
! static (constants, global variables)
! dynamic (heap, malloc / new
allocated)
! grows up
Heap
Stack
Data
! local variables
! grows down
Text
Reserved
! int x;
Alvin R. Lebeck
From Sorin, Hilton, Roth
25
2n-1
x 0x400
Heap
result 0x208
Data
3
Text
0
Alvin R. Lebeck
From Sorin, Hilton, Roth
Stack
Reserved
26
Alvin R. Lebeck
From Sorin, Hilton, Roth
a = 78, f = 0.5
a = 78, f = 0.31234
a = 23, f = 0.5
a = 23, f = 0.31234
27
Alvin R. Lebeck
From Sorin, Hilton, Roth
2n-1
a 0xF0020
f 0x F0000
Stack
Heap
f 0x20004
a 0x 20000
Data
Text
Reserved
28
C
! A pointer is a variable that contains the location of another
variable
! A pointer is a memory location that contains the address of
another memory location
! Can manipulate the value of pointer (double edge sword)
Alvin R. Lebeck
From Sorin, Hilton, Roth
29
Pointers
Declaration of pointer variables
! int *x_ptr; char *c_ptr; void *ptr;
x_ptr = &intvar;
Alvin R. Lebeck
From Sorin, Hilton, Roth
30
Pointers
address of operator &
! don t confuse with bitwise AND operator
Given
int x; int* p; // p points to an int
p = &x;
Then
*p = 2; and x = 2; produce the same result
Note: p is a pointer, *p is an int
x 0x26cf0
...
p 0x26d00 0x26cf0
Alvin R. Lebeck
From Sorin, Hilton, Roth
31
Pointers
De-reference using *ptr to get what is pointed at
statement
x_ptr
int x;
??
??
int *x_ptr;
??
??
x=2
??
x_ptr = &x;
&x
*x_ptr = 68;
68
&x
x_ptr = 200;
68
200
*x_ptr = 42
68
200
32
A.
Alvin R. Lebeck
From Sorin, Hilton, Roth
B.
33
Strings as Arrays
s t
0 1
r i
15 16
g \0
42 43
Alvin R. Lebeck
From Sorin, Hilton, Roth
34
Structures
Loosely like objects
! Have member variables
! Do not have methods!
Alvin R. Lebeck
From Sorin, Hilton, Roth
35
Summary
C is different from Java
! Procedural
! Structures not objects
! Pointers!
Next time
! Explicit memory deallocation
! Pointer manipulation
! Linked Structures
Alvin R. Lebeck
From Sorin, Hilton, Roth
36