Design Goals:
Minimize the number of memory accesses
Minimize the instruction size
Variables:
Always static:
Always dynamic: Depends:
Register Transfer Language (RTL)
The pseudo language we will use to describe instruct semantics
Syntax
Each line is in the form:
Value in memory at address a: Register number i:
Examples:
Assume the variable a is stored at address 0x1234 with value 7.
Write comments beside each line to describe the following sequence of instructions:
r[0] <- 10
r[1] <- 0x1234
r[2] <- m[r[1]]
r[2] <- r[0] + r[2]
m[r[1]] <- r[2]
Putting it all together:
Remember that array access is computed from base and index. Write out the ASSM and Machine Instructions for each RTL line
The Simple Machine (SM213) memory access instructions:
SM213 arithmetic instructions:
SM213 logical instructions (i.e., shifting), NOP, and halt:
Internal state:
pc: instruction:
Cycle stages:
Declaration of variables:
int a=5; Declare an ________ variable, value: ___________ int* b=&a; Declare a ________ variable, value: ___________
Dynamic allocation
malloc: sizeof:
Static:
The variable b has a static address, but a dynamic value. The value of b is an address (that’s why its type is int*)
Dynamic:
Dereferencing
&x: x: *x:
Code and Memory tracing examples:
Syntax for accessing an element of a static array is the same as for a dynamic array.
Code:
int a; int* b;
a = 5; b = &a;
Address 0x1000
... 0x2000
Address 0x1000 ...
0x2000 0x2004 0x2008
... 0x3000
Value
a
b
Value
a
b[0] b[1] ...
c
Which implementation is faster? Why?
Tradeoffs
Static array benefits:
Dynamic array benefits:
Pointer arithmetic
Adding x to a pointer of type Y*, adds (x * sizeof(Y)) to the pointers memory address value.
From example above:
printf(“%d\n”, *(c+4));
c = c+2;
printf(“%d\n”, *c);
Output:
Output:
a:
&a:
b:
*b:
&b:
Note: b is allocated statically
The array is allocated dynamically, and then the address of the dynamically allocated memory is assigned to b.
Code:
int a;
int b[10];
int* c= &a;
a = 8;
b[0] = a-2;
c = &b;
Output:
a:
&a:
b[0]:
&b[0]:
c:
&c:
*c: