Assembly Language Programming – Lesson 2

Learning Objectives

  • To learn more advanced assembly language programming techniques
  • To understand how a program is processed by the central processing unit (CPU)

Learning Outcomes

All must copy the example LMC decision making program.  With lots of help come up with working solutions for problems 1 and 2.  Taken screenshots of the code and a test result. (Grade D/E)

Most should copy the example LMC decision making program.  Come up with working solutions for problems 1 and 2 with not much help.  Taken screenshots of the code and a test result. (Grade B/C)

Some could copy the example LMC decision making program.  Come up with working solutions for problems 1, 2 and 3 mostly independently.  Taken screenshots of the code and a test result. (Grade A*/A)

Keywords

Words to learn: assembly, language, programming

Starter

In the last lesson you created some programs in assembly language that were executed in order – in other words do one instruction, then the next and so on.  Central Processing Units (CPUs) are capable of making decisions as well.  This is known as branching.

The Little Man Computer (LMC) supports the following branching instructions:

Instruction Mnemonic What does it do?
Branch If Zero BRZ Jump to the RAM location specified if the answer in the accumulator is zero
Branch If Positive BRP Jump to the RAM location specified if the answer in the accumulator is more than zero
Branch Always BRA Jump to the RAM location specified always (used for loops)
Label DAT Can be used to give names instead of RAM addresses

We are going to create the program below:

Ask for a number
Store it in RAM location 90
Ask for a number
Subtract the accumulator from the value in RAM location 90
If the answer is zero then jump to the named location answerIsZero
Stop
answerIsZero: Ask for a number
Output number
Stop

In assembly language this becomes:

INP
STA 90
INP
SUB 90
BRZ answerIsZero
HLT

answerIsZero INP
OUT
HLT

answerIsZero DAT

Try it out (click here to open the LMC) and see if you can get it to work.  What should happen is that if you type in two numbers that are the same then the program will ask for another number and display it on screen.  If the two numbers are not the same then the program will simply stop.

Main – Challenges

Create a Little Man Computer (LMC) program for each of the problems below.  For each problem take a screenshot of your finished code and show the results of a test run to make sure it works!

Problem 1

Ask the user for 2 numbers. If the numbers are the same then double the number and display them. If they are different then display both of the numbers.

Inputs Outputs
15,15 30
12,9 12,9

Problem 2

Ask the user for 2 numbers. Display the biggest, then the smallest.

Inputs Outputs
12,15 15,12
7,2 7,2

Problem 3

Ask the user for 2 numbers, display the result of the biggest number minus the smallest number

Inputs Outputs
7,3 4
5,12 7

Plenary

Come up with a problem of your own that uses branching and challenge the person sat next to you to come up with a Little Man Computer program to solve it.

Challenges based upon the LMC Booklet produced by Mark Clarkson from Computing At School