DOC PREVIEW
Penn CIS 240 - Programming the LC III

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Based on slides © McGraw-HillAdditional material © 2004/2005/2006 Lewis/MartinChapter 6Programming the LC-36-2CSE 240Aside: Booting the Computer How does it all begin?• We have LC-3 hardware and a program, but what next? Initial state of computer• All zeros (registers, memory, condition codes)• Only mostly true Boot process• Load boot code held in ROM (read-only memory)!BIOS (basic input/output system)• Loads operating system from disk (or other input device)• Operating systems loads other programs!Uses memory operations (loads, stores)!Sets PC to beginning of program to run it!Programs invoke O.S. using TRAP instructions6-3CSE 240Solving Problems using a Computer Methodologies for creating computer programsthat perform a desired function Problem Solving• How do we figure out what to tell the computer to do?• Convert problem statement into algorithm(stepwise refinement)• Convert algorithm into LC-3 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!6-4CSE 240Stepwise Refinement Also known as systematic decomposition Start with problem statement: “We wish to count the number of occurrences of a characterin a file. The character in question is to be input fromthe 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 level6-5CSE 240Problem Statement Because problem statements are written in English,they are sometimes ambiguous and/or incomplete• Where is the data located? How big is it, or how do I knowwhen I’ve reached the end?• How should final count be printed? A decimal number?• If the character is a letter, should I count bothupper-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 it6-6CSE 240Three Basic Constructs There are three basic ways to decompose a task:TaskSubtask 1Subtask 2Subtask 1 Subtask 2TestconditionSubtaskTestconditionSequential Conditional IterativeTrueTrueFalseFalse6-7CSE 240Programming at the Instruction LevelAdvantage: can do anything• General, powerful Disadvantage: can do anything• Difficult to structure, modify, understand Mitigate disadvantages using structured programming• Use familiar constructs (even at the instruction level)!From Java/C/Pascal/Fortran/Basic• Iteration (while loop, for loop)• Conditional (if statement, switch/case statement)6-8CSE 240Sequential Do Subtask 1 to completion,then do Subtask 2 to completion, etc.Get characterinput fromkeyboardExamine file andcount the numberof characters thatmatchPrint numberto the screenCount and print theoccurrences of acharacter in a file6-9CSE 240Conditional If condition is true, do Subtask 1;else, do Subtask 2Test character.If match, incrementcounter.Count = Count + 1file char= input?True False6-10CSE 240Iterative Do Subtask over and over,as long as the test condition is trueCheck each element ofthe file and count thecharacters that match.Check next char andcount if matches.more charsto check?TrueFalse6-11CSE 240LC-3 Control Instructions How can instructions encode these basic constructs? Sequential• Instructions naturally flow from one to next, so no specialinstruction needed to go from one sequential subtask to next Conditional and Iterative• Create code that converts condition into N, Z, or P!Condition: “Is R0 = R1?”!Code: Subtract R1 from R0; if equal, Z bit will be set• Use BR instruction to transfer control• What about R0 < R1?!Code: Subtract R1 from R0 (R0-R1), if less, N bit will be set6-12CSE 240Code for ConditionalGenerateConditionInstructionA0000BSubtask 1CSubtask 2NextSubtaskD? C0000 111 DSubtask 1TestConditionTrue FalseSubtask 2NextSubtaskExact bits dependon conditionbeing testedPC offset toaddress CPC offset toaddress DUnconditional branchto Next SubtaskAssuming all addresses are close enough that PC-relative branch can be used6-13CSE 240Code for IterationGenerateConditionInstructionA0000BSubtaskCNextSubtask? C0000 111 ASubtaskTestConditionTrueFalseNextSubtaskExact bits dependon conditionbeing testedPC offset toaddress CPC offset toaddress AUnconditional branchto retest conditionAssuming all addresses are close enough that PC-relative branch can be used6-14CSE 240Example (from both Ch 5 and 6) Count the occurrences of a character in a file• Program begins at location x3000• Read character from keyboard• Load each character from a “file”! In this example the “file” is already in sequence of memory locations! Starting address of file is stored in the memory locationimmediately after the program• If file character equals input character, increment counter• End of file is indicated by a special ASCII value: EOT (x04)• At the end, print the number of characters and halt(assume there will be fewer than 10 occurrences of the character)A special character used to indicate the end of a sequenceis often called a sentinel• Useful when you don’t know ahead of time how many timesto execute a loop6-15CSE 240Example: Counting CharactersInput a character. Thenscan a file, countingoccurrences of thatcharacter. Finally, displayon the monitor the numberof occurrences of thecharacter (up to 9).STARTSTOPInitialize: Put initial valuesinto all locations that will beneeded to carry out thistask.- Input a character.- Set up a pointer to the firstlocation of the file that willbe scanned.- Get the first character fromthe file.- Zero the register that holdsthe count.STARTSTOPScan the file, location bylocation, incrementing thecounter if the charactermatches.Display the count on themonitor.ABCInitial refinement: Big task intothree sequential subtasks.6-16CSE 240Refining BScan the file, location bylocation, incrementing thecounter if the charactermatches.BTest character. If a match,increment counter. Get nextcharacter.B1Done?NoYesBRefining B into iterative construct.6-17CSE 240Refining B1Refining B1 into sequential subtasks.Test character. If a match,increment counter. Get nextcharacter.B1Done?NoYesBGet next character.B1Done?NoYesTest character. If matches,increment counter.B2B36-18CSE 240Refining B2 and B3R1 = M[R3]Done?NoYesB2B3R3 = R3 + 1R1 = R0?R2 = R2 + 1NoYesGet next character.B1Done?NoYesTest character.


View Full Document

Penn CIS 240 - Programming the LC III

Download Programming the LC III
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming the LC III and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming the LC III 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?