170 likes | 266 Views
Responding to Java-Centric CS Curricula: Integration of C into a Course in Computer Organization. Eric Freudenthal Brian A. Carter ( UG) Rafael Escalante (MS ) University of Texas at El Paso. Context. In our Computer Science (BS) program Much emphasis is on software engineering
E N D
Responding to Java-Centric CS Curricula: Integration of C into a Course in Computer Organization Eric Freudenthal Brian A. Carter (UG) Rafael Escalante (MS) University of Texas at El Paso
Context • In our Computer Science (BS) program • Much emphasis is on software engineering • Low-level fundamentals still valued • Req’d courses in digital design, computer org, architecture, OS • Most coursework in object-oriented languages • Higher level abstractions • Encourage students to think modularly • Connection to systems issues become tenuous • Tools and memory model abstract • Functions defined in context of data • Graduates no longer able to program in C • Others report similar problems
Embedded Target System • MSP430 low-power embedded controller • Twenty-seven instructions • $20 for full development system • Completely free open-source GNU tools • No IDE permitted!
Reform to computer organization course • Exploit • Syntactic similarly Java (familiar) and C • Semantic similarity of C & machine • Second in 4-course Systems sequence: • Digital design (logic, gates, latches, counters, FSA) • Computer organization now teaches C & assembly language • Micro-architecture (pipelining, caches, …) • Operating systems requires understanding of C • Previous design • Intensive assembly language programming for embedded target • New focus: bridging the gap between language & architecture • Integrate teaching of C and assembly; embedded target • Students learn to think in C, implement in both C & assy
Key: Start programming right away by initially limiting addressing modes • Simultaneously represent concepts in C and assembly lang. • Initially hide scoping issues • Global integer variables (even hide registers) • Early: Build skill while focusing on the familiar • Arithmetic expressions and control-flow structures • Complex & unfamiliar features taught as clever tricks • Stacks & subroutines (methods) • Pointers • Local variables • Implementation of arrays & structs …and the addressing modes that enable them
Early programs manipulate static variables • Only requires direct (absolute) addressing mode • Format: operation &source, &dest • Encoding: • Example of simultaneous presentation of C & implementaiton No mention of addressing modes and regs!
Linearization of arithmetic expressions • Operator (parse) trees expose and motivate • Evaluation order dependencies • Need for temporary variables • How compilers work Expression Parse Tree Assembly .data t1: .word t2: .word .text mov &b, &t2 ; t2 = b rra &t2 ; t2 = b/2 mov &c, &t1 ; t1 = c add &t1, &t1 ; t1 = c*2 sub &t1, &t2 ; t1 -= t2 mov &t1, &d ; d = t1 d = c*2 - b/2; = (t1) - d (t2) ÷ (t1) * b 2 c 2 Students mastered more quickly when taught before addressing modes & registers!
Linearization of block-structures with goto C • goto is legal in C • Rather than memorizing templates • Students first translate for, while, etc. to goto C • Conversion of goto C to assembly language is trivial • Homework: deduce rules Remember: No immediate mode Block-structured C source code goto C Assembly Language if (x!=3) y = x; else y = 3; if (x==3)goto x3_else y = x gotox3_end; x3_else: y = 3; x3_end: .data three: .word 3 .text cmp &three, &x jnzx3_else mov &x, &y jmpx3_end x3_else: mov&three, &y x3_end:
Registers • After students master… • Arithmetic expressions, control-flow (if, for, while) • Introduce registers as optimization • More compact instruction • Faster execution • Exposes previously unexplored operand fields mov &src, &dest mov r6, r5
Immediate mode • Mistake: • Initially introduced too late • Optimization for constants • Get the students to suggest it as useful optimization • Only available for source operand (makes sense) • Syntax • #Value • Encoding: don’t explain details • As=3; Reg# = 0 (PC), value in extension word • (Addr mode 3 = register indirect autoincrement)
Pointers and indexed addressing • Discuss C representation of arrays • Contiguous addresses in memory. • Relation to address-of and dereference operators • a[i] is *(a+i) • Pointer variables • p = a • p+i == a+i • Now present problem: no way to implement *p • Discuss potential solutions with students • Introduce appropriate addressing mode • MSP-430 has two: register indirect (2) and indexed (1) • But only indirect (1) is orthogonal
Simplify source and construct parse tree prior to translation • Array reference • a[i] = b • Reduces to • *(a+i) = b • Translate to assy = / \ * b | + (r4) / \ a i mov #a, r4 add &I, r4 ; repeat for sizeof(*a) mov &b, 0(r4) mov &(b+2), 2(r4) ; additional word(s) if needed
Finally, introduce stack and subroutines • Motivate with discussion on subroutines • Students understand jmp • But where/how to store return address? • Introduce stack pointer register and push/pop • Discuss how stack is suitable for supporting recursion • Initially code with explicit push/pop • Then introduce call / ret instruction • As handy optimization • Motivate need for calling convention • Engage students in discussing options. • Local vars: Initially just use registers or static, push/pop • Ensure that they understand C compiler linkage • Labs: lotsa short functions
Results • Academic • During the class • Faster coverage of core concepts • Substantially increased mastery of control flow and addressing-mode selection • Measured using tests, quizzes, labs • Subsequent courses • Students attending course in OS successfully program in C • Increased interest in courses on compilers • Employment • Positive reports from employers • Questions?
Lab Course • Students first learn UNIX tools • Editor, shell, make • Students write C programs that expose: • Variable types (scalar, array, pointer) & access modes • Access modes related to addressing modes • Pointers • Shifts & masks • Students write leaf routines (called by C) in assembly language • Students call C routines from assembly language • Students learn how interrupts work
Tools • SVN (version control system) to manage assignments • Entire repository accessible to TA & instructor • Separate subdir for each student • Separate subdir for each project • Students “collaborate” with selves & TA • Avoids carrying files around on USB stick • Repository tracks commit dates • TA can obtain or “peek” at project, can leave comments • Gcc, gas, ld, make, gdb, emacs • Students required to learn emacs • Exposes how IDEs work