x1public class Foo {
2 static int a;
3 static int[] b;
4
5 public void foo() {
6 a=0;
7 b[a] =a ;
8 }
9
10}
xxxxxxxxxx
21int a;
2
Two crucial phases of Computation
Anything the compiler can compute is called static
Anything that can only be discovered when executing is called dynamic
The ALU is a combination circuit similar to the adder used in 121
In additions to the inputs and outputs of the adder, there is a third input used to select which function/operation to compute (could be +, *, etc)
Let's draw out the operations
Implements a set of instructions
Each instruction is implemented using logic gates
Say we propose an instruction that does:
Instruction parameters: addresses of
Every instruction is encoded as set of bits in memory:
add
)01|00001024|00001028|0000102c
operation|A|B|C
Accessing memory is slow
Big instructions are costly
Register file
Registers
Memory instructions
Other instructions access data in registers
01|00001024|00001028|0000102c
<- using memory
01|0|1|2
<- using register
ISA is a formal interface to a processor implementation
Types of instructions:
Register Transfer Language (RTL): simple, convenient pseudo language to describe semantics
Syntax:
LHS <- RHS
LHS
is memory or register that receives a valueRHS
is constant, memory, register or expression on two registersm[a]
is memory in address a
r[i]
is register with number i
Assume the value is stored at address 0x1234
What do the following instructions do?
r[0]
10
r[1]
0x1234
r[2]
m[r[1]]
//memory at register 1r[3]
r[0] + r[2]
m[r[1]]
r[2]
xxxxxxxxxx
71int a;
2int b[10];
3
4void foo() {
5 a = 0;
6 b[a] = a;
7}
Allocation is
Static vs. dynamic computation
Key observation:
a,b[0],b[1],…
are constants known to the compiler.Use RTL to specify the instructions needed for a = 0
r[0]
0x1000
m[r[0]]
0
Compiler doesn't know address of b[a]
Array access is computed from base and index
Similar to previous exercise, use RTL to specific the instructions for b[a] = a
r[0]
0x1000
r[1]
m[r0]
r[2]
0x2000
r[3]
r[2] + 0x4 * r[1]
m[r[3]]
r1
Generalizing and simplifying, we get:
r[x]
constantm[r[x]
r[y]
r[y]
m[r[x]
m[r[x] + r[y] * 4]
r[z]
r[z]
m[r[x] + r[y] * 4]
Architecture
Instruction format
Binary format represented as (each character is a hex digit):
x-01, xxsd, x0vv, v-sd vvvvvvvv
Where:
We have 4 addressing modes for operands:
PC (program counter): address of next instruction to fetch
Instruction: value of the current instruction
Separated into components:
Fetch stage:
Execute stage:
Internal registers:
General purpose registers:
Main memory:
xxxxxxxxxx
71public class Foo {
2 static int a;
3 static int[] b = new int[10];
4 public void foo () {
5 b[a] = a;
6 }
7}
xxxxxxxxxx
61int a;
2int* b;
3void food() {
4 b = malloc(10*sizeof(int));
5 b[a] = a;
6}
Arrays in Java
Store reference to array allocated dynamically with new
statement
int b[] = new int[10];
Arrays in C
Can store static arrays
int bst[10];
Can store points to other arrays
int *bptr = &bst[0]; // or int *bptr = bst;
Can store points to arrays allocated dynamically with call too malloc
library function
int *bdyn = malloc(10 * sizeof(int))
Terminology
Declaration
Allocation
malloc
allocates a block of bytes (no type, and no constructor)Bounds checking
Static allocation:
int b[10];
xxxxxxxxxx
410x2000: value of b[0]
20x2004: value of b[1]
3...
40x2024: value of b[9]
Dynamic allocation:
int *b = malloc(10 * sizeof(int));
xxxxxxxxxx
110x2000: 0x3000
When the program runs:
Static allocation:
int b[10]
xxxxxxxxxx
41 0x2000: value of b[0]
2 0x2004: value of b[1]
3 ...
4 0x2024: value of b[9]
Dynamic allocation:
int *b = malloc(10 * sizeof(int));
Pointers are declared as:
type *varname
Pointers may be accessed as:
varname[0] or *varname
varname[i] or *(varname + i)
The address of a variable can be obtained with:
xxxxxxxxxx
21type a;
2type *ptr = &a;
variable | address | value |
---|---|---|
a | 0x1000 | 3 |
ptr: | 0x2000 | 0x1000 |
xxxxxxxxxx
51 a == 3;
2 &a == 0x1000;
3 ptr == 0x1000;
4 &ptr == 0x2000;
5 *ptr == 3;
*
and &
example xxxxxxxxxx
131int a;
2int b;
3int *c;
4
5void foo() {
6 a = 4;
7 b = 7;
8 c = &a;
9
10 *c = 5;
11 b = 2;
12 c = &b;
13}
In both languages:
In C
m[b + x * sizeof(array-element)] <- 0
m[m[b] + x * sizeof(array-element)] <- 0
Adding an integer to a pointer;
a
is a pointer of type int, then (a+i) is equivalent to 4*i bytes ahead of a.Subtracting two pointers of the same type
&a[7] - &a[2] == 5