Introduction to Computing Systems and Programming Programming.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

PART 6 Programming 1.Decomposition 2.Three Constructs 3.Counting Example 4.Debugging.
CS107 Introduction to Computer Science Lecture 2.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
Chapter 1 - An Introduction to Computers and Problem Solving
CS1010 Programming Methodology
Chapter 2: Algorithm Discovery and Design
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Chapter 16 Programming and Languages: Telling the Computer What to Do.
Chapter 2: Algorithm Discovery and Design
Chapter 1 Program Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to High-Level Language Programming
The University of Texas – Pan American
CIS Computer Programming Logic
COP1220/CGS2423 Introduction to C++/ C for Engineers Professor: Dr. Miguel Alonso Jr. Fall 2008.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
1 Overview of Programming and Problem Solving Chapter 1.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4 - Implementing Standard Program Structures in 8086 Assembly Language from Microprocessors and Interfacing by Douglas Hall.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 6 Programming. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 6-2 Solving Problems using a Computer.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 6 Programming l Problem solving l Debugging.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
 Problem Analysis  Coding  Debugging  Testing.
1 The user’s view  A user is a person employing the computer to do useful work  Examples of useful work include spreadsheets word processing developing.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Chapter 6 Programming Adapted from slides provided by McGraw-Hill Companies Inc., modified by professors at University of Wisconsin-Madison.
Chapter 7 Assembly Language
Chapter 7 Assembly Language
COSC121: Computer Systems
Lecture 2 Introduction to Programming
Introduction to Computer Engineering
Chapter 15 Debugging.
Programming Problem solving Debugging
Chapter 6 Programming.
Introduction to Computer Engineering
Chapter 6 Programming.
Chapter 15 Debugging.
Introduction to Computer Engineering
Introduction to Computer Engineering
Chapter 7 Assembly Language
ICT Gaming Lesson 2.
COSC121: Computer Systems
Chapter 15 Debugging.
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Chapter 6 Programming.
Chapter 13 Control Structures
Chapter 15 Debugging.
Presentation transcript:

Introduction to Computing Systems and Programming Programming

Fall 1384, 2Introduction to Computing Systems and Programming Solving Problems using a Computer  Methodologies for creating computer programs that perform a desired function.  Problem Solving How do we figure out what to tell the computer to do? Convert problem statement into algorithm, using stepwise refinement. Convert algorithm into LC-2 machine instructions.  Debugging How do we figure out why it didn’t work? Examining registers and memory, setting breakpoints, etc. Time spent on the first can reduce time spent on the second!

Fall 1384, 3Introduction to Computing Systems and Programming Stepwise Refinement  Also known as systematic decomposition.  Start with problem statement: “We wish to count the number of occurrences of a character in a file. The character in question is to be input from the keyboard; the result is to be displayed on the monitor.”  Decompose task into a few simpler subtasks.  Decompose each subtask into smaller subtasks, and these into even smaller subtasks, etc.... until you get to the machine instruction level.

Fall 1384, 4Introduction to Computing Systems and Programming Problem Statement  Because problem statements are written in English, they are sometimes ambiguous and/or incomplete. Where is “file” located? How big is it, or how do I know when I’ve reached the end? How should final count be printed? A decimal number? If the character is a letter, should I count both upper-case and lower-case occurrences?  How do you resolve these issues? Ask the person who wants the problem solved, or Make a decision and document it.

Fall 1384, 5Introduction to Computing Systems and Programming Three Basic Constructs  There are three basic ways to decompose a task:

Fall 1384, 6Introduction to Computing Systems and Programming Sequential  Do Subtask 1 to completion, then do Subtask 2 to completion, etc. Get character input from keyboard Examine file and count the number of characters that match Print number to the screen Count and print the occurrences of a character in a file

Fall 1384, 7Introduction to Computing Systems and Programming Conditional  If condition is true, do Subtask 1; else, do Subtask 2. Test character. If match, increment counter. Count = Count + 1 file char = input? TrueFalse

Fall 1384, 8Introduction to Computing Systems and Programming Iterative  Do Subtask over and over, as long as the test condition is true. Check each element of the file and count the characters that match. Check next char and count if matches. more chars to check? True False

Fall 1384, 9Introduction to Computing Systems and Programming Problem Solving Skills  Learn to convert problem statement into step-by-step description of subtasks. Like a puzzle, or a “word problem” from grammar school math. What is the starting state of the system? What is the desired ending state? How do we move from one state to another? Recognize the words that correlate to three basic constructs: “do A then do B”  sequential “if G, then do H”  conditional “for each X, do Y”  iterative “do Z until W”  iterative

Fall 1384, 10Introduction to Computing Systems and Programming LC-2 Control Instructions  How do we use LC-2 instructions to encode the three basic constructs?  Sequential Instructions naturally flow from one to the next, so no special instruction needed to go from one sequential subtask to the next.  Conditional and Iterative Create code that converts condition into N, Z, or P. Example: Condition: “Is R0 = R1?” Code: Subtract R1 from R0; if equal, Z bit will be set. Then use BR instruction to transfer control to the proper subtask.

Fall 1384, 11Introduction to Computing Systems and Programming Example: Counting Characters Input a character. Then scan a file, counting occurrences of that character. Finally, display on the monitor the number of occurrences of the character (up to 9). START STOP Initialize: Put initial values into all locations that will be needed to carry out this task. - Input a character. - Set up a pointer to the first location of the file that will be scanned. - Get the first character from the file. - Zero the register that holds the count. START STOP Scan the file, location by location, incrementing the counter if the character matches. Display the count on the monitor. A B C Initial refinement: Big task into three sequential subtasks.

Fall 1384, 12Introduction to Computing Systems and Programming Refining B Scan the file, location by location, incrementing the counter if the character matches. B Test character. If a match, increment counter. Get next character. B1 Done? No Yes B Refining B into iterative construct.

Fall 1384, 13Introduction to Computing Systems and Programming Refining B1 Refining B1 into sequential subtasks. Test character. If a match, increment counter. Get next character. B1 Done? No Yes B Get next character. B1 Done? No Yes Test character. If matches, increment counter. B2 B3

Fall 1384, 14Introduction to Computing Systems and Programming Refining B2 and B3 R1 = M[R3] Done? No Yes B2 B3 R3 = R3 + 1 R1 = R0? R2 = R2 + 1 NoYes Get next character. B1 Done? No Yes Test character. If matches, increment counter. B2 B3 Conditional (B2) and sequential (B3). Use of LC-2 registers and instructions.

Fall 1384, 15Introduction to Computing Systems and Programming The Last Step: LC-2 Instructions  Use comments to separate into modules and to document your code. R1 = M[R3] Done? No Yes B2 B3 R3 = R3 + 1 R1 = R0? R2 = R2 + 1 NoYes ; Look at each char in file. is R1 = EOT? ; is R1 = EOT? ; if so, exit loop xxxxxxxxx ; if so, exit loop ; Check for match with R0. ; R1 = -char ; R1 = -char ; R1 = R0 – char ; R1 = R0 – char ; no match, skip incr xxxxxxxxx ; no match, skip incr ; R2 = R ; R2 = R2 + 1 ; Incr file ptr and get next char R3 = R ; R3 = R3 + 1 ; R1 = M[R3] ; R1 = M[R3] Don’t know address bits until all the code is done

Fall 1384, 16Introduction to Computing Systems and Programming Debugging  You’ve written your program and it doesn’t work.  Now what?  What do you do when you’re lost in a city? Drive around randomly and hope you find it?  Return to a known point and look at a map?  In debugging, the equivalent to looking at a map is tracing your program. Examine the sequence of instructions being executed. Keep track of results being produced. Compare result from each instruction to the expected result.

Fall 1384, 17Introduction to Computing Systems and Programming Debugging Operations  Any debugging environment should provide means to: 1. Display values in memory and registers. 2. Deposit values in memory and registers. 3. Execute instruction sequence in a program. 4. Stop execution when desired. Different programming levels offer different tools. High-level languages (C, Java,...) usually have source-code debugging tools. For debugging at the machine instruction level: simulators operating system “monitor” tools in-circuit emulators (ICE)  plug-in hardware replacements that give instruction-level control

Fall 1384, 18Introduction to Computing Systems and Programming LC-2 Simulator set/displayregisters and memory executeinstructionsequences stop execution, set breakpoints

Fall 1384, 19Introduction to Computing Systems and Programming Types of Errors  Syntax Errors You made a typing error that resulted in an illegal operation. Not usually an issue with machine language, because almost any bit pattern corresponds to some legal instruction. In high-level languages, these are often caught during the translation from language to machine code.  Logic Errors Your program is legal, but wrong, so the results don’t match the problem statement. Trace the program to see what’s really happening and determine how to get the proper behavior.  Data Errors Input data is different than what you expected. Test the program with a wide variety of inputs.

Fall 1384, 20Introduction to Computing Systems and Programming Tracing the Program  Execute the program one piece at a time, examining register and memory to see results at each step.  Single-Stepping Execute one instruction at a time. Tedious, but useful to help you verify each step of your program.  Breakpoints Tell the simulator to stop executing when it reaches a specific instruction. Check overall results at specific points in the program. Lets you quickly execute sequences to get a high-level overview of the execution behavior. Quickly execute sequences that your believe are correct.  Watchpoints Tell the simulator to stop when a register or memory location changes or when it equals a specific value. Useful when you don’t know where or when a value is changed.

Fall 1384, 21Introduction to Computing Systems and Programming Debugging: Lessons Learned  Trace program to see what’s going on. Breakpoints, single-stepping  When tracing, make sure to notice what’s really happening, not what you think should happen. In summing program, it would be easy to not notice that address x3107 was loaded instead of x3100.  Test your program using a variety of input data. In Examples 3 and 4, the program works for many data sets. Be sure to test extreme cases (all ones, no ones,...).