210 likes | 332 Vues
This lecture focuses on the fundamental concepts of programming, emphasizing the control and functionality achieved through writing one's own code. It covers essential topics such as binary and decimal conversions, the importance of commenting in code, and the steps to transition from problem analysis to creating a runnable program. The session will demonstrate the process of software engineering, explaining the roles of source files, compilers, and executables, ensuring participants understand how to debug and test their creations effectively.
E N D
Introduction to Programming CGS 3460, Lecture 3 Jan 13, 2006 Hen-I Yang
Previously… • Introduction to Computer • Writing your own program gives the total control of what your computer can do for you. • Introduction to Binary – Decimal – Hex conversions
Administrivia • Turn in your questionnaire if you have not done so. • Course material is updated on the course web site. • My office hour will be temporarily hold in E309, effective today. • TA’s office hour schedule will be announced next Wednesday. • Comments on your responses to the questionnaire.
Agenda • Machine Code – Assembly – C • 3 Steps to a runnable program. • Software Engineering: Implementing a software that solves the problem. • Importance of commenting.
1001 1101 1110 0011 Machine Code 106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: 90 10 20 19 mov 0x19, %o0 10704: 40 00 40 71 call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: 01 00 00 00 nop 1070c: 82 10 00 08 mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp + -20 ] 10714: 03 00 00 41 sethi %hi(0x10400), %g1 10718: 90 10 63 f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: 40 00 40 6e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: 01 00 00 00 nop 10724: 03 00 00 42 sethi %hi(0x10800), %g1 10728: 90 10 60 10 or %g1, 0x10, %o0 ! 10810 <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp + -20 ], %o1 10730: 40 00 40 6c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: 01 00 00 00 nop 10738: 03 00 00 42 sethi %hi(0x10800), %g1 1073c: 90 10 60 18 or %g1, 0x18, %o0 ! 10818 <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp + -20 ], %o1 10744: 40 00 40 6a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: 01 00 00 00 nop 1074c: 82 10 20 00 clr %g1 ! 0 <_START_-0x10000> 10750: b0 10 00 01 mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e8 00 00 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: 90 10 20 19 mov 0x19, %o0 10704: 40 00 40 71 call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: 01 00 00 00 nop 1070c: 82 10 00 08 mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp + -20 ] 10714: 03 00 00 41 sethi %hi(0x10400), %g1 10718: 90 10 63 f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: 40 00 40 6e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: 01 00 00 00 nop 10724: 03 00 00 42 sethi %hi(0x10800), %g1 10728: 90 10 60 10 or %g1, 0x10, %o0 ! 10810 <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp + -20 ], %o1 10730: 40 00 40 6c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: 01 00 00 00 nop 10738: 03 00 00 42 sethi %hi(0x10800), %g1 1073c: 90 10 60 18 or %g1, 0x18, %o0 ! 10818 <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp + -20 ], %o1 10744: 40 00 40 6a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: 01 00 00 00 nop 1074c: 82 10 20 00 clr %g1 ! 0 <_START_-0x10000> 10750: b0 10 00 01 mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e8 00 00 restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7
106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: 90 10 20 19 mov 0x19, %o0 10704: 40 00 40 71 call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: 01 00 00 00 nop 1070c: 82 10 00 08 mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp + -20 ] 10714: 03 00 00 41 sethi %hi(0x10400), %g1 10718: 90 10 63 f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: 40 00 40 6e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: 01 00 00 00 nop 10724: 03 00 00 42 sethi %hi(0x10800), %g1 10728: 90 10 60 10 or %g1, 0x10, %o0 ! 10810 <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp + -20 ], %o1 10730: 40 00 40 6c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: 01 00 00 00 nop 10738: 03 00 00 42 sethi %hi(0x10800), %g1 1073c: 90 10 60 18 or %g1, 0x18, %o0 ! 10818 <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp + -20 ], %o1 10744: 40 00 40 6a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: 01 00 00 00 nop 1074c: 82 10 20 00 clr %g1 ! 0 <_START_-0x10000> 10750: b0 10 00 01 mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e8 00 00 restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7
C Source Code #include <stdio.h> int main() { char *name; name = (char *) malloc (25); puts ("Please enter your name:"); scanf ("%20s", name); printf ("\nHello, %s\n", name); return 0; }
3 Steps To Runnable Program • Source file (.c) • Editor: The tool you use to edit your source code. • Object file (.o) • Compiler + other utilities: The tool computer runs to create object file and executables. • Executables • The end product that actually delivers what you coded. • Demo
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.
Greetings 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
#include <stdio.h> /** * 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 <stdio.h> /** * 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; }
C Source Code #include <stdio.h> int main() { char *name; name = (char *) malloc (25); puts ("Please enter your name:"); scanf ("%20s", name); printf ("\nHello, %s\n", name); return 0; }
Assembly 106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: 90 10 20 19 mov 0x19, %o0 10704: 40 00 40 71 call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: 01 00 00 00 nop 1070c: 82 10 00 08 mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp + -20 ] 10714: 03 00 00 41 sethi %hi(0x10400), %g1 10718: 90 10 63 f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: 40 00 40 6e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: 01 00 00 00 nop 10724: 03 00 00 42 sethi %hi(0x10800), %g1 10728: 90 10 60 10 or %g1, 0x10, %o0 ! 10810 <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp + -20 ], %o1 10730: 40 00 40 6c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: 01 00 00 00 nop 10738: 03 00 00 42 sethi %hi(0x10800), %g1 1073c: 90 10 60 18 or %g1, 0x18, %o0 ! 10818 <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp + -20 ], %o1 10744: 40 00 40 6a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: 01 00 00 00 nop 1074c: 82 10 20 00 clr %g1 ! 0 <_START_-0x10000> 10750: b0 10 00 01 mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e8 00 00 restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7
Machine Code 106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: 90 10 20 19 mov 0x19, %o0 10704: 40 00 40 71 call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: 01 00 00 00 nop 1070c: 82 10 00 08 mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp + -20 ] 10714: 03 00 00 41 sethi %hi(0x10400), %g1 10718: 90 10 63 f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: 40 00 40 6e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: 01 00 00 00 nop 10724: 03 00 00 42 sethi %hi(0x10800), %g1 10728: 90 10 60 10 or %g1, 0x10, %o0 ! 10810 <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp + -20 ], %o1 10730: 40 00 40 6c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: 01 00 00 00 nop 10738: 03 00 00 42 sethi %hi(0x10800), %g1 1073c: 90 10 60 18 or %g1, 0x18, %o0 ! 10818 <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp + -20 ], %o1 10744: 40 00 40 6a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: 01 00 00 00 nop 1074c: 82 10 20 00 clr %g1 ! 0 <_START_-0x10000> 10750: b0 10 00 01 mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e8 00 00 restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7
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 2k, (k = 3, 2, 1, 0) • if the quote is 1, put down 1, n = n – 2k • If the quote is 0, put down 0 • moves on to the next k
Sorting Problem: A stack of graded exam papers, sort them from the highest score to the lowest • Pick the one from the top of the pile, put it down • Pick the next one, • if the score is higher, put it on top • Otherwise, flip the paper and see if the score is higher than the next one • If it’s higher than insert it to the slot • If not, keep flipping through to the next page • If it’s to the bottom of the sorted pile, put it down on the back
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
Summary • What is a program? Different Formats of a program. • 3 steps to a runnable program. • 7 steps from analysis of problem to executable. • Comments.
Before you go • Read Chapter 1 and Section 2.1 – 2.2. • Problem: design a software for Gatorade vending machine.