1 / 21

Introduction to Programming

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.

eljah
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

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

  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

  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.

  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: 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

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

  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.

More Related