2010SuCS61C-L15-eric..

advertisement
inst.eecs.berkeley.edu/~cs61c
CS61C : Machine Structures
Lecture 15 – Review
2010-07-15
Instructor Eric Chang
CS61C Review (1)
Chang, Summer 2010 © UCB
Numbers: positional notation
• Number Base B  B symbols per digit:
• Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Base 2 (Binary):
0, 1
• Number representation:
• d31d30 ... d1d0 is a 32 digit number
• value = d31  B31 + d30  B30 + ... + d1  B1 + d0  B0
• Binary:
0,1 (In binary digits called “bits”)
• 0b11010 = 124 + 123 + 022 + 121 + 020
= 16 + 8 + 2
#s often written = 26
0b…
Here 5 digit binary # turns into a 2 digit decimal #
• Can we find a base that converts to binary easily?
•
CS61C Review (2)
Yes! Hexadecimal and Octal.
Chang, Summer 2010 © UCB
Integer representation schemes
• Unsigned – ignore negative numbers.
• Sign and Magnitude – most significant bit denotes
sign; 0 is positive, 1 is negative.
• One’s Complement – Flip all the bits to denote
negative number. 0 in the most significant bit
denotes positive number (not useful, why?)
• Two’s Complement – One’s complement then add 1
(standard representation).
• Bias – Pick a bias, and interpret numbers as
unsigned numbers subtracted by the bias (used in
floating point exponent).
• Representation consideration: hardware
implementation, odometer, duplicate
representations for certain numbers?
CS61C Review (3)
Chang, Summer 2010 © UCB
Two’s Complement Formula
• Can represent positive and negative numbers
in terms of the bit value times a power of 2:
d31 x -(231) + d30 x 230 + ... + d2 x 22 + d1 x 21 + d0 x 20
• Example: 1101two in a nibble?
= 1x-(23) + 1x22 + 0x21 + 1x20
= -23 + 22 + 0 + 20
= -8 + 4 + 0 + 1
= -8 + 5
= -3ten
CS61C Review (4)
Example: -3 to +3 to -3
(again, in a nibble):
x : 1101two
x’: 0010two
+1: 0011two
()’: 1100two
+1: 1101two
Chang, Summer 2010 © UCB
Bit Manipulation
• C gives us couple operators to let us
manipulate bit by bit.
• >>, <<, |, &, ^, ~, etc.
• Normally use a “mask” (a number with
a 1 in a specific bit) to access (with &),
set to 1 (with |), or flip (with ^) the
specified bit of a number.
• x ^= 1<<5 will flip the 5th bit of x.
• Incidentally, multiplying, dividing, and
modding by powers of 2 can be done
more efficiently with bitwise
operations.
CS61C Review (5)
Chang, Summer 2010 © UCB
Pointers
• An address refers to a particular
memory location. In other words, it
points to a memory location.
• Pointer: A variable that contains the
address of a variable.
Location (address)
...
101 102 103 104 105 ...
23
42
104
x
y
p
...
name
CS61C Review (6)
Chang, Summer 2010 © UCB
Pointers
• How to create a pointer:
& operator: get address of a variable
int *p;
int x;
x = 3;
p =&x;
p
?
x
?
p
?
x
3
x
3
p
Note the “*” gets used
2 different ways in
this example. In the
declaration to indicate
that p is going to be a
pointer, and in the
printf to get the
value pointed to by p.
• How get a value pointed to?
* “dereference operator”: get value pointed to
use on left side of = to assign value to address
pointed to
printf(“p points to %d\n”,*p);
CS61C Review (7)
Chang, Summer 2010 © UCB
Pointers and Parameter Passing
• Java and C pass parameters “by value”
• procedure/function/method gets a copy of the
parameter, so changing the copy cannot
change the original
• If you need to modify parameter, pass an
address (pointer).
address
101 102 103 104 105 ...
void addOne (int x) {
...
3
x = x + 1;
y
}
4
x
name
int y = 3;
printf(“What is y? %d\n”, y);
addOne(y);
CS61C Review (8)
y is still = 3
Chang, Summer 2010 © UCB
Arrays
• Key Concept: An array variable is a fixed
“pointer” to the first element.
• Differences:
• Cannot re-assign arrays.
• Sizeof(array) returns number of bytes total in
the array, sizeof(pointer) returns number of
bytes in address (4).
 Reason: sizeof is a function done by the compiler.
• Multidimensional arrays occupy consecutive
blocks in memory, pointer of pointers can point
to discrete blocks in memory (affects parameter
passing).
• Coding Advice: only use arrays for
temporary memory storage (memory you
don’t need after function finish).
CS61C Review (9)
Chang, Summer 2010 © UCB
Arrays (cont.)
• Consequences:
•arr is an array variable but looks like a pointer
in many respects (though not all)
•arr[0] is the same as *arr
•arr[2] is the same as *(arr+2)
• In fact, you can also write 2[arr]
• We can use pointer arithmetic to access arrays
more conveniently.
• Declared arrays are only allocated
while the scope is valid
int *foo() {
int arr[32]; ...;
return arr;
} is incorrect
CS61C Review (10)
Chang, Summer 2010 © UCB
C Strings
• A string in C is just an array of
characters (which always ends in \0).
char str[] = "abc"; // 4 elements
• Use functions such as strlen,
strncmp, strncpy in string.h to
manipulate strings.
• Since strings are just an array of
characters, use the same judgment as
before to decide whether to store it in
char[] or char *.
CS61C Review (11)
Chang, Summer 2010 © UCB
Malloc and Free
• To allocate room for something new to
point to, use malloc() (with the help of a
typecast and sizeof):
ptr = (int *) malloc (n*sizeof(int));
• This allocates an array of n integers.
• After dynamically allocating space, we
must dynamically free it:
free(ptr);
Every malloc’d memory MUST be freed!
CS61C Review (12)
Chang, Summer 2010 © UCB
Check malloc’s return value!
• malloc() can fail!
• Requested something too large
• Out of memory!
• Random OS Error
• If malloc fails, NULL is returned
• Every time you call malloc(), you MUST check it’s return
value against NULL. NO EXCEPTIONS!
• Note: I said in my sections that assert() can be used
for quick error checking. However, it is a very bad
practice to not make a program gracefully exit when it
should (who wants their game to crash all the time when
you can fix it by using a simple malloc check?)
assert() should be used ONLY during development
and not in actual code (i.e. your exam).
CS61C Review (13)
Chang, Summer 2010 © UCB
C structures : Overview
• A struct is a data structure
composed from simpler data types.
struct point {
int x;
int y;
};
/* type definition */
As always in C, the argument is passed by “value” – a copy is made.
void PrintPoint(struct point p)
{
printf(“(%d,%d)”, p.x, p.y);
}
struct point p1 = {0,10}; /* x=0, y=10 */
PrintPoint(p1);
• Usually, more efficient to pass a
pointer to the struct.
• ptr->y is shorthand for (*ptr).y
CS61C Review (14)
Chang, Summer 2010 © UCB
Pointer Exercise
typedef struct vector
{
int *array;
int array_size;
} vector_t;
If &g = 20 and
g.rows[0]->array[0] = 1, what is
the value of X??
typedef struct grid {
vector_t **rows;
int rows_size;
} grid_t;
a) 0
b) 1
c) 4
d) 16
e) No idea
grid_t g;
// codes not shown …
Address:
4
8
12
16
20
24
28
32
X
1
0
1
28
1
4
4
CS61C Review (15)
Chang, Summer 2010 © UCB
Answer
typedef struct vector
{
int *array;
int array_size;
} vector_t;
If &g = 20 and
g.rows[0]->array[0] = 1, what is
the value of X??
Red: grid_t
Blue: vector_t *rows
Green: vector_t
Purple: int *array
a) 0
b) 1
c) 4
d) 16
e) No idea
typedef struct grid {
vector_t **rows;
int rows_size;
} grid_t;
grid_t g;
// codes not shown …
Address:
4
8
12
16
20
24
28
32
X
1
0
1
28
1
4
4
CS61C Review (16)
Chang, Summer 2010 © UCB
Normal C Memory Management
• A program’s address
space contains 4 regions:
~ FFFF FFFFhex
stack
• stack: local variables,
grows downward
• heap: space requested for
pointers via malloc() ;
resizes dynamically,
grows upward
• static data: variables
declared outside main,
does not grow or shrink ~ 0
heap
static data
code
hex
• code: loaded when
program starts,
does not change
CS61C Review (17)
For now, OS somehow
prevents accesses between
stack and heap (gray hash
lines). Wait for virtual memory
Chang, Summer 2010 © UCB
Stack
• Last In, First Out (LIFO) data structure
stack
main ()
{ a(0);
}
void a (int m)
{ b(1);
}
void b (int n)
{ c(2);
}
void c (int o)
{ d(3);
}
void d (int p)
{
}
CS61C Review (18)
Stack
Stack Pointer
grows
down
Stack Pointer
Stack Pointer
Stack Pointer
Stack Pointer
Chang, Summer 2010 © UCB
Heap Memory Management
• For small amounts of memory request,
generally use slab allocator/buddy system
• For larger amounts of memory, use a free
list with one of the three following policy for
traversal:
• First fit (return first block of free space found)
• Next fit (keep track of where we left off next time
instead of starting over every time)
• Best fit (find the block that fits best).
Also, automatic memory management exists
for strongly typed languages (not C).
Examples are Mark and Sweep and Garbage
Collection.
CS61C Review (19)
Chang, Summer 2010 © UCB
MIPS Assembly Language
• Deals primarily with typeless registers
(instead of memory) to perform tasks.
• Regular instructions that manipulates
registers (add, addiu, slt, lui, ori, etc.)
•sltiu $t0, $a0, 5
• Instructions that manipulates memory (lw,
sw).
•sw $t0 4($sp)
• Equivalent to dereferencing and modifying
pointers.
• Instructions that control data flow (j, jr,
beq).
•beq $t0 $t1 label
CS61C Review (20)
Chang, Summer 2010 © UCB
MIPS Signed vs. Unsigned – diff meanin
• MIPS terms Signed/Unsigned
“overloaded”:
• Do/Don't sign extend
 (lb, lbu)
• Do/Don't overflow
 (add, addi, sub, mult, div)
 (addu, addiu, subu, multu, divu)
• Do signed/unsigned compare
 (slt, slti/sltu, sltiu)
CS61C Review (21)
Chang, Summer 2010 © UCB
MIPS Registers
The constant 0
Reserved for Assembler
Return Values
Arguments
Temporary
Saved
More Temporary
Used by Kernel
Global Pointer
Stack Pointer
Frame Pointer
Return Address
$0
$1
$2-$3
$4-$7
$8-$15
$16-$23
$24-$25
$26-27
$28
$29
$30
$31
$zero
$at
$v0-$v1
$a0-$a3
$t0-$t7
$s0-$s7
$t8-$t9
$k0-$k1
$gp
$sp
$fp
$ra
Except for save registers, $0, and stack pointer, all
other registers are volatile (might change after function
call) and must be SAVED!!!
CS61C Review (22)
Chang, Summer 2010 © UCB
MIPS Function Layout
Prologue: FunctionFoo:
addiu $sp, $sp, -FrameSize
#reserve space on
#the stack
sw $s0, 0($sp) #store needed
#registers
sw $s1, 4($sp)
… save the rest of the registers …
sw $ra, (Framesize – 4)($sp)
… Do some stuff …
jal something
… Do some stuff …
Body:
Epilogue:
lw $s0, 0($sp) #restore registers
… load the rest of the registers…
lw $s1, 4($sp)
lw $ra, (Framesize – 4)($sp)
addiu $sp, $sp, FrameSize #release stack
#spaces
jr $ra #return to normal execution
CS61C Review (23)
Chang, Summer 2010 © UCB
MIPS Exercise
Translate the following to MIPS:
void insertionSort(int *arr, int size){
int i, j;
for(i=1; i<size; i++){
j=i;
while(j>0 && arr[j]<arr[j-1]){
swap(arr + j, arr + (j-1));
j--;
}
}
}
CS61C Review (24)
Chang, Summer 2010 © UCB
Instruction Format
R opcode
I opcode
J opcode
rs
rs
rt
rd shamt funct
rt
immediate
target address
• For I-instruction, immediate is sign extended to
32 bits for HW simplicity.
•
•
Some I-instructions suffers from sign extension, so
assembler must recompile with lui/ori to compensate.
Branch stores the different (PC-relative addressing)
between the current address and the target address
(divided by 4) in the immediate field.
• J-instruction borrow the upper 4 bits of PC and
append them to the target address multiplied by
4.
CS61C Review (25)
Chang, Summer 2010 © UCB
MAL v.s. TAL
• Since it is usually quite hard to
convert directly from MAL to binary
format, we convert MAL to TAL first,
then convert from TAL to binary.
• TAL is a subset of MAL
• Need to convert to TAL when:
• Using psuedo-instructions.
• Doesn’t fit into Immediate field.
• When converting to TAL, make sure to
not change ANY registers besides
$at!!!!
CS61C Review (26)
Chang, Summer 2010 © UCB
Floating Point Representation
• Normalized format:
+1.xxx…xtwo*2yyy…ytwo
• Multiple of Word Size (32 bits)
31 30
23 22
S Exponent
0
Significand
1 bit 8 bits
23 bits
• S represents Sign
Exponent represents y’s
Significand represents x’s
• Exponent Bias = 2^(E - 1) – 1
• Denorm Imp. Exp. = - (Exp. Bias – 1)
• Why this representation? Nice ordering.
CS61C Review (27)
Chang, Summer 2010 © UCB
Denorms (1/2)
• Problem: There’s a gap among
representable FP numbers around 0
• Smallest representable pos num:
a = 1.000…0 2 * 2-126 = 2-126
• Second smallest representable pos num:
b = 1.000……1 2 * 2-126
= (1 + 0.00…12) * 2-126
= (1 + 2-23) * 2-126
= 2-126 + 2-149
Normalization and
implicit 1
is to blame!
a - 0 = 2-126
b - a = 2-149
CS61C Review (28)
Gaps!
b
0 a
+
Chang, Summer 2010 © UCB
Denorms (2/2)
• Solution:
• We still haven’t used Exponent=0,
Significand nonzero
• Denormalized number: no (implied)
leading 1, implicit exponent = -126
(-1)S x (0 . Significand) x 2(-De. Imp. Exp.)
• Smallest representable pos num:
 A = 2-149
• Second smallest representable pos num:
 b = 2--148
CS61C Review (29)
0
+
Chang, Summer 2010 © UCB
Special Numbers Summary
• Reserve exponents, significands:
Exponent
0
Significand
0
Object
+/-0
0
nonzero
Denorm
1-254
Anything
+/- fl. Pt #
255
0
+/- ∞
255
nonzero
NaN
CS61C Review (30)
Chang, Summer 2010 © UCB
Download