Basic assembly code programming is part of the A-level Computing syllabus in the UK. For many programmers the thought of writing in assembly code is extremly tedious. However, the pupils seem to actually quite like it. They like breaking the problems down into their absolute basic components. The excellent Little Man Computer designed by Stuart Madnick models a basic Von Neumann Arctitechture in a web browser. It has an instruction set of only 9 commands so is easy to get started on. I've included some examples of programmes below. They can also be found on my github.
two numbers
Write a program that will prompt for 2 numbers, subtract the first from the second and output the answer, then subtract the second from the first and output the answer.
INP
STA 90
INP
SUB 90
OUT
one to ten up
Write a program to output the numbers 1 to 10 in ascending order.
LDA one
STA count
OUT
loopstart LDA count
ADD one
OUT
STA count
SUB ten
BRP loopend
BRA loopstart
loopend HLT
one DAT 1
ten DAT 10
count DAT
one to ten down
Write a program to output the numbers 1 to 10 in descending order.
LDA ten
STA count
OUT
loopstart LDA count
SUB one
BRZ loopend
OUT
STA count
BRA loopstart
loopend HLT
one DAT 1
ten DAT 10
count DAT
count in twos
Write a program to input a number then count up to that number in steps of 2, outputting the sequence.
INP
STA input
loopstart LDA count
ADD two
OUT
STA count
SUB input
BRP loopend
BRA loopstart
loopend HLT
one DAT 1
two DAT 2
input DAT
count DAT
multiply two numbers
Write a program that will input two numbers and multiply them.
INP
STA input1
INP
STA input2
loopstart LDA output
ADD input2
STA output
LDA count
ADD one
STA count
SUB input1
BRP loopend
BRA loopstart
loopend LDA output
OUT
HLT
input1 DAT
input2 DAT
count DAT
output DAT 0
one DAT 1
multiply two numbers extended
Extend the program above that it will let the user repeatedly input and multiply pairs of numbers, only stopping if a zero is entered.
start LDA zero
STA output
STA count
INP
BRZ end
STA input1
INP
BRZ end
STA input2
loopstart LDA output
ADD input2
STA output
LDA count
ADD one
STA count
SUB input1
BRP loopend
BRA loopstart
loopend LDA output
OUT
BRA start
end HLT
input1 DAT
input2 DAT
count DAT
output DAT
one DAT 1
zero DAT 0
convert integer to binary
Take as input a positive whole number and output the binary equivalent
start INP
STA 99
loopstart SUB one
BRZ loopend
ADD one
BRZ loopend
SUB two
STA 99
LDA count
ADD one
STA count
LDA 99
BRA loopstart
loopend LDA 99
OUT
LDA count
BRZ end
STA 99
LDA reset
STA count
LDA 99
BRA loopstart
end HLT
two DAT 2
one DAT 1
count DAT 0
reset DAT 0
fibonacci
Take as input the number of terms in the sequence to calculate after 1,1. Output the terms in the sequence 1,1,2,3,5,8,13...
start INP
STA count
LDA one
STA 98
STA 97
OUT
OUT
loopstart LAD 97
ADD 98
STA 99
OUT
LDA 98
STA 97
LDA 99
STA 98
LDA count
SUB one
STA count
BRZ loopend
BRA loopstart
loopend HLT
one DAT 1
count DAT 0
Comments