Solved

Little Man Computer Programming

Posted on 2009-05-19
6
8,682 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
  • 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now