The LC-3 PowerPoint PPT Presentation

presentation player overlay
1 / 11
About This Presentation
Transcript and Presenter's Notes

Title: The LC-3


1
The LC-3 Chapter 5
  • COMP 2620

2
Methods of Loop Control
  • Recall from last class, we designed a loop to
    implement the addition of 12 integers
  • That is, a loop is a sequence of instructions
    that executed a number of times in repetition
  • The code the executes in the loop is called the
    body of the loop
  • Each time the loop executes, this is called one
    iteration

3
Methods of Loop Control
  • There are two common methods for controlling
    loops that are used in programming
  • You probably have seen both of these in your
    C/C/Java courses in some form

4
Methods of Loop Control
  • The first method is the one we used last class
  • It requires a counter variables and the loop
    terminates when the counter variable reaches a
    certain value
  • That is, we execute the loop n times by setting a
    counter to n and decrementing each time

5
Methods of Loop Control
  • Then, we of course check to see when the loop
    counter reaches 0.
  • If it is not zero, we set the PC to the start of
    the loop and continue with another iteration
  • Alternatively, we could start the loop counter at
    0 or 1 and increment up to n

6
Methods of Loop Control
  • The second method of loop control is called a
    sentinel
  • This is usually used when we do not know ahead of
    time how many iterations we need to run
  • We choose a value that can never occur in
    processing to indicate termination of the loop
  • This value is called the sentinel

7
Methods of Loop Control
  • For a sequence of positive numbers this could be
    a negative value
  • For a list of pointer values, it could be NULL
    value for the current pointer
  • When we find this value, the loop terminates

8
Sentinel Example
  • We consider a similar example to adding the 12
    integers, but this time we assume the numbers are
    all positive
  • In this case, we could choose any negative value
    for the sentinel
  • As convention many times, we choose the number -1

9
Sentinel Example
  • Pseudocode
  • addr 0x3100
  • sum0
  • Load value from memoryaddr
  • Is value the sentinel? if yes, end loop
  • Sumsumvalue
  • addraddr1
  • Load value from memoryaddr

10
Sentinel Example
11
Sentinel Example
Address Instruction Comments
x3000 1 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1 R1 ? x3100 (PC0xFF)
x3001 0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3 ? 0
x3002 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 R4 ? MR1
x3003 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 BRn 0x3008
x3004 0 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0 R3 ? R3 R4
x3005 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1 ? R1 1
x3006 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 R4 ? MR1
x3007 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 BRnzp 0x3003
Write a Comment
User Comments (0)
About PowerShow.com