210 likes | 316 Views
The LC-3 – Chapter 6. COMP 2620. Problem Solving. Recall from Chapter 1 that for electrons to solve problems, we need to get through several levels of transformation We go from natural language description of the problem down to electrons
E N D
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP 2620
Problem Solving • Recall from Chapter 1 that for electrons to solve problems, we need to get through several levels of transformation • We go from natural language description of the problem down to electrons • Recall the second step was to write the problem as an algorithm
Problem Solving • That is, we write a step-by-step process to solve our problem • Recall the properties of an algorithm: • Finiteness – terminates properly • Definiteness – each step is precisely stated • Effective Computability – it can be carried out by a computer or Turing machine
Problem Solving • For this course, we will use the concept of structured programming • This originated in the 1960s as a way describing a complex system and decomposing it into smaller, more manageable tasks • Sometimes we call this systematic decomposition
Problem Solving • We will use systematic decomposition as a way to design computer programs that are more complex • This technique is useful at any level
Three Constructs • The main idea behind systematic decomposition is taking a task and breaking it into smaller units • These units, when put together, work the same as the larger task • We can repeat the process over and over • Works well with extremely complex tasks
Three Constructs • This process is sometimes referred to as stepwise refinement • That is, we take each larger step and refine it into smaller, more detailed tasks • The smaller tasks are generally simplier that the larger task
Three Constructs • There are three constructs for doing this: • Sequential • Conditional • Iterative
Three Constructs • The sequential construct is the one to use if the task can be broken into two subtasks, one following the other • The first task executes, then the second one happens • Many times, the first task computes something that is needed by the second task • There is no repetition here!
Three Constructs • The conditional construct tests a condition and performs one of two subtasks depending on the result • If condition is true, we perform one task • Otherwise, there is another task that we perform
Three Constructs • This correlates with an if-else statement • Either task may be vacuous • There is no repetition of the check or the tasks
Three Constructs • The iterative construct is used when there needs to be a task run multiple times • First, a condition is tested to determine where the iteration should continue • If so, the subtask runs • Otherwise the iteration is done
Three Constructs • After the subtask is finished, the condition is rechecked • Then, the process repeats until the condition is false • This is similar to a while() loop
Using the LC-3 with Constructs • We will now illustrate how the control instructions on the LC-3 can implement this three constructs • How do we use LC-3 instructions to encodethe three basic constructs? • Sequential • Instructions naturally flow from one to the next,so no special instruction needed to gofrom one sequential subtask to the next.
Using the LC-3 with Constructs • 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.
Using the LC-3 with Constructs PC offset toaddress C Exact bits depend on condition being tested Unconditional branchto Next Subtask PC offset toaddress D Assuming all addresses are close enough that PC-relative branch can be used.
Using the LC-3 with Constructs PC offset to address C Exact bits depend on condition being tested Unconditional branchto retest condition PC offset toaddress A Assuming all addresses are on the same page.