Assembly language hacked by 14-year-olds

The following two programs are very impressive examples of independent work produced by two 14-year-old boys using my Little Man Computer simulator.

The first one by Eric is an integer division program that can deal with both negative and positive integers and which returns both the whole number division and the remainder.  The second program, written by Will, is a general purpose calculator that can deal with addition, subtraction, multiplication, integer division, square roots and raising one number to power.

Not bad stuff for a program that can only ADD and SUBTRACT(!)

Eric's division program first.  Enter two numbers X and Y respectively and the program calculates X DIV Y and X MOD Y.


#DIVISION with remainders and negatives!

#By Eric
.
INP
STA TOP #Input the top part of the fraction, e.g. [8]/3
BRZ TOPZERO #If top zero simulate input and then
#output 0
INP
BRZ HALT #If zero, just go. DIVBYZERO
STA BOTTOM #Input the bottom part of the
#fraction, e.g. 8/[3]
BRA NCHKO
.
TOPZERO INP #sim input
LDA SWAP #load swap (0 by now)
OUT #outputs it
HLT #and stops!
.
NCHKO BRP NCHKT #check bottom for efficiency
SUB BOTTOM #use clever maths to realize that x-(x-x) = -x
SUB BOTTOM #if the numbers weren't positive it would
STA BOTTOM #mess the program up
LDA MINUSONE #negmult is now -1, so it's negative
STA NEGMULT
BRA NCHKT
.
NCHKT LDA TOP #check top now
BRP START #if not negative just go to setup
SUB TOP #same clever maths as before
SUB TOP
STA TOP
LDA NEGMULT #loads the negative multiplier
SUB NEGMULT
SUB NEGMULT
STA NEGMULT
#and uses SAME clever maths trick to invert, again
LDA TOP
BRA START
.
.
.
START SUB BOTTOM
#alright so I better explain
#the thing is that the way I divide is:
#I count how many times I subtract the bottom from the top
#When the top is negative it checks for a remainder
#by adding the bottom on again
#that is the remainder
#so example:
#14/3 - it subtracts 3 from 14 5 times
#and then adds 3 again, takes one away from the
#5 times (makes it 4) and the remainder is the result after
#adding 3. 4r2 which is the correct answer.
BRP NFINISH #if positive NotFINISHed
BRA FINISHUP#else finish all things up
NFINISH STA SWAP #stores current result in swap
LDA RESULT #loads the result
ADD ONE #increments
STA RESULT #stores result
LDA SWAP
BRA START #loads swap and removes and starts again
.
.
.
FINISHUP ADD BOTTOM #adds bottom to figure out remainder
STA REMAINDER #stores that as remainder
LDA NEGMULT #remember this?
BRP OUTPUT #if negmult is normal just output
LDA RESULT
SUB RESULT #else use my favourite maths trick
SUB RESULT
STA RESULT
OUTPUT LDA RESULT #easy peasy
OUT #output result
LDA REMAINDER
OUT #remainder
HALT HLT #cya 

#Variables
NEGMULT DAT 1 #Negative multiplier
MINUSONE DAT -1 #-1 [CONST]
ONE DAT 1 #1 [CONST]
TOP DAT 0 #The top part of fraction
BOTTOM DAT 0 #Botttom part
RESULT DAT 0 #Result
REMAI DAT 0 #Remainder
SWAP DAT 0

#Swap data used by program for general stuff




Here is Will's general purpose calculator.  The first number input is the menu option:
0: for square root
2: for division
3: for subtraction
4: for addition
5: for powers
6: for multiplication


#Will
#A general purpose calculator which works with the limitations of the little man computer
#
INP # Press zero for square root, two for divide, three for subtract, four for addition and five for power, six for multiply
STA D
INP #Enter the first number, this is the number that all the functions will be done to
STA A
INP #With square and power, due to limitations of little man computer size, does nothing but is the operating number
STA B
LDA D
BRZ SQUARE #Go to square function
SUB ONE
BRZ DIVIDE #Go to multiply function
SUB ONE
BRZ SUBTRACT #Go to subract function
SUB TWO
BRZ ADDITION #Go to addition function
SUB ONE
BRZ POWER #Go to Power

LDA B #Multiply Function
SUB ONE
STA B
LOOP LDA C
ADD A
STA C
LDA B
SUB ONE
STA B
BRP LOOP
LDA C
OUT
HLT

DIVIDE LDA A #Divide function
STA A
LOOPD LDA COUNT
ADD ONE
STA COUNT
LDA A
SUB B
STA A
BRZ ZERO
BRP LOOPD
ZERO LDA COUNT
OUT
HLT


ADDITION LDA A #Addition Function
ADD B
OUT
HLT

SUBTRACT LDA A #Subtract Function
SUB B
OUT
HLT



SQUARE LDA A #Square root function
STA A
LOOPS LDA COUNT
ADD ONE
STA COUNT
LDA A
SUB X
STA A
LDA X
ADD TWO
STA X
LDA A
BRP LOOPS
LDA COUNT
SUB ONE
OUT
HLT

POWER LDA A #Power function
STA COUNT
LDA COUNT
SUB ONE
STA COUNT
LOOPP LDA COUNT
SUB ONE
STA COUNT
LDA RESULT
ADD A
STA RESULT
LDA COUNT
BRP LOOPP
LDA RESULT
OUT
HLT


A DAT
B DAT
C DAT 000
COUNT DAT
X DAT 001
ONE DAT 001
TWO DAT 002
RESULT DAT