Showing posts with label Little Man Computer. Show all posts
Showing posts with label Little Man Computer. Show all posts

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


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


Getting started with the Little Man Computer (post #02)

Continuing our series on The Little Man Computer.  This time - adding.

<-- previous post.  

The little man has an instruction for adding - simply "ADD". An instruction for adding might look like "ADD X", which means "add the value at the address labelled 'X' onto the accumulator." 

Example program number four.

Add three numbers together.

...adds three numbers together

lda x
add y
add z
out
.
.. the values of x, y, z
.. are coded here
x dat 5
y dat 10

z dat 100

When the LMC compiles this program it translates the first four commands into the codes, 599 (lda x), 198 (add y), 197 (add z) and 902 (output) as shown in the image below.

The LMC memory showing how the first four commands have been translated into codes.
The values of 'x', 'y' and 'z' are placed at locations 99, 98 and 97 (the little man decides that this is the best place to put them).  In the image below you can see the values 5, 10 and 100 in these locations.
The little man puts the values of 5, 10 and 100 into address 99, 98 and 97 and labels them x, y and z.


What is happening?
First the little man reads code 00: 599 and translates this as "Load the value in address at 99 into the accumulator.  This is our value 5.
Now that we have a value in the accumulator, we can add the other two values on, so...
The code 01: 198 means 'Add the value at address 98 onto the accumulator.'  This means that 10 is added to 5 and stored in the accumulator.  The accumulator should now contain the value 15.  The next code, 02:197 means, 'Add the value at address 97 onto the accumulator', so the value 100 is added to the current value in the accumulator - 15, giving an answer of 115.  Opening the output report should prove this:


The output instruction puts the current value of the accumulator into a file for easy reading.


Mnemonics used in this post:


add  -  adds a value at an address onto the accumulator.  For example 'add 99' adds the value at the address '99' onto the accumulator.  CAREFUL!  This is NOT the same as adding the value 99 to the accumulator.


lda  -  loads a value at an address into the accumulator.  For example 'lda 99' puts the value at address '99' into the accumulator (replacing any other value currently there.

out  -  writes the current value in the accumulator into the output file. 

dat  -  puts a value into the computer memory.  for example, 'one dat 001' puts the value '1' into an address and labels it 'one'.

#littleManComputer

Getting started with the Little Man Computer (post #01)

The Little Man Computer is a simple way of showing how a real computer works underneath.

We can imagine a little man running around inside the computer fetching instructions, and faithfully executing them.  

The little man uses the following instructions, which can be either coded as mnemonics in a program, or posted directly into the computer's 'brain'. The computer memory is represented as one hundred boxes numbered 0-99.

Instruction                    Mnemonic          Code
ADD                            ADD               1..
SUBTRACT                       SUB               2..
STORE                          STA               3.. 
LOAD                           LDA               5..
INPUT                          INP               901
OUTPUT                         OUT               902
BRANCH ALWAYS                  BRA               6.. 
BRANCH IF ZERO                 BRZ               7..
BRANCH IF ZERO OR POSITIVE     BRP               8.. 

For example, the Code 399 means 'STORE the current value in box 99.  We could write this as "STA 99"

Example program one.
Get a number from the human and store it in the computer memory (for later).

... a simple LMC program
inp

sta 99

The first line is a comment and is ignored by the little man.  It is there to help us understand our program.
The second line is a request for input.
The third line is our STORE in address 99

Here is a screenshot:
Your program after you compile it and press "execute".  You should see the LMC has converted our mnemonics into two codes in adresses 00 and 01.  The value we entered is put into address 99.

In fact we could store our value at any point in the computer memory.  Try the following program:

Example program two.
Get a number from the human and store it in the computer memory (for later).

... a simple LMC program two
inp

sta 54


Here we store the human's input at address 54.

What is happening?
Here, the mnemonic "INP" has been translated to the command "901".  When the little man reads this, he knows that it means, "get input from the human".  Here I typed the number '4', and the value of 4 is placed into the accumulator. The mnemonic instruction "sta 54" is translated to "354" and the little man understands that this means, "write the value in the accumulator into address 54".

Example program number three.

Let the little man decide where to put the human's input.

...example of variables
inp
sta first
inp

sta second


Here the little man has decided that it is best to store the first value in address 99 and the second in 98.
What is happening?
In this program there are two inputs.  The little man also receives the instruction "sta first", which means "store the value in the accumulator into the next available address and label it 'first'".
The instruction "sta second" means "store the value in the accumulator into the next available address and label it second."

Next time.  Adding numbers together.

What...another Little Man Computer update?!

That's right Little Man Computer fans - introducing Little Man Computer version 1.0.0.8.  You will be thrilled to discover the new and updated version of the Little Man Computer by proceeding directly to the downloads page and grabbing your copy from the version 1.0.0.8 link.

What's new in version 0.8?

  • The output report only opens if there is output to display.
  • Line labels can be numeric (if you really must...)
  • Screen-layout changed and a larger font and edit box used (useful for tired eyes)
  • Decorative art changed from naff robot to slick-looking context-sensitive artwork.   
LMC version 1.0.0.8 running on Windows 7

New updates for the Little Man Computer

Introducing a new update for the Little Man Computer - version 1.0.0.7.  Please proceed to the download page.

If you can contain your excitement, I'll explain what's new:
  •  Line numbers added (allows for better error correction).
  • White space is ignored by the parser.
  • Alternate comment character full stop (.)
  • Improved error messages.
  • Improved overflow error messages.

Image showing some of the new features of the Little Man Computer.
New Improved Little Man Computer

New Improved Little Man Computer

Following some user feedback with a class of students following the OCR GCSE Computing low-level programming module (A451), I have made the following improvements to the Little Man Computer Simulator:

LMC version 1.0.0.5

  • Scroll-bars have been added to the input window.
  • Lower-case input is accepted (the LMC is not case-sensitive, so 'y' and 'Y' are same label name).
  • Improved step-through mode (there is now time to click the mouse to halt the program)

Please follow this link to the DOWNLOADS page to update your version of the LMC Simulator.

New version of the Little Man Computer

We are proud to announce a new update to the our Little Man Computer.

Follow this link to the downloads page.

What's new in version 0.4?
  • Comments.  Lines beginning with a hash '#' are ignored by the compiler.
  • System reset.  This feature allows you to 'roll back' the LMC memory to its post-compile state.  Handy if you want to run your program again without recompiling.  A useful tool for beginning students of LMC who might forget to properly initialize their variables.
  • Ouput log.  The value of the OUT register is saved to a temporary text file.  Useful if you want to see the intermediate steps of a calculation.
  • New demo programs: addition, multiply

New! - Little Man Computer

Please enjoy our latest creation - the Little Man Computer (follow this link).

The LMC is an instructional model of a computer CPU and has been written as a significant improvement over the much cited University of York at Toronto's version.


Label