The most impressive Little Man Computer Code...so far

What follows is some of the most impressive code written for the Little Man Computer that I have ever seen.

Thanks to Eric and Will, here is a 'general purpose calculator' program, squeezed into the one hundred available memory locations of the LMC.

The program can perform Square root, DIV, addition, square numbers, Integer powers and multiplication.

In order to use the calculator the two operands are entered into the keypad first, and then lastly the operation is entered by selecting the corresponding operation code (listed in the comments).


For example, in order to perform the operation 13 DIV 2,
firstly enter 13 then 2, then 0 to select DIV.

You can download the source code from these pages, or find it listed below.



#Will

#Modified by Eric Rodriguez for more than just add and divide

#A general purpose calculator which works with the limitations of the little man computer

#

#KEY:

#1 or above for SUBTRACT

#0 for DIVIDE

#-1 for ADDITION

#-2 for SQUARE

#-3 for POW

#-4 for SQRT

#5 or below for MULT

#Input 1 is the first number

#Input 2 is the second

#Input 3 is the selection

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

INP

BRZ DIVIDE #case 0

BRP SUBTRACT #case 1+

ADD ONE

BRZ ADDITION #case -1

ADD ONE

BRZ SQUARE #case -2

ADD ONE

BRZ POW #case -3

ADD ONE

BRZ SQRT #case -4

#else fall through to mult

MULT 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

BRA MULTOUT

DIVIDE LDA A #Divide function

STA A

LOOPD LDA COUNT

ADD ONE

STA COUNT

LDA A

SUB B

STA A

BRZ ZEROD

BRP LOOPD

LDA COUNT

SUB ONE

STA COUNT

ZEROD LDA COUNT

BRA OUTPUT

ADDITION LDA A #Addition Function

ADD B

BRA OUTPUT

SUBTRACT LDA A #Subtract Function

SUB B

BRA OUTPUT

SQRT LDA A #Square root function

STA A

LOOPS LDA COUNT #SQRT loop

ADD ONE

STA COUNT

LDA A

SUB X

STA A

LDA X

ADD TWO

STA X

LDA A

BRP LOOPS

LDA COUNT

SUB ONE

BRA OUTPUT

SQUARE LDA A #Square function (set A = B and let multiply do the rest)

STA B

BRA MULT

POW LDA POWOUT

STA MULTOUT

LDA B

SUB TWO

STA POWC

LDA A

STA B

STA POWNUM

BRA MULT

MULTRTN STA A

LDA ZERO

STA C

LDA POWNUM

STA B

LDA POWC

SUB ONE

STA POWC

BRP MULT

LDA A

OUTPUT OUT

HLT

MULTOUT DAT 684 #default out - to OUTPUT

POWOUT DAT 674 #power out - to MULTRTN

A DAT

B DAT

C DAT

POWC DAT

COUNT DAT

X DAT 001

ONE DAT 001

TWO DAT 002

POWNUM DAT

ZERO DAT 000