Reading Examine Java and C Piece by Piece ‣ Companion ‣ Reading writing and arithmetic on variables CPSC 213 • 2.2.3, 2.3, 2.4.1-2.4.3, 2.6 •static base types (e.g., int, char) ‣ Textbook •static and dynamic arrays of base types •dynamically allocated objects/structs and object references •Array Allocation and Access •object instance variables •1ed: 3.8 Design Plan •procedure locals and arguments •2ed: 3.8 ‣ Control flow Introduction to Computer Systems •static intra-procedure control flow (e.g., if, for, while) •static procedure calls Unit 1b •dynamic control flow Scalars and Arrays 1 2 3 4 Java and C: Many Syntax Similarities Java and C: Many Differences Design Tasks The Simple Machine (SM213) ISA ‣ similar syntax for many low-level operations ‣ some syntax differences, many deeper differences ‣ Design Instructions for SM213 ISA ‣ Architecture • C is not (intrinsically) object oriented •design instructions necessary to implement the languages •Register File 8, 32-bit general purpose registers • ancestor of both Java and C++ •keep hardware simple/fast by adding as few/simple instructions possible •CPU one cycle per instruction (fetch + execute) ‣ declaration, assignment ‣ more details as we go! ‣ Develop Compilation Strategy •Main Memory byte addressed, Big Endian integers Java Hello World... •int a = 4; ‣ Instruction Format •determine how compiler will compile each language feature it sees ‣ control flow (often) import java.io.*; •which instructions will it use? •2 or 6 byte instructions (each character is a hex digit) public class HelloWorld { •if (a == 4) ... else ... public static void main (String[] args) { - x-sd , xsd- , xxsd , xsvv, xxvs , or xs-- vvvvvvvv •in what order? •for (int i = 0; i < 10; i++) {...} System.out.println("Hello world"); •where •what can compiler compute statically? •while (i < 10) {...} } - x or xx is opcode (unique identifier for this instruction) ‣ Consider Static and Dynamic Phases of Computation ‣ casting } - - means unused C Hello World... •the static phase of computation (compilation) happens just once - s and d are operands (registers), sometimes left blank with - int a; •the dynamic phase (running the program) happens many times - vv and vvvvvvvv are immediate / constant values #include <stdio.h> long b; main() { •thus anything the compiler computes, saves execution time later a = (int) b; printf("Hello world\n"); } 5 6 7 8 Machine and Assembly Syntax Register Transfer Language (RTL) The CPU Implementation ‣ Machine code ‣ Goal ‣ Internal state • pc address of next instruction to fetch •[ addr: ] x-01 [ vvvvvvvv ] • a simple, convenient pseudo language to describe instruction semantics • instruction the value of the current instruction - addr: sets starting address for subsequent instructions • easy to read and write, directly translated to machine steps - insOpCode - x-01 hex value of instruction with opcode x and operands 0 and 1 ‣ Syntax - insOp0 - vvvvvvvv hex value of optional extended value part instruction - insOp1 • each line is of the form LHS ← RHS ‣ Assembly code - insOp2 Implementing the ISA • LHS is memory or register specification - insOpImm •( [label:] [instruction | directive] [# comment] | )* • RHS is constant, memory, or arithmetic expression on two registers - insOpExt - directive :: (.pos number) | (.long number) ‣ Operation ‣ Register and Memory are treated as arrays - instruction :: opcode operand+ • fetch • m[a] is memory location at address a -operand :: $literal | reg | o fg set (reg) | (reg,reg,4) - read instruction at pc from memory, determine its size and read all of it • r[i] is register number i -reg - separate the components of the instruction into sub-registers :: r 0..7 ‣ For example - set pc to store address of next instruction, sequentially -literal :: number • execute -o fg set :: number • r[0] ← 10 - use insOpCode to select operation to perform -number :: decimal | 0x hex • r[1] ← m[r[0]] - read internal state, memory, and/or register file • r[2] ← r[0] + r[1] - update memory, register file and/or pc 9 10 11 12 Static Variables, Built-In Types (S1-global-static) Static Variable Allocation Static Variable Allocation Static Memory Layout ‣ Java 0x1000: value of a int a; int a; int a; int a; • static data members are allocated to a class, not an object int b[10]; int b[10]; 0x2000: value of b[0] int b[10]; int b[10]; • they can store built-in scalar types or references to arrays or objects (references later) 0x2004: value of b[1] void foo () { void foo () { public class Foo { ... a = 0; a = 0; static int a; b[a] = a; b[a] = a; 0x2024: value of b[9] static int[] b; // array is not static, so skip for now } } Static Variables of public void foo () { a = 0; ‣ Allocation is ‣ Allocation is Built-In Types }} ‣ C • assigning a memory location to store variable’s value • assigning a memory location to store variable’s value • global variables and any other variable declared static • assigning the variable an address (its name for reading and writing) • assigning the variable an address (its name for reading and writing) • they can be static scalars, arrays or structs or pointers (pointers later) ‣ Key observation ‣ Key observation int a; • global/static variables can exist before program starts and live until after it finishes • global/static variables can exist before program starts and live until after it finishes int b[10]; ‣ Static vs dynamic computation ‣ Static vs dynamic computation void foo () { • compiler allocates variables, giving them a constant address • compiler allocates variables, giving them a constant address a = 0; b[a] = a; • no dynamic computation required to allocate the variables, they just exist • no dynamic computation required to allocate the variables, they just exist } 13 14 15 15
Recommend
More recommend