Link to home
Start Free TrialLog in
Avatar of robzgman
robzgman

asked on

Little Man Computer Programming

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
Add to First			ADD FIRST
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
Add to Value			ADD INCREM
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

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

>> 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.
Avatar of Todd Mummert
Todd Mummert


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

Avatar of robzgman

ASKER

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
Add to First			ADD FIRST
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
Add to Value			ADD INCREM
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

Open in new window


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
ADD FIRST
STA NEXT
LDA SECOND
STA FIRST
LDA NEXT
STA SECOND
LDA COUNTER
ADD INCREM
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

Open in new window

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
ASKER CERTIFIED SOLUTION
Avatar of Todd Mummert
Todd Mummert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial