Assembly language Lesson 2: Looping

In the previous lesson we wrote some simple programs in assembly language using Little Man Computer (LMC). We will now move on to create more programs making use of the branching instructions in LMC assembly language to repeatedly run sections of code (iteration/looping).

Iteration in assembly language

In high level languages loops come in 3 types:

  • for loop (iterates for a fixed number of times)
  • while loop (iterates based on a condition at the the top of the loop)
  • do…while loop (iterates based on a condition at the end of the loop)

In assembly language programming, you need to use one or more branch instructions to achieve the same result.

With LMC it is a little harder than in real CPUs because we only have two conditional branch instructions: BRZ and BRP. BRZ will branch when the accumulator contains zero, BRP will branch if the accumulator contains a positive number (or zero, which is not a negative number obviously).

Activity 1

      INP
      STA num
top   ADD num
      OUT
      BRA top
      HLT
num   DAT

What do you think the above program will do?

Enter the code into LMC and run it to see if you are right.

Activity 2

          INP
          STA bigNum
          INP
          STA littleNum
loopStart LDA bigNum
          SUB littleNum
          STA bigNum
          OUT
          BRZ end
          BRA loopStart
end       HLT
bigNum    DAT
littleNum DAT

Enter and run the program above. Input the values 12 and 3 and use the trace table below to record the output each time round the loop until the program ends. The first 5 rows are complete. Give yourself plenty of space as you will need a lot of rows!

Hint: use the step button repeatedly rather than the run button so you can run a single instruction at a time.

PCInstructionACCINOUTbigNumlittleNum
0INP12
1STA bigNum1212
2INP3
3STA littleNum33
4LDA bigNum12
5
6
7
8
9
4
Copy this trace table into your book and complete the rows. You may not need to change a value in every column on each row.

Activity 3

Write a program which keeps a running total of all numbers input by a user. The program should end when the user inputs a 0, and should display the final total when that happens.

Draw a flowchart design for this program in your book.

Test it with the inputs 3, 5, 9, 12, 0 which should produce an output of 29.

Challenge 1

Ask the user to input two numbers, a big number and a small number. Your program should repeatedly subtract the small number from the bigger number until the result becomes negative.

Output the negative number when the program finishes.

Test the program with the values 12, 3 (should give -3) and 16, 3 (-2)

Challenge 2

Adapt the program above so that it counts how many times the smaller number was subtracted from the bigger number.

Output the count when the program finishes.

Test the program with the values 12, 3 (should give 5) and 16, 3 (6)

Challenge 3

Adapt the program above one final time. You may have noticed that the program is almost functioning as an integer divide. However it is slightly out.

Adapt the program so that it outputs the quotient of bigger number and little number and the remainder of bigger number and little number.

Test the program with values 12,3 (should give 4 and 0) and 16,3 (5 and 1).

Exam style question

        INP
        STA arg1
        INP
        STA arg2
        LDA arg1
loop    SUB arg2
        BRP loop
        ADD arg2
        OUT
        HLT
arg1    DAT
arg2    DAT
  1. What would the above program output if the numbers 13 and 3 were entered?
  2. What would the above program output if the numbers 16 and 4 were entered?
  3. What useful maths operation does the above program perform?
  4. Write the equivalent to the above program in a high level language or pseudo code.

Lesson 3 (A Level only)