2020 Winter Term 2 Unit 1b - Static Scalars and Arrays: CPSC 213 2020 ST1 © 2020 Jonatan Schroeder
2020 Winter Term 2 Unit 1b - Static Scalars and Arrays: CPSC 213 2020 ST1 © 2020 Jonatan Schroeder
Human
Creation Compilation Execution
Source Object Dynamic
Idea
Code Code State
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 7
▪ Execution
▪ Parameterized by input values unknown at compilation
▪ Producing output values that are unknowable at compilation
▪ Anything the compiler can compute is called static
▪ Anything that can only be discovered when executing is
called dynamic
Human
Creation Compilation Execution
Source Object Dynamic
Idea
Code Code State
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 8
Consider the code below:
int a;
a = 5;
▪ Registers
▪ Each register named by a number (e.g., 0-7)
▪ Size: architecture’s common integer (32 or 64 bits)
Regs
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 14
▪ Memory instructions handle only memory
▪ Load data from memory into register (slow)
▪ Store data from register into memory (slow)
Regs
01 0 1 2 01 0 1
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 15
▪ A special-purpose register can only be used for certain
purposes
▪ May not be accessible by all instructions (e.g., can’t be used as
argument for an add instruction)
▪ May have special meaning or be treated specially by CPU
▪ Examples:
▪ PC (Program Counter): address of the next instruction to execute
▪ IR (Instruction Register): instruction that has just been loaded from
memory
▪ Types of instructions:
▪ math and logic
▪ memory access
▪ control transfer: “gotos” and conditional “gotos”
▪ Instruction format
▪ sequence of bits: opcode and operand values
▪ all represented as numbers
▪ Assembly language:
▪ Symbolic (textual) representation of machine code
▪ Syntax:
▪ each line is of the form: LHS ← RHS
▪ LHS is memory or register that receives a value
▪ RHS is constant, memory, register or expression on two registers
▪ m[a] is memory in address a
▪ r[i] is register with number i
▪ Allocation is
▪ assigning a memory location to store a specified value (e.g., variable)
▪ associating the variable to the address of that memory location (its name
for reading and writing)
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 24
int a; Static Memory Layout
int b[10];
int a; 0x1000: value of a
int b[10]; …
void foo () {
0x2000: value of b[0]
a = 0;
0x2004: value of b[1]
b[a] = a;
…
}
0x2024: value of b[9]
▪ Static vs. dynamic computation
▪ Global/static variables can exist before program starts
▪ Compiler allocates static variables, giving them a constant address
▪ No dynamic computation required to allocate the variables, they just exist
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 25
Assume a is a global variable in C. When is space for a allocated? In
other words, when is its address determined?
Regs
01 0 1
13 bytes
2 bytes
▪ r[x] ← constant
▪ m[r[x]] ← r[y]
▪ r[y] ← m[r[x]]
.pos 0x1000
a: .long 0 # the variable a
00-- 00000000
01-- 00001000 .pos 0x2000
3001 b_data .long 0 # the variable b[0]
1102 .long 0 # the variable b[1]
03-- 00002000 ...
4232
.long 0 # the variable b[9]
▪ Instruction format
▪ 2-byte or 6-byte instructions
Ins Op Code
Ins Op 0
Ins Op 1
Ins Op 2
▪ Execute stage:
▪ Read internal state and/or memory
▪ Perform specified computation
▪ Update internal state and/or memory
▪ Main memory:
▪ Read with mem.readInteger(address)
▪ Change with mem.writeInteger(address, value)
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 55
▪ Java:
public class Foo {
static int a;
static int[] b = new int[10];
public void foo () {
b[a] = a; ▪ C:
} int a;
} int* b;
void foo () {
b = malloc(10*sizeof(int));
b[a] = a;
}
CPSC 213 2020 ST1 © 2020 Jonatan Schroeder 56
▪ 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 pointers to other arrays
int *bptr = &bst[0]; // or int *bptr = bst;
▪ Can store pointers to arrays allocated dynamically with call to malloc
library function
int *bdyn = malloc(10 * sizeof(int));
A. 2
B. 3
C. 4
D. 5
E. I don’t know
CPSC 213 2020 WT2 © 2021 Jonatan Schroeder 64
▪ In Java and C:
▪ An array is a list of items of the same type
▪ Array elements are named by non-negative integers starting at 0
▪ Syntax for accessing element i of array b is b[i]
▪ In Java:
▪ Variable stores a pointer to the array
▪ In C
▪ Variable can store a pointer to the array or the array itself
▪ Distinguished by variable type
ld $3, r3 # r3 = 3
ld (r2,r3,4), r4 # r4 = c[3]
st r4, (r2) # *c = c[3]
.pos 0x2000
c: .long 0 # or some data used in simulator to emulate malloc