Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
CGS 3460, Lecture 3 Jan 13, 2006 Hen-I Yang

2 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

3 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.

4 Agenda Machine Code – Assembly – C 3 Steps to a runnable program.
Software Engineering: Implementing a software that solves the problem. Importance of commenting.

5 Machine Code 106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: mov 0x19, %o0 10704: call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: nop 1070c: mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g1 10718: f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: nop 10724: sethi %hi(0x10800), %g1 10728: or %g1, 0x10, %o0 ! <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp ], %o1 10730: c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0 ! <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp ], %o1 10744: a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: nop 1074c: clr %g1 ! 0 <_START_-0x10000> 10750: b mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7 Does any one uses Hex? Who uses

6 Assembly 106fc: 9d e3 bf 88 save %sp, -120, %sp
10700: mov 0x19, %o0 10704: call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: nop 1070c: mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g1 10718: f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: nop 10724: sethi %hi(0x10800), %g1 10728: or %g1, 0x10, %o0 ! <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp ], %o1 10730: c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0 ! <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp ], %o1 10744: a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: nop 1074c: clr %g1 ! 0 <_START_-0x10000> 10750: b mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7

7 106fc: 9d e3 bf 88 save %sp, -120, %sp 10700: mov 0x19, %o0 10704: call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: nop 1070c: mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g1 10718: f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: nop 10724: sethi %hi(0x10800), %g1 10728: or %g1, 0x10, %o0 ! <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp ], %o1 10730: c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0 ! <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp ], %o1 10744: a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: nop 1074c: clr %g1 ! 0 <_START_-0x10000> 10750: b mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7

8 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; }

9 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 HD, memory, CPU

10 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. Compile/Interpret

11 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

12 #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; }

13 #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; }

14 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; }

15 Assembly 106fc: 9d e3 bf 88 save %sp, -120, %sp
10700: mov 0x19, %o0 10704: call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: nop 1070c: mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g1 10718: f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: nop 10724: sethi %hi(0x10800), %g1 10728: or %g1, 0x10, %o0 ! <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp ], %o1 10730: c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0 ! <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp ], %o1 10744: a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: nop 1074c: clr %g1 ! 0 <_START_-0x10000> 10750: b mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7

16 Machine Code 106fc: 9d e3 bf 88 save %sp, -120, %sp
10700: mov 0x19, %o0 10704: call 208c8 <_PROCEDURE_LINKAGE_TABLE_+0x78> 10708: nop 1070c: mov %o0, %g1 10710: c2 27 bf ec st %g1, [ %fp ] 10714: sethi %hi(0x10400), %g1 10718: f8 or %g1, 0x3f8, %o0 ! 107f8 <_lib_version+0x8> 1071c: e call 208d4 <_PROCEDURE_LINKAGE_TABLE_+0x84> 10720: nop 10724: sethi %hi(0x10800), %g1 10728: or %g1, 0x10, %o0 ! <_lib_version+0x20> 1072c: d2 07 bf ec ld [ %fp ], %o1 10730: c call 208e0 <_PROCEDURE_LINKAGE_TABLE_+0x90> 10734: nop 10738: sethi %hi(0x10800), %g1 1073c: or %g1, 0x18, %o0 ! <_lib_version+0x28> 10740: d2 07 bf ec ld [ %fp ], %o1 10744: a call 208ec <_PROCEDURE_LINKAGE_TABLE_+0x9c> 10748: nop 1074c: clr %g1 ! 0 <_START_-0x10000> 10750: b mov %g1, %i0 10754: 81 c7 e0 08 ret 10758: 81 e restore 1075c: 81 c3 e0 08 retl 10760: ae 03 c0 17 add %o7, %l7, %l7

17 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

18 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

19 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

20 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.

21 Before you go Read Chapter 1 and Section 2.1 – 2.2.
Problem: design a software for Gatorade vending machine.


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google