# Little Man Programming



## Matt3453 (Oct 17, 2009)

Hello everyone,

New to the forums and I really need some help. I'm trying to create a little man program that would accept any three user inputs and return them in order from largest to smallest. I really have no clue where to start since I'm completely new to programming. Please help..


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Perhaps this thread may help:
Does anyone know Little Man Computer?


----------



## Matt3453 (Oct 17, 2009)

Thanks for your quick reply. What TheOutecast placed on that forum I'm able to understand:

You'll need a Halt in line 26
Here's what I came up with for finding the largest, using 17 lines:
00 : 901
01 : 397
02 : 901
03 : 398
04 : 901
05 : 399
06 : 297
07 : 810
08 : 597
09 : 611
10 : 599
11 : 298
12 : 815
13 : 598
14 : 616
15 : 599
16 : 902
17 : 000
97 : 000 DATA
98 : 000 DATA
99 : 000 DATA


However, I don't understand the explanation he gives for putting it in sorted order or this "bubble sort." We read a short chapter on Little Man that basically explains the different op codes, and I'm able to write very simply programs. But these more complex programs, I'm just completely lost...


----------



## TheOutcaste (Aug 8, 2007)

Wikipedia has a pretty good Step-by-step example of how it works.
http://en.wikipedia.org/wiki/Bubble_sort.

You just keep going through the list repeatedly, swapping numbers if needed, until you make one full pass without needing to swap. Then you know the numbers are sorted.

Write some code, try it out, fix the errors, all part of the learning process.


----------



## Matt3453 (Oct 17, 2009)

Well, I don't know if this makes sense. This is just a guess on my part I suppose so...



00 : 901 - IN
01 : 397 - STO
02 : 901 - IN
03 : 398 - STO
04 : 901 &#8211; IN
05 : 399 - STO
06 : 297 &#8211; SUB 97 from 99
07 : 810 &#8211; If positive, 99 largest
08 : 597 &#8211; negative, 97 largest
09 : 611 &#8211; branch unconditional
10 : 599 &#8211; load 99
11 : 298 &#8211; 99 minus 98
12 : 815 &#8211; If positive, 99 largest
13 : 598 &#8211; negative, 98 largest
14 : 616 &#8211; branch 16
15 : 599 &#8211; Load 99
16 : 902 &#8211; Output since largest
17 : 000
18 : 598
19 : 297 &#8211; SUB 97 from 98
20 : 800 - if positive, 98 largest
21 : 598 - if negative, 97 largest
22 : 598 &#8211; Load 98
23 : 902 &#8211; Output 98 since it is largest of 98 and 97
24 : 597 &#8211; Smallest of all values
25: 902 &#8211; Output smallest
26 : 000
99 : 000 DATA
98: 000 DATA
97 : 000 DATA


----------



## Matt3453 (Oct 17, 2009)

00 inp 901
01 sto 90 390
02 inp 901
03 sto 91 392
04 inp 901
05 sto 93 393
06 lda 90 590
07 sub 91 291
08 brp 812
09 lda 91 391
10 out 902 
11 br 614
12 lda 90 390 
13 out 902
14 lda 91 591
15 sub 92 292
16 brp 820
17 lda 92 392 
18 out 902
19 br 622
20 lda 91 391 
21 out 902
22 lda 90 590
23 sub 91 291
24 brp 829
25 lda 90 591 
28 out 902
29 lda 91 590
30 out 902
31 hlt 000


----------



## TheOutcaste (Aug 8, 2007)

Close
Line 3 shows store to 91, but the op code is for 92
Line 5 stores in 93, should be 92
Lines 25 and 29, the op code doesn't match the Mnemonic.
Based on the op code for line 25 and 29:
If the numbers are entered largest to smallest it works.
Input
30
20
10
output
30
20
10
If they are entered smallest to largest though, you get 4 numbers output:
Input
10
20
30
output
20
30
20
10

And This:
20
10
30
Gives this
20
30
20

Most assemblers will show the data in this format:
*Line number Label Op Code Mnemonic Comments*.

I've corrected lines 3 and 5, and changed the Mnemonic to match the Op code in lines 25 and 29, and added some comments for what each line is doing, but didn't add any labels:

```
00 : 901 inp     Get data from user and store it
01 : 390 sto 90
02 : 901 inp
03 : 391 sto 91  
04 : 901 inp
05 : 392 sto 92
06 : 590 lda 90  Load First
07 : 291 sub 91  Sub Second
08 : 812 brp 12  If Positive, First is larger, go output First
09 : 391 lda 91  Load Second
10 : 902 out     Output Second
11 : 614 br 14   Always Branch to compare Second and Third
12 : 390 lda 90  Load First
13 : 902 out     Output First
14 : 591 lda 91  Load Second
15 : 292 sub 92  Sub Third
16 : 820 brp 20  If Positive, Second is larger, go output
17 : 392 lda 92  Load Third
18 : 902 out     Output Third
19 : 622 br 22   Always Branch
20 : 391 lda 91  Load Second
21 : 902 out     Output Second
22 : 590 lda 90  Load First
23 : 291 sub 91  Sub Second
24 : 827 brp 27  If Positive, First is larger, go output
25 : 591 lda 91  Load Second
26 : 902 out     Output Second
27 : 590 lda 90  Load First
28 : 902 out     Output First
29 : 000 hlt
90 : First
91 : Second
92 : Third
```
You need to actually swap the data in the mail boxes. Outputting the larger of the first two numbers won't work as you haven't compared either of those numbers to the third. If the third is the largest, it should be output first
Here's the logic I would use:

```
Load the numbers into storage locations
Set a flag location to zero
Compare Third mailbox and Second mailbox
If Third mailbox larger, Third and Second are in correct order. Go compare Second mailbox and First mailbox
Second mailbox is larger so:
     set flag to 1 to show a change was made
     swap the data in the Third and Second mailbox
Compare Second and First (If a swap was made, you are actually comparing the number that was in third to first)
If Second mailbox larger, Second and First are in correct order. Go Check Flag
First is larger
     set flag to 1
     swap the data in the  Second and First mailbox
Check Flag
If Flag is zero, go output numbers
If Flag not zero, numbers were swapped, so need to check again
Go back to beginning where flag is set to zero
Output the numbers then halt
data temp
data flag
data box with 1 in it to set flag
data First
data Second
data Third
```
This can be expanded to more than three numbers, but with only 100 spots availible, you'll run out of room quickly. With more than three you'd want to reuse the code to save space, either by using a pointer to the numbers being checked, or by actually modifying the code as it's running, but that's getting way ahead of this problem.

EDIT: Just noticed that lines 26 and 27 are missing. Corrected that above


----------

