Solved

# Little Man Computer Programming

Posted on 2009-05-19
8,763 Views
Programming a Fibonacci algorithm, I'm having issues with the concept of where I can assign labels (e.g can I do it for entire sections or just individual lines. How branching works?

What it needs to do:

Prompt user for a number
Input Test number
Set First = 1
Set Second = 1
Set Counter = 2

While Test is greater than Second
Next = Second + First
First = Second
Second = Next
Counter = Counter + 1

If Test is equal to Second
Display Counter

Else
Display zero
``````User Input			INP
Store Input			STA TEST

Start + Load Second		START LDA SECOND
Subtract Test			SUB TEST
Initiate Loop			BRP GREATER
Initiate Loop			BRZ EQUAL
Initiate Loop			BRZ END

Define + Load second		GREATER LDA SECOND
Save as Next			STA NEXT
Load Second			LDA SECOND
Save as First			STA FIRST
Load Next			LDA NEXT
Save as Second			STA SECOND
Load Counter			LDA COUNTER
Save as Counter			STA COUNTER
Close Loop			BRA START

Define + Load Second		EQUAL LDA COUNTER
Output				OUT
Stop				HLT

Stop				END HLT

Define Test = 0			TEST DAT 000
Define First = 1		FIRST DAT 001
Define Next = 0			NEXT DAT 000
Define Second = 1		SECOND DAT 001
Set Counter = 2			COUNTER DAT 002
Set Increment 1			INCREM DAT 001
``````
0
Question by:robzgman
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 2

LVL 53

Expert Comment

ID: 24429955
>> I'm having issues with the concept of where I can assign labels (e.g can I do it for entire sections or just individual lines.

A label is for a single instruction. So, when you jump to the label, the next instruction that is executed is the one where the label was added.
Of course, this means that execution continues with the instruction after that, etc., so the whole "section" of instructions following the label will be executed. In that sense, you could say that the label refers to the whole section (but only for discussion purposes).

>> How branching works?

There are basically three branch instructions :

* BRA (unconditional branch) : sets the PC (program counter) to the given address. The next instruction that is executed is the instruction at that address.

* BRZ (branch if zero) : if the accumulator currently contains the value 0, then the PC is set to the given address. In that case, the next instruction that is executed is the instruction at that address.

* BRP (branch if positive) : if the accumulator currently contains a value >= 0, then the PC is set to the given address. In that case, the next instruction that is executed is the instruction at that address.

If the branch is not taken, the PC is incremented (asusual), and the next instruction (immediately after the branch instruction) is executed.
0

LVL 11

Expert Comment

ID: 24435864

Labels are placeholders for addresses.   They can be placed in front of any command.   For example, your 'SECOND DAT 001' is an example of a label.   It just gives you a way to refer to addresses more clearly.  They aren't actual instructions.

Quickly looking at your code, you may want to change the following:

you want to test    TEST - SECOND,   not SECOND - TEST.   So load  TEST first, then subtract SECOND, then BRP.

Your call to 'BRZ END' will never take the brance, since the 'BRZ EQUAL' above it will never return.   So just remove it.

Otherwise, I think (quick glance) your code is quite good.

for more info on labels, and branching, see
http://en.wikipedia.org/wiki/Little_man_computer

0

Author Comment

ID: 24437472
It has no errors in compilation. However it just continues to run indefinitely (hundreds of lines) even with a small input of 8 (should return as final '6')

So I'm assuming one of the loop backs isn't working / properly
``````User Input			INP
Store Input			STA TEST

Start + Load Second		START LDA TEST
Subtract Test			SUB SECOND
Initiate Loop			BRP GREATER
Initiate Loop			BRZ EQUAL

Define + Load second		GREATER LDA SECOND
Save as Next			STA NEXT
Load Second			LDA SECOND
Save as First			STA FIRST
Load Next			LDA NEXT
Save as Second			STA SECOND
Load Counter			LDA COUNTER
Save as Counter			STA COUNTER
Close Loop			BRA START

Define + Load Second		EQUAL LDA COUNTER
Output				OUT
Stop				HLT

Stop				END HLT

Define Test = 0			TEST DAT 000
Define First = 1		FIRST DAT 001
Define Next = 0			NEXT DAT 000
Define Second = 1		SECOND DAT 001
Set Counter = 2			COUNTER DAT 002
Set Increment 1			INCREM DAT 001
``````
0

LVL 11

Expert Comment

ID: 24437754

The branching is wrong.   BRP will branch if >= 0.    so you'll never get to the BRZ EQUAL statement.

you probably want something like this instead

BRZ EQUAL
BRP GREATER
BRA END

So if it exits w/o printing something that means it wasn't a FIBONACCI number.   I don't know if that's what you want it to do or not.

For example, 1, 2, 3, 5, 8, 13,. ...    would print the FIB(x),   others would just exit w/o printing

You can run this at:

http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html

It's slow...  but does get the job done...

``````INP
STA TEST

START LDA TEST
SUB SECOND
BRZ EQUAL
BRP GREATER
BRA END

GREATER LDA SECOND
STA NEXT
LDA SECOND
STA FIRST
LDA NEXT
STA SECOND
LDA COUNTER
STA COUNTER
BRA START

EQUAL LDA COUNTER
OUT
HLT

END HLT

TEST DAT 000
FIRST DAT 001
NEXT DAT 000
SECOND DAT 001
COUNTER DAT 002
INCREM DAT 001
``````
0

Author Comment

ID: 24438371
Arg, nearly got it - its now working out where the number is a fibonacci number, however if its not a fibonacci number, it just runs out and ends up with a random number - e.g -4

I want it to just display 0 if not a fib number
0

LVL 11

Accepted Solution

climbgunks earned 500 total points
ID: 24438398
modified end  and added a zero dat value

``````INP
STA TEST

START LDA TEST
SUB SECOND
BRZ EQUAL
BRP GREATER
BRA END

GREATER LDA SECOND
STA NEXT
LDA SECOND
STA FIRST
LDA NEXT
STA SECOND
LDA COUNTER
STA COUNTER
BRA START

EQUAL LDA COUNTER
OUT
HLT

END LDA ZERO
OUT
HLT

TEST DAT 000
FIRST DAT 001
NEXT DAT 000
SECOND DAT 001
COUNTER DAT 002
INCREM DAT 001
ZERO DAT 000
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
###### Suggested Courses
Course of the Month8 days, 4 hours left to enroll

#### 737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.