C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang
Previously… Machine Code – Assembly – C 3 Steps to a runnable program. + Demo Greetings.c Software Engineering: Implementing a software that solves real problems. Importance of commenting
Administrivia Introducing your TAs. TAs Office Hours (E309) Gang Tu 6 – 9th periods Zhen M 5—7th, Th 3 – 5th periods Xiao Li W 7—8 th, F 5—7th periods Info available on the web site. A new Q&A page has been added.
Administrivia (II) Put [CGS3460] in the subject when sending s related to the course. /questions asked during office hours regarding course related materials may be edited, compiled or modified and posted on the web to help other students learn. The compilation and edition will protect students’ privacy. If you strongly against the discussion being edited, modified or posted, please indicate clearly [DO NOT POST] in your .
Agenda Continue Discussion on “Software Engineering: Implementing a software that solves the problem”. Discuss Vending Machine Exercise.
Cold Pizza Software Engineering: Implementing a software that solves the problem. Importance of commenting.
From Analysis to Executable Problem Analysis (Human Perception) Strategy Forming (Pesudo code) Implementation(High Level Language) Compilation(Assembly Language) Debug Execution(Machine Code) Debug/Testing Example: Greeting Program.
Problem: Write a program that greets the user Step 1: Collect User’s Name Step 2: Print Greeting Message On Screen Name Collect User’s Name Name Screen Greetings
#include /** * First sample program of CGS 3460, spring 2006 * Author: Hen-I Yang * Date: Jan 10, 2006 * Version: 1.0 * Note: This program greets the user * Revision: **/ int main() { /* Declare a variable called name to store name collected */ /* Allocate Memory to Record User’s Name Collected */ /* Collect User’s Name (Name Collect User’s Name) */ /* Print a Greeting Message (Name Screen) */ return 0; }
#include /** * First sample program of CGS 3460, spring 2006 * Author: Hen-I Yang * Date: Jan 10, 2006 * Version: 1.0 * Note: * Revision: **/ int main() { /* Declare a variable called name to store name collected */ char *name; /* Allocate Memory to Record User’s Name Collected */ name = (char *) malloc (25); /* Collect User’s Name (Name Collect User’s Name) */ puts ("Please enter your name:"); scanf ("%20s", name); /* Print a Greeting Message (Name Screen) */ printf ("Hello, %s\n", name); return 0; }
#include int main() { char *name; name = (char *) malloc (25); puts ("Please enter your name:"); scanf ("%20s", name); printf ("\nHello, %s\n", name); return 0; } C Source Code
106fc:9d e3 bf 88 save %sp, -120, %sp 10700: mov 0x19, %o : call 208c : nop 1070c: mov %o0, %g :c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g : f8 or %g1, 0x3f8, %o0! 107f8 1071c: e call 208d : nop 10724: sethi %hi(0x10800), %g : or %g1, 0x10, %o0! c:d2 07 bf ec ld [ %fp ], %o : c call 208e : nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0! :d2 07 bf ec ld [ %fp ], %o : a call 208ec 10748: nop 1074c: clr %g1! :b mov %g1, %i :81 c7 e0 08 ret 10758:81 e restore 1075c:81 c3 e0 08 retl 10760:ae 03 c0 17 add %o7, %l7, %l7 Assembly
106fc:9d e3 bf 88 save %sp, -120, %sp 10700: mov 0x19, %o : call 208c : nop 1070c: mov %o0, %g :c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g : f8 or %g1, 0x3f8, %o0! 107f8 1071c: e call 208d : nop 10724: sethi %hi(0x10800), %g : or %g1, 0x10, %o0! c:d2 07 bf ec ld [ %fp ], %o : c call 208e : nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0! :d2 07 bf ec ld [ %fp ], %o : a call 208ec 10748: nop 1074c: clr %g1! :b mov %g1, %i :81 c7 e0 08 ret 10758:81 e restore 1075c:81 c3 e0 08 retl 10760:ae 03 c0 17 add %o7, %l7, %l7 Machine Code
Hex to Binary Conversion Problem: Given a number in hex(adecimal), convert it to binary representation Get a number in Hex, print out a number in binary Convert to Decimal Divide the number n by 2 k, (k = 3, 2, 1, 0) if the quote is 1, put down 1, n = n – 2 k If the quote is 0, put down 0 moves on to the next k
Gatorade Vending Machine Problem: Design a software that can handle transactions of vending machine Strategy: Input1: Nickle, Dime, Quarter (Penny not allowed) Input2: Orange, Blue, Green, Pink, Refund Output: Drink and Change keep receiving Input1 until Input2 is received if ΣInput1 > Input2 then return Input + (ΣInput1 - Input2) else return (“insufficient fund” screen)
Comments on Comments Programs should be readable by both computer and human Remind yourself what the code does Extremely useful when debugging Important for collaboration Readability
History of C Language Developed in early 1970s. By product of UNIX operating system. (DEC PDP-7) Predecessor: Algol, B, NB 1978, The C Programming Langauge (K&R) Widespread to IBM PC in 80s 1983, ANSI-C C++
Strength/Weakness of C Low-level language (Machine Level, computer’s built-in instruction and fast, Socket, etc) Permissive Flexibility Small, efficient, powerful language, execute and develop fast Standard Library Power Portability Integration with UNIX Too close to machine Error-prone Difficult to understand (many advanced features, terse nature, outdated with minimal interactive, programmers are smarter) Difficult to modify (no module, etc)
Why learn C instead of C++ C++ is much harder to learn Still lots of C code around Not everyone is likely to switch to C++ Still the lowest maintenance for small programs Side Effect avoid learning bad habits
Summary 7 steps from analysis of problem to executable. 3 examples of problem analysis Comments. C trivia. (Chapter 1)
Before you go Make sure you finish reading Chapter 2. Read Chapter 3. Exercises 2.11 and 2.12.