Performing Multiplication in Assembly Language

Name
Date
Little Man Computer
The Little Man Computer is a simulator of a CPU developed at M.I.T. in the 1960s to illustrate how
the Fetch Execute Cycle executes instructions at the Assembler level in a Von Neumann style CPU
processor.
Write here a brief description of who Von Neumann was and what he did in micro processor
research
Assembly language is unlike higher level languages (such as Python, Pascal, VB…) in that it has a
limited number of Mnemonic instructions available to it.
A CISC processor has more instructions available than a RISC processor.
Find out the names of some different types of RISC processors and its uses
What are the key factors in determining the speed of a processor and what levels of cache are
there?
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Assembly language is processor specific. Each family of processor has its own instruction set so an
assembly language code is not portable in the way high level language code is.
One of the LMC simulators available is available here http://peterhigginson.co.uk/LMC/
Mnemonic
ADD
Op Code
1xx
SUB
2xx
INP
901
OUT
902
STA
3xx
LDA xx
5xx
HLT
000
Assembly Language A Level Computer Science Worksheet
Description
Add the contents of the IAS
address specified to the
contents of the accumulator
(this uses the ALU to perform
the calculation).
Subtract the contents of the IAS
address specified from the
contents of the accumulator
(this uses the ALU to perform
the calculation).
Input a value and store it in the
accumulator register
Display the contents of the
accumulator register
Store a value from the
accumulator into the IAS
address specified
Load the accumulator with the
contents of the IAS address
specified
Stop the program
@CompSciTeaching
These are some basic assembler codes. When compiling a high level language such as Python it is
first compiled into assembler, then compiled into machine code. These are binary numbered op
codes (operation codes). These op codes are stored in RAM locations and fetched, then executed by
the processor. This is the fetch execute cycle.
Consider this simple python code
x = int(input("Enter a number: "))
print(x)
This is portable code meaning it can be compiled and executed on any family of processor on a
machine that can run Python.
The compiler turns this into processor specific assembly language and then assembler then turns it
into machine code (pure binary numbers). These are opcodes.
In assembly language this would look like…
x
INP
STA x
OUT
HLT
DAT
Try entering this into LMC and seeing what happens.
Remember identifier names for variables can be anything you want them to be, buts its always
useful to refer to them as meaningful names that are descriptive about their use and the data they
are storing. This makes reading code much easier.
Now consider this Python code
numa = 12
print (numa)
This uses the variable identifier numa as a symbolic memory location to store the integer value 12.
Assembly language can also use symbolic addressing. In LMC this would look like
LDA numa
OUT
HLT
numa DAT 12
Notice the numa declaration sets the input as 12. It does not require a user input.
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Consider this python code. This performs an addition.
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
sum = x+y
print(sum)
This is the pseudo code for assembly language to add 2 numbers together
INP a value
STA Store it under a label identifier
INP another value
ADD this value to the first value
OUT Output the value
HLT Stop the program
Declare the labels
You can enter these commands in the left hand side panel or you can enter the opcodes directly into
the RAM.
Task
Create an LMC program that allows the user to adding 3 numbers together. The user should input all
3 values.
Performing Multiplication in Assembly Language
As we have seen the only operations a processor performs is addition or subtraction. So
how would a multiplication operation be performed?
This would be achieved through a series of additions. For example..
5x4 = 20
This is same as performing
5 + 5 + 5 + 5 = 20
So we need to take the first operand (5) and add that value to itself by the number of the
second operand (4). So 5 will be added 5, 4 times in sequence.
How would this work in LMC?
Firstly we can declare 3 labels – num1 and num2 and given them values and a third label
called result (or whatever..) to store the accumulated result of the additions.
You will need a loop, a BRA and a BRZ operation to break the loop. Here is a rough guide
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Start Loop
Add the value of num1 to result
Store the result
Subtract 1 from num2
Store num2
When num2 reaches 0 then break the loop
End Loop
Output result
Can you alter the code to allow the user to enter the values for num1 and num2?
Assembly Language Selection
Unlike in higher level languages you can’t use comparison operators in an IF statement.
Consider this simple code…
numa = int(input("Enter a number: "))
numb = int(input("Enter a number: "))
if (numa>numb):
print(numb)
If numb is smaller it will output numb. Alter the Python code so it will output the higher of the 2
inputs.
Assembly language cannot perform the comparison operation in this manner so it subtracts the 2
numbers to see if the sum is either positive or negative to determine which one is higher.
The instructions set commands used to do this are…
Mnemonic
BRA
Op Code
6
BRP
7
BRZ
8
Assembly Language A Level Computer Science Worksheet
Description
Branch
Use label or the IAS address
specified as the address of the
next instruction
Branch if Positive
Use label or the IAS address
specified as the address of the
next instruction if the contents
of the Accumulator is a positive
number
Branch if Zero
@CompSciTeaching
Use label or the IAS address
specified as the address of the
next instruction if the contents
of the Accumulator is 0
Here is some code – try entering it and working it out..
INP
STA numa
INP another value
STA numb
SUB numa
BRP label1
LDA numa
BRA label2
label1
LDA numb
label2
OUT
HLT
numa
DAT
numb
DAT
Label1 and label2 are label identifiers. These are like a variable identifier in Python. Notice what
happens to the contents of the Program Counter when it encounters a branch instruction
This code will output the higher of the 2 numbers – how can you alter it to output the lower of the 2
numbers?
Assembly Language Iteration
In higher level languages we code loops using either count loops (FOR) or condition loops (WHILE
DO or REPEAT UNTIL)
Consider this Python code…
for i in range(10,0,-1):
print(i)
This will print the variable i from the range 10 down to (but not including) 0. Try it and see.
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
To produce this countdown effect in Assembly language we cannot use a For Loop, but instead need
to perform a series of subtractions. Remember a processor is only capable of addition and
subtraction (but it performs these operations very quickly!).
The pseudo code to produce the same output in LMC would look like the following
loop
x = 10
sub 1
store x
output x
Branch if Zero to STOP
Branch to loop
STOP halt
Declare the labels x
Declare the labels for 1
You will have to declare a label (in this case x) and give it the value of 10. The subtract 1 from it and
store it again. The loop will continue only when the Branch Zero operation occurs. This will goto the
STOP label when the value of the accumulator reaches 0. Why do you need to declare a label for 1
when you sub it?
Try writing this in LMC and test it. Can you alter the code so the user inputs the value of x?
Creating Validation in Assembly Language using Iteration
Consider this Pseudo code. Write this in Python and test it.
Start Loop
Enter number
If number > 100 then output number
End loop when number > 100
This is a simple validation code to enter a number larger than 100. Write it in Python and test it.
How would this convert into Assembly Language? As with Selection you cannot use a comparison
operator so instead will have to input the number from the user and do a subtraction to see if you
get a positive or negative number. Then use a BRP command.
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Try creating the following program in LMC…
Input a Number
Check to see if the number if greater than 100
If the number is greater than 100 output the number
If number is less than 100 then output 0
Use symbolic addressing wherever possible in your LMC code.
Now you need to create a loop to validate the input. To do this in LMC you use a label. For example
loop INP
Rest of code here...
BRP loop
Rest of code here...
Write the python code to enter a number > 100. Screen shot to prove it works. Then write the same
code in LMC. Screen shot the code. Brief explanations then print out and submit please. No more
than 2 sides is required. Put your name in the header.
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Creating a Fibonacci Sequencer in Assembly Language
The most common ways to illustrate iteration is with a program that generates Factorials and
Fibonacci Numbers. If you don’t know what these are or how they relate to the Golden Ratio then
find out!
Fibonacci numbers work as below….
num1
num2
num3
0
+
1
=
1
1
+
1
=
2
1
+
2
=
3
2
+
3
=
5
3
5
8
13
21
+
+
+
+
+
5
8
13
21
34
=
=
8
13
21
34
55
Can you see the pattern?
num1 becomes num2, num2 becomes num3.
How can you create this in Assembly Language using LMC? You will need to output the result of each
sum so it appears in output window.
You will need to use 3 labels as variables num1, num2, num3
Store the value 0 and 1 in num1 and num2 respectively
Then what? Try and work it out.
You will need to use a loop to perform the recursion.
Extension Tasks
Can you…
Output the first 10 numbers in the Fibbonaci sequence. You will need to use a countdown – here is
how you would do it…
Create 2 labels such as…
number
dat 10
one
dat 1
To perform a countdown you need to do the following…
lda number
sub one
sta number
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
You then need a BRZ and a BRA as with the previous exercise.
Instead of counting down from 10 could you also modify the code so that the user enters the
number of Fibonacci numbers to output? You will need to input a value a store it under the number
label.
What to print out and put in your folder.
Screen shot of LMC producing Fibonacci numbers along with the assembly language code and a brief
explanation of how it works. This can be an infinite loop.
Screen shot of LMC producing the first 10 Fibonacci numbers along with the assembly language code
and a brief explanation of how it works .
Screen shot of LMC producing Fibonacci numbers that allows the user to enter how many they wish
to display, along with the assembly language code and a brief explanation of how it works .
This should be put in your folder along with your other LMC assignments.
Questions on LMC/Assembly Language are guaranteed to be in the exam so
understanding how this works is critical to answering these questions correctly.
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
Types of Languages Revision
High level Language
Problem oriented. Built in mathematical functions. Portable
across platforms. Uses library routines. Easy to maintain. Python,
C, Pascal, VB….
Low level Language
Assembly or Binary coding. Reflects design of processor.
Processor dependent. Allows for direct access to memory
locations. Direct coding of processor and hardware. Used to
program device drivers and other embedded systems.
Assembly Language
Low level language. A language related closely to the computer
being programmed/low level language/machine specific. Uses
descriptive names and mnemonics. Machine orientated. Related
to design of the computer/processor.
Assembler
Turns Assembly language into machine code. Reserves storage
for instructions and data. Replaces mnemonic opcodes to
machine codes instructions. Replaces symbolic addresses
(variable names) to numeric addresses in RAM. Creates symbol
table. Checks syntax. Error diagnostics.
Machine Code
Low level language. Binary notation. Set of all instructions
available to the architecture which depend on the hardware
design of the processor. Harder to code – pure 0s and 1s
Why use Low Level Languages?
They allow direct access to the processors registers and IAS locations. This means very fast processing
for big data sets.
Hardware drivers and embedded systems (eg the coding that goes into a washing machine) are
written to directly access the hardware for those devices.
Does not mean that a compiler is required which makes for more much faster and efficient use of
processor time.
Assembly Language vs Machine Code

Assembly Language
◦ A language related closely
to the computer being
programmed/low level
language/machine specific
◦ Uses mnemonics for
instructions
◦ Uses descriptive names for
data stores (symbolic
addressing)
◦ Translated by an assembler
into machine code
◦ Easier to write than
machine code but more
difficult than high level
language
Assembly Language A Level Computer Science Worksheet

Machine code
◦ Written in Binary (or
hexadecimal)
◦ No translation needed
◦ Set of all instructions
available to the
architecture which
depend on the hardware
design of the processor
◦ Very difficult to write
@CompSciTeaching
Assembly Language Test
H446 L6 Computing
1) Which register stores the result of ALU operations?
(1)
2) Which register stores the location in RAM of the next instruction to be executed?
(1)
3) What unit is the clock speed of a processor measured in?
(1)
4) The use of labels and identifiers to refer to memory locations is referred to as what addressing
type?
(1)
5) Describe what is meant by the term high level language?
(3)
6) Describe what is meant by the term low level language?
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
(3)
7) Describe two advantages of using a low level language over a high level language
(4)
Consider the code below…
INP
STA NUMA
INP
STA NUMB
SUB NUMA
BRP LABEL
LDA NUMB
BRA STOP
LABEL
LDA NUMA
STOP
OUT
HLT
8) Describe one reason why this code will not assemble and what lines need to be added in order for
it to execute
(2)
9) Using the values 6 for NUMA and 12 for NUMB describe what operation this code performs and
what value will be outputted
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
(2)
The following code is designed to output a countdown from the value of x down to 0
loop
LDA x
SUB one
STA x
OUT
BRA loop
HLT
x
DAT 10
one
DAT 1
10) State one reason why this code will not execute correctly and how you would alter it and what
lines you would add to make it execute correctly.
(2)
The following code is intended to check if 2 values are the same and output them if they are. It will
output 0 if the values are different.
LDA NUMA
SUB NUMB
BRP LABEL
LDA ZERO
BRA STOP
LABEL
LDA NUMA
STOP
OUT
HLT
NUMA
DAT 4
NUMB
DAT 4
ZERO
DAT 0
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
11) At present the code will not execute as intended which line is incorrect and state what the
correct line should read
(2)
12) This code needs to be altered so that the user inputs the values for NUMA and NUMB. What
additional lines should be added to the code and what lines need to be altered.
(4)
13) Write the pseudo code for a higher level language that will perform this operation (input 2
numbers, if the values are the same output the number, otherwise output a 0).
(4)
Total
/ 30
%
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
ANSWER SHEET
1) accumulator
2) program counter
3) hertz
4) symbolic addressing
5) Problem oriented. Built in mathematical
functions. Portable across platforms. Uses
library routines. Easy to maintain and code.
6) Assembly or Binary coding. Reflects design
of processor. Processor dependent. Allows
for direct access to memory locations. Direct
coding of processor and hardware. Used to
program device drivers and other embedded
systems.
7) Direct access to the processors registers
and IAS locations. Very fast processing for big
data sets. Hardware drivers and embedded
systems written to directly access the
hardware for those devices. No compiler is
required - faster/efficient use of processor
8) NUMA NUMB not declared as labels
NUMA DAT
NUMB DAT
9) 6 - Find lowest number
10) No break out of the loop. BRZ to OUT
using suitable label
11) BRP LABEL
BRZ LABEL
12) INP
STA NUMA
INP
STA NUMB
NUMA DAT
NUMB DAT
13) NUMA = input (Enter number A)
NUMB = input (Enter number B)
If NUMA=NUMB
print(NUMA)
ELSE
print(0)
Assembly Language A Level Computer Science Worksheet
1) accumulator
2) program counter
3) hertz
4) symbolic addressing
5) Problem oriented. Built in mathematical
functions. Portable across platforms. Uses
library routines. Easy to maintain and code.
6) Assembly or Binary coding. Reflects design
of processor. Processor dependent. Allows for
direct access to memory locations. Direct
coding of processor and hardware. Used to
program device drivers and other embedded
systems.
7) Direct access to the processors registers and
IAS locations. Very fast processing for big data
sets. Hardware drivers and embedded systems
written to directly access the hardware for
those devices. No compiler is required faster/efficient use of processor
8) NUMA NUMB not declared as labels
NUMA DAT
NUMB DAT
9) 6 – Finds the lowest number
10) No break out of the loop. BRZ to OUT using
suitable label
11) BRP LABEL to
BRZ LABEL
12) INP
STA NUMA
INP
STA NUMB
NUMA DAT
NUMB DAT
13) NUMA = input (Enter number A)
NUMB = input (Enter number B)
If NUMA=NUMB
print(NUMA)
ELSE
print(0)
@CompSciTeaching
LMC CODES
##### Countdown from num downto 1 ####
loop
lda num
out
sub one
sta num
brz stop
bra loop
stop
hlt
num
dat 10
one
dat 1
#### Input 2 numbers - output the highest number ####
inp
sta numa
inp
sta numb
sub numa
brp label1
lda numa
bra label2
label1 lda numb
label2 out
hlt
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
numa dat
numb dat
#### Input 2 numbers - output the lowest number ####
inp
sta numa
inp
sta numb
sub numa
brp label1
lda numb
bra label2
label1
lda numa
label2
out
hlt
numa
dat
numb
dat
#### Input 2 numbers - outputs 1 if the numbers are the same, outputs 0
otherwise ####
inp
sta numa
inp
sta numb
sub numa
brz label1
lda numdiff
bra label2
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
label1
lda numsame
label2
out
hlt
numa
dat
numb
dat
numsame dat 1
numdiff dat 0
#### Validation for number more than equal to 100 ####
loop
inp
sta numa
sub validate
brp label1
brz label1
bra loop
label1
lda numa
out
hlt
validate dat 100
numa
dat
### Outputs number if number is 101 of more - otherwise loops
#### Validation for number less than 100 ####
loop
inp
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
sta numa
sub validate
brp loop
bra label1
label1
lda numa
out
hlt
validate dat 100
numa
dat
## Only allows a number less than 100 to be entered
#### Fibbonaci Sequencer#####
inp
sta number
loop
lda a
add b
out
sta c
lda a
sta b
lda c
sta a
lda number
sub one
sta number
brz stop
bra loop
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
stop
hlt
a
dat 0
b
dat 1
c
dat 0
number
dat
one
dat 1
##### Multiplier ########
lda num1
loop
lda result
add num1
sta result
lda num2
sub one
sta num2
BRZ STOP
BRA loop
STOP
lda result
OUT
HLT
num1
DAT 4
num2
DAT 5
one
DAT 1
result
dat
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching
##### Multiplier with user input ########
inp
sta num1
inp
sta num2
lda num1
loop
lda result
add num1
sta result
lda num2
sub one
sta num2
BRZ STOP
BRA loop
STOP
lda result
OUT
HLT
num1
DAT
num2
DAT
one
DAT 1
result
dat
Assembly Language A Level Computer Science Worksheet
@CompSciTeaching