?
Solved

Little Man Computer Programming

Posted on 2009-05-19
6
Medium Priority
?
8,784 Views
Last Modified: 2012-08-13
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

0
Comment
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
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
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

by:climbgunks
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

by:robzgman
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
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

0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 11

Expert Comment

by:climbgunks
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
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

0
 

Author Comment

by:robzgman
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

by:
climbgunks earned 2000 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
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 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

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

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

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

Join & Ask a Question