Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Systems and Programming Programming.

Similar presentations


Presentation on theme: "Introduction to Computing Systems and Programming Programming."— Presentation transcript:

1 Introduction to Computing Systems and Programming Programming

2 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!

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

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

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

6 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

7 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

8 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

9 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

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

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

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

13 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

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

15 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? 0001100001111100 ; is R1 = EOT? ; if so, exit loop 0000010xxxxxxxxx ; if so, exit loop ; Check for match with R0. ; R1 = -char 1001001001111111 ; R1 = -char 0001001001100001 ; R1 = R0 – char 0001001000000001 ; R1 = R0 – char ; no match, skip incr 0000101xxxxxxxxx ; no match, skip incr ; R2 = R2 + 1 0001010010100001 ; R2 = R2 + 1 ; Incr file ptr and get next char R3 = R3 + 1 0001011011100001 ; R3 = R3 + 1 ; R1 = M[R3] 0110001011000000 ; R1 = M[R3] Don’t know address bits until all the code is done

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

17 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

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

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

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

21 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,...).


Download ppt "Introduction to Computing Systems and Programming Programming."

Similar presentations


Ads by Google