Assembly Language

Assembly language is a low-level programming language - it is closer to machine code (binary) than high-level programming languages like Python.

Assembly language uses mnemonics (abbreviations of commands) to signify instructions; for example, input is written as INP and output is written as OUT.

Little Man Computer is a representation of assembly language. This simulator will help you understand assembly language and allow you to check if your instructions are correct.

Assembly Language Mnemonics

INP

(Input)

INP is used to input a number. The number is temporarily stored in the accumulator.

OUT

(Output)

OUT is used to output the number currently stored in the accumulator.

STA

(Store)

STA stores the value that is currently in the accumulator. It can be used to assign a value to a variable.

ADD

(Addition)

ADD is used to add a number to the value currently stored in the accumulator.

SUB

(Subtraction)

SUB takes away a number from the value currently stored in the accumulator.

LDA

(Load)

LDA is used to load the value of a stored variable back into the accumulator

BRZ

(Branch if Zero)

BRZ is used to loop only if the value in the accumulator is currently 0.

BRP

(Branch if Positive)

BRP is used to loop only if the value in the accumulator is currently positive (including 0).

BRA

(Branch Always)

BRA is used to loop continuously.

HLT

(Halt)

HLT will stop running the program. Every program MUST have a HLT command.

DAT

(Data Definition)

DAT must be used to define a variable name (and / or set it with a starting value). Data definitions must be written at the end of the instructions.

Peter Higginson's Little Man Computer simulation

Examples of Simple Assembly Language Programs

#1 - Input & Output

Program Purpose: Input a number, store the number as a variable called Number1 and output the number.

1. Lets the user input a number

3. Outputs the value in the accumulator - which will be the number that was just inputted.

5. Defines a variable called 'Number1'. This has to be at the end of the program and you must write the variable name first, not the command first.

INP

STA Number1

OUT

HLT

Number1 DAT

2. Stores the number in a variable named 'Number1' - there must be no spaces in a variable name.

4. Halts (stops) the program.

Type these instructions line by line into the Little Man Computer simulator to see how it works.

#2 - Addition

Program Purpose: Input and store two numbers. Add them together. Output the total.

1. Lets the user input a number

3. Lets the user input another number

5. Adds number1 to the value in the accumulator (which is currently number2 as you just inputted it).

7. Halts the program.

Type these instructions line by line into the Little Man Computer simulator to see how it works. Then change the program to subtract the number instead.

INP

STA Number1

INP

STA Number2

ADD Number1

OUT

HLT

Number1 DAT

Number2 DAT

2. Stores the inputted number in a variable named 'Number1'.

4. Stores the inputted number in a variable named 'Number2'.

6. Outputs the value in the accumulator (which is now number1 added to number2.

8. & 9. The two variables Number1 and Number2 are defined on separate lines.

#3 - Load in Order

Program Purpose: Input and store three numbers. Load and output them in the order that they were entered.

1. - 6. Lets the user input three numbers and immediately stores each one as they are entered.

8. Now that Number1 has been loaded into the accumulator, this value is outputted.

13. Halts the program.

Type these instructions line by line into the Little Man Computer simulator to see how it works. Let the user input a fourth number and output this fourth number last.

INP

STA Number1

INP

STA Number2

INP

STA Number3

LDA Number1

OUT

LDA Number2

OUT

LDA Number3

OUT

HLT

Number1 DAT

Number2 DAT

Number3 DAT

14. - 16. The three variables Number1, Number2 & Number3 are defined on separate lines.

9. - 12. Number2 is loaded and output then Number3 is loaded and output

7. Once all three numbers have been inputted and stored, the first number is loaded back into the accumulator.

#4 - Branching

Program Purpose: Input and store two numbers. Output the largest number. (Branching required).

1. - 4. Lets the user input two numbers and immediately stores each one as they are entered.

7. BRP is 'Branch is Positive'. If the result of Number1 - Number2 is positive then the program will jump to line 11. You can write any value instead of 'loop', such as 'jump' or 'break'. If the result is not positive it will continue to the next line.

11. - 13. The program will jump to line 11 if the result of Number1 - Number2 is positive. This means that Number1 is larger than Number2 so Number1 is loaded and output then the program is halted. 

INP

STA Number1

INP

STA Number2

LDA Number1

SUB Number2

BRP loop

LDA Number2

OUT

HLT

loop LDA Number1

OUT

HLT

Number1 DAT

Number2 DAT

5. & 6. Loads Number1 and subtracts Number2 from it.

8. - 10. The program will continue to line 8 if the result of Number1 - Number2 is not positive. Because the result is a negative number, this tells us that Number2 is larger than Number1. So we load Number2, output it because it is bigger, then halt the program.

14. - 15. The variables Number1 & Number2 are defined on separate lines.

Type these instructions line by line into the Little Man Computer simulator to see how it works. Change the program so that the smallest number is output.

© CSNewbs 2020