Solved

Little Man Computer Programming

Posted on 2009-05-19
6
8,706 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
529 college savings 5 101
NLP in .Net for Categorization 34 334
Math homework question 5 82
sorting efficency of sorting algorithm 30 103
Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

13 Experts available now in Live!

Get 1:1 Help Now