Solved

# Little Man Computer programming

Posted on 2009-04-20
7,886 Views
Hi everyone!
I need some help with a task that I have to do in my university class. I have been doing some java programming but I don´t understand the assembler programming that I have to use in "Little Man Computer". I could use a tip or two... :)

The program have to have this function... you have to write in to numbers and then you have to choose if the program shall add or multibly the numbers. If you type in "1" you are choosing addition and if you type in "2" you are choosing multiplication.

I have to use this LMC program > http://www.atkinson.yorku.ca/~sychen/research/LMC/LMCHome.html
0
Question by:Lisa79

LVL 4

Expert Comment

that link has all sort of example plz check before asking questions
0

LVL 53

Expert Comment

>>  I could use a tip or two... :)

Ok, what is it that you have a problem with ? Are you stuck somewhere ? Or don't know how to start ? If you tried anything yet, can you post that, so we can see how far you got ?
0

Author Comment

blaaze>
Thank you for your answer. But I belive it is obvious that I have already checked that link... and I tried to use that information a long time (two weeks) before I wrote my question here. Because some people writes here before they try by themself doesn´t mean that everyone does that!
0

Author Comment

Infinity08>
Hi! Thank you for helping me.. :)

Yes you can say that I´m stuck. I understand how I load and store the two numbers.. I don´t really understand how to use the (Branch if...) BRZ. And then I think I have to use loops to get the multiplication right? But I don´t understand how to write the code... and then I have to check that the code can handle if the inputs are zero. Can I do that through "Branch if zero or positive"?

I have done this:

``````INP

STA FIRST

INP

STA SECOND

INP

STA NUMBERONE

BRZ SUBTRACTION

LDA FIRST

SUB SECOND

OUT

INP

STA NUMBERTWO

(LOOP)

OUT

HLT
``````
0

LVL 53

Assisted Solution

So, let's see :

>> INP
>> STA FIRST
>> INP
>> STA SECOND

reads the two numbers, and stores them in memory. Looks good.

Note that you need to reserve memory space for these two variables (FIRST, SECOND). You do that using DAT. For example :

FIRST DAT

reserves space for a variable in memory, and calls it FIRST.

You then have to read the operation type (should be either 1 or 2 for addition or multiplication resp.), and store it in memory (don't forget to reserve space for it).

I would suggest to stop there for now, and just output the three values that were just read, and then halt the program.

Once that is working fine, we can start working on adding the functionality for branching and adding, and ...

Post what you get here, and don't hesitate to ask if you have doubts.

Note that the site you mentioned contains all information you need regarding the proper use of each of the instructions.
0

Author Comment

Infinity08>

Hi again,
sorry that I haven´t answered you until know.

I have a little problem with reading the operation type and to store it in the memory. But I have done this.. but I´m not sure that it is right.

``````INP

STA FIRST

INP

STA SECOND

LDA FIRST

OUT

LDA SECOND

OUT

STA ONE_SUB

HLT

FIRST DAT

SECOND DAT

ONE_SUB DAT
``````
0

LVL 53

Expert Comment

>> But I have done this.. but I´m not sure that it is right.

So, you take the two values as input, and then show them to the user.
Then you try to store ONE_SUB, but you haven't asked for the value yet. The user needs to tell you the operation type, so use the INP instruction to get it.
0

Author Comment

sorry... I see that I forgot that.

``````INP

STA FIRST

INP

STA SECOND

LDA FIRST

OUT

LDA SECOND

OUT

INP

STA ONE_SUB

HLT

FIRST DAT

SECOND DAT

ONE_SUB DAT
``````
0

LVL 53

Expert Comment

Ok, so now you've got the three values. Now, based on the operation type, you have to do two different things ... That seems like a job for the branch instructions ...
0

Author Comment

If I have understood everything right, I have to use addition and a loop for the multiplication. But I don´t understand how the program are going to know how many times it has to add the numbers. The example om the website has set the loop on ten times... but that is not going to work in my case!
0

LVL 53

Expert Comment

>> But I don´t understand how the program are going to know how many times it has to add the numbers.

If you have to multiply by 10, then you add the same value together 10 times.
If you have to multiply by 20, then you add the same value together 20 times.
If you have to multiply by x, then you add the same value together x times.
0

Author Comment

Ok, but how do I  know how many times I have to multiply? Do I have to specify that in the code? I´m thinking that I have to write the code so that the program calculates it in some way... of course... should I add the first number as many times as the second number? i.e run the loop as many times that I have printed in the second number? Adding the first number the same number of times as the second number? Am I going in the right direction...

My brain doesn´t want to think in this language. I´m sorry that I´m so "stupid" and asking so dumb questions. But I appreciate that you aren´t just giving me the code, because I still have to learn. :) And I´m sorry if my english isn´t perfect. :)
0

LVL 53

Expert Comment

>> Ok, but how do I  know how many times I have to multiply?

What do you mean ? If you have to calculate x * y, then that's what you have to do : one multiplication.

Note that :

x * y = x + x + x + ... + x

where there are y x's on the right side, added together. Or a more specific example with x = 5 and y = 7 :

5 * 7 = 5 + 5 + 5 + 5 + 5 + 5 + 5
0

Author Comment

Ok, that was what I thought.. but I couldn´t explain it better.

Now I have written a code, but it will not compile. >>>>

"LittleManException: Target Label "ONE" is not valid.
LittleManException: ----- Compile Unsuccessful -----"
``````INP

STA FIRST

INP

STA SECOND

LDA FIRST

OUT

LDA SECOND

OUT

INP

STA ONEORTWO

SUB ONE

BRZ NRONE

LDA FIRST

OUT

LDA ONEORTWO

SUB TWO

BRZ NRTWO

LDA FIRST

STA COUNT

LOOPTOP LDA COUNT

OUT

STA COUNT

SUB SECOND

BRP ENDLOOP

BRA LOOPTOP

ENDLOOP HLT

FIRST DAT

SECOND DAT

ONEORTWO DAT

COUNT DAT
``````
0

LVL 53

Expert Comment

>> "LittleManException: Target Label "ONE" is not valid.

That's because you haven't defined the label ONE. You need to give it some storage space (using DAT) and give it the appropriate value.
0

LVL 16

Expert Comment

Listening...
0

LVL 16

Expert Comment

(I hate the way EE doesn't allow text to line up properly.....)
0

LVL 53

Expert Comment

t0t0, do you realize that this was an assignment ? And that you're not supposed to solve the assignment ?
0

LVL 16

Assisted Solution

Infinity08
Thank you for pointing that out to me.

Lisa79
Infinity08 has raised an important concern in his last comment. I therefore ask you NOT to use my code as your answer for your class assignment

Be aware unless you show a strong aptitude in this subject area then you're tutor will instinctively know this is NOT your own work and may fail you for plagiarism.

However, I do feel you are just as capable of learning by examining my code as you are by painstakingly trying to learn from scratch on your own. The difference now though is not HOW, but WHY.

I have discussed some advanced techniques you wouldn't normally find a student capable of implementing so I suggest, as part of your understanding my code and for the purpose of completing your assignment, you make the following changes to my code and THEN (and ONLY then) submit it as your own work.

If you do not follow my guidelins then you are CHEATING yourself and you will NOT be able to feel pride nor a sense of accomplishment.

(1) GetOption. This consists of 7 lines of code.
(a) Change it so that it does not branch to 'End' if a '0' is entered.
(b) Instead of looping back to get another number, just end the program.
Hint: You should end up with just 6 lines of code.

(2) Addition. This consists of 4 lines of code.
(a) DO NOT simply output the answer and halt the program. Store the answer somewhere appropriate
(b) Jump to an appropriate part of the code to then output the answer and quit the program.
Hint: You should end up with still just 4 lines of code.

(3) Multiply. This consists of 4 sections of code.
(a) Initialising 'Result' (3 lines of code). Do not 'calculate' a zero as I have done. Instead...
(i) Add another appropriately named constant somewhere near the end of the program
(ii) Set this constant to an appropriate value
(iii) Replace these 3 lines of code with 2 lines of code that uses this constant to initialise 'Result'
Hint: You should end up with just 2 lines of code.

(b) Comparing which number is greater or equal than the other. (3 lines of code)
(i) Remove this section completely.

(c) Swapping the numbers to optimise your multiply loop. (6 lines of code).
(i) Remove this section completely.
(ii) Also, remove any non-required memory storage areas from the code.

(d) Instead of using Num2 as a 'Count' value...
(i) Add a memory storage area labelled 'Count'
(ii) Insert 2 lines of code just before the 'Loop' to copy the second input number to 'Count'

(e) Loop. This consists of 8 lines of code.
(i) Change just 2 lines to use 'Count' instead of 'Num2'

(4) Change the following labels:
(a) 'Num1' to 'First'
(b) 'Num2' to 'Second'

(5) In the data section, you should have just 5 labelled memory locations reserved.
(a) There is only need to set 2 of the memory storage areas to their initial values, so unset any that are not required to be set.

Once you have implemented these changes, compile and debug if necessary. And of course, the code is now your own work. Well done.
0

LVL 16

Expert Comment

VeeMod

Thank you.

As you can see, you got there before I was able to complete and post my last comment.

I didn't know you could do that in these circumstances.
0

LVL 16

Expert Comment

Lisa79
I submitted code earlier which has since been removed by the moderators. I apologise for the confusion in my last comment. Please ignore it's contents.
0

LVL 53

Expert Comment

I'm not sure if Lisa79 is still around (it has been a few days), and how far she got with this assignment since our last interaction.

Lisa79, if you're still working on this, there are now two experts here ready to help you out if needed :) So, just let us know if you need our help ...
0

LVL 16

Expert Comment

VeeMod

I appreciate your assistance in this matter.

1974 was a good year. Lots of sun, great music blaring out of our 'transistors', adolescence, girls, roller-skating and socialising - it's not surprising you left Assembly tucked away in the dark corners of your mind :)
0

LVL 16

Expert Comment

Infinity08

Thank you for the mention.... and please don't feel I intended to steal your chances to earn points, it's just that I have a passion with LMC.

I would love to offer some help however, my attepts do tend to be quite direct and I must learn to 'guide' rather than 'solve' for the asker.

0

LVL 53

Expert Comment

No worries, t0t0. If Lisa79 comes back, I'll step back and let you  guide her, how does that sound ? ;)
0

LVL 16

Expert Comment

Infinity08
I was rather hoping to offer a complete discussion on techniques and ideas relevent only to Lisa79's assignment and LMC. I don't want to exclude you from that discussion and I hope you are able to continue to offer Lisa79 assistance. From that point on, I will be happy to retire from this thread without credit. My motivation is a desire to evaluate the effectiveness of the conveyance of ideas and principles in Assembly language using the LMC machine.
0

Author Comment

Hi Everyone!
You could say that I was surprised when I saw my mailbox today. A lot of messages... :)

I´m sorry that I haven´t responded to your messages but I got sick in the beginning of the week and I have not done anything on this asignment. I have not seen the complete code, and I am glad/happy fot that because I want to understand the code not just have the right answer. I will sit down and do the asignment tomorrow... thank you for your messages! :)
0

LVL 16

Expert Comment

Hiya all
I had a nasty fall yesterday and as a result I have not been able to concentrate on EE. Havine spent nearly 10 hours in hospital and sitting here with my leg bandaged up, I'm feeling quite sorry for myself.

Lisa79
I didn't just provide a solution. I did a whole write-up. I'm pleased to hear you would rather learn for yourself. With that said, I hope to be able to post something a little later once I am settled.
0

Author Comment

t0t0>

I´m sorry to hear about your injury! But it´s a good thing that you are at home again... take care!
0

LVL 53

Expert Comment

>> I will sit down and do the asignment tomorrow...

So, how far did you get with that ? Any trouble ?
0

LVL 16

Expert Comment

Hiya all

It was really unfortunate I haven't been able to put any time into this due to my accident. Anyway, I've returned from my surgery and I've got an hour before I have to reurn so here goes. by the way, hiya infinity08..
0

Author Comment

Infinity08>
Well.. I have problems with the multiplication. It doesn´t work... I guess my loop is incorrect. I´m just looking at it and trying to get a solution. :)

This is my code right now.
``````INP

STA FIRST

INP

STA SECOND

LDA FIRST

OUT

LDA SECOND

OUT

INP

STA ONEORTWO

SUB ONE

BRZ NRONE

LDA FIRST

OUT

LDA ONEORTWO

SUB TWO

BRZ NRTWO

LDA FIRST

STA COUNT

LOOPTOP LDA COUNT

OUT

STA COUNT

SUB SECOND

BRP ENDLOOP

BRA LOOPTOP

ENDLOOP HLT

FIRST DAT

SECOND DAT

ONEORTWO DAT

ONE DAT

TWO DAT

NRONE DAT

NRTWO DAT

COUNT DAT
``````
0

LVL 16

Assisted Solution

Please allow me to come in at this stage....

I can see Infinity08's approach - to take you through this step-by-step explaining things along the way and I hope some of his advice has helped you.

For example, these are the tasks that I would personally break it down into. Do you agree?

1) Initialise constants and variables
2) Input two numbers
3) Input option
4) On option: 1 ADD, 2 MULTIPLY
6) Multiply both numbers
7) Output result
8) End
0

Author Comment

t0t0>
Hi!
Yes, I do agree. If I understand right you want me to change the order in my code?
Should it be like this?

1)
FIRST DAT
SECOND DAT
OPTION DAT
ONE DAT
TWO DAT
NRONE DAT
NRTWO DAT
COUNT DAT

2)
INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT

3)
INP
STA OPTION

Correct?

I´m still looking att the rest.. The program should first decide that input 1 is addition and number 2 is multiplication? And then depending on the choise the program jumps? Right? I have seen a instruction for jump in other assembler languages... but I´m not sure this LMC has the instruction "Jump"?

I have read one of your other messages and I saw that your tip was to write the code so it can not take se number "0". But I have to make a program that can read the input "0".
0

LVL 16

Assisted Solution

Looking at your code, it appears you'll need to reserve some space for variables - namely, FIRST and SECOND. This would be:

FIRST       DAT
SECOND  DAT

You could 'set' these to initial values as in:

FIRST       DAT  0
SECOND  DAT  0

but there wouldn't be any need to.

Disregarding whch operation you choose to perform, you may also decide you need somewhere to store your result. You may decided to label this RESULT.

In the case of multiplication, you may then decide to add SECOND to RESULT FIRST number of times so you may decide to 'set' RESULT to an initial starting value.

Unfortunately, LMC does not allow you to use direct numbers such as:

or

SUB 1

so you'll need to somehow get a '1' into your code and store it as a constant. I assume you've already done this somewhere in your code and if I'm correct, it would look something like:

ONE  DAT  1

So now, when you add 1 or subtract 1, you can now do the following:

or

SUB ONE

Furthermore, you need to give thought to what you'll be adding to or subtracting from. It won't be FIRST nor SECOND nor RESULT nor ONE, so you'll have to think of another appropriately named variable. Because you'll be counting (up or down - we haven't decided yet), you might decide to call it COUNT.

So all-in-all, you should have a minimum of 5 reserved memory locations appropriately labelled and initialised. This might look something like:

FIRST       DAT
SECOND  DAT
RESULT   DAT  0
ONE         DAT  1
COUNT    DAT

A point worth mentioning is that LMC does not automatically re-initialise your variables each time you run your program. Consider the following code:

Start    INP
STA Result
OUT
End      HLT

Result  DAT  0    STA Result

When you compile and run this code, you'll be expected to enter a number. Say you enter '5', looking at the code, we know 'Result' is initialised to '0', so ADD Result adds '0' to '5' (making this just 5 still) and STA Result stores this back to Result. The program ends by outputting the result.

After the program has executed, if you 'Reset' the program and run it again, and enter 5 when prompted to, this time, the program adds the contents of Result to our '5' again but this time, it is still set to the last value we stored there - '5'. So now you end up with 5 + 5 instead of 5 + 0.

This goes to highlihgt an important point - if you change the contents of memory (or your variables) during program execution, these will remain in that state next time you run your program.

What does this mean? Well, it means where you've initially initialised Result to '0' at the start of program execution, don't assume this will work next time around.

What can we do about it? Easy. Get the program itself to initialise it to '0' during run-time.

How? Well, there are three principle ways you can do this. The easiest is to declare another constant data type called something like...... ZERO and set it's contents to '0' as in:

Zero   DAT  0

and then use this to reset Result at the start of your program as in:

Start    LDA Zero
STA Result
etc...
HLT

Zero    DAT  0
Result  DAT
etc...

Another way does without the need to set up a constant labelled 'Zero'. Here, you have to 'compute' a zero. How? Like this (remember we already have a constant 'One' defined):

Start    LDA One
SUB One
STA Result
etc...
HLT

One     DAT  1
Result  DAT
etc...

The other way is to grab any opcode in the program code itself and subtract itself from itself. But you would only do this IF (and only IF there was no way of pre-declaring variables or constants as was the case in a similar exercise I advised on. But that isn't a problem in your case.
0

LVL 16

Expert Comment

Give me a moment to comment.....
0

Author Comment

but... I have reserved space for the variables?

0

LVL 16

Assisted Solution

Okay, firstly, consider the following two programs. Copy and paste them into LMC.

Firstly, note what happens when you compile each program.

Secondly, note what happens when you run both programs.

PROGRAM ONE

One   DAT 1
Six   DAT 6

Start LDA One
OUT
End   HLT

PROGRAM 2

Start LDA One
OUT
End   HLT

One   DAT 1
Six   DAT 6
0

LVL 16

Assisted Solution

Now consider what happens when you copy and paste this code. Does it compile? Does it run?

One  DAT 1
Zero DAT 0

Start LDA One
OUT
End   HLT

0

LVL 16

Expert Comment

okay, my fault... looking at my list of 8 sections, you may have interpretted that as placing the variables and constants at the start of the code. These should always (normally) appear at the end of the code.

I'm looking at your code and this is what we have so far....

INP
STA FIRST
INP
STA SECOND
LDA FIRST
OUT
LDA SECOND
OUT

FIRST DAT
SECOND DAT
OPTION DAT
ONE DAT
TWO DAT
NRONE DAT
NRTWO DAT
COUNT DAT

Please allow me to simplify this for you.

(At this stage, because we're concentrating on the input section only, I'm going to omit some of your variables and only include those necessary for this section).

Looking at your code, you're getting both numbers in and storing them. That looks good. Then, you've decdied to re-load and output them. Is this really necessary? Might it not be simpler to just do the following and call it a day as far as getting your data in is concerned?

GetInput INP First
STA First
INP Second
STA Second

First       DAT
Second  DAT

0

LVL 16

Assisted Solution

Now, we've got both numbers in, so we can move onto the next section - 3) Input option.

That's easy....

Firstly, I want to emphasise the use of labels. Just as you label your variables and branch locations, it makes sense to label sections of code too.' It makes it easy to read through and helps to identify sections of code. I cannot emphasise this enough. If you follow this golden rule when you hand in your assignemt I guarantee you will earn BIG marks. For a tutor, who has to mark in excess of 20 pieces of work, there is nothing worse than receiving a long list of cryptic-like code and then trying to wade through it attempting to figure out what it's supposed to do.

Believe me on this point. You will SOCRE BIG points if you follow my examples in labelling and the use of blank lines to separate each section of code.

So, get ready to score some BIG points..... Now for section 3.

GetOption    INP

That's all there is to it - just one line of code. There's no need to store it because we only need to use it the once, to make a decision where to go to next. Storing it would require another instruction plus another memory location to store it too. Why over-complicate things?

So, so far we have....

GetInput     INP First
STA First
INP Second
STA Second

GetOption  INP

First           DAT
Second      DAT

Right, let's move straight on to the next section - section (4), branching to our Addition routine or our Subtract routine....I believe this is where you've been having a few problems.
0

LVL 16

Expert Comment

Are you happy with the code so far?
0

LVL 16

Expert Comment

OOOoooppps! Sorry.... I meant the following: (I don't know how the 'INP First' crept in, it meant to be just 'INP')

So, so far we have....

GetInput     INP
STA First
INP
STA Second

GetOption  INP

First           DAT
Second      DAT
0

LVL 16

Accepted Solution

Well, tha's not entirely true:

1) it knows when something is ZERO or NON-ZERO
2) it knows when something is POSITIVE or NEGATIVE
3) it knows how to ADD
4) it knows how to SUBTRACT

and that's about the scope of it.

As far as LMC is concerned, these are the possible combinations that a number can be:

All numbers greater than 0         NON-ZERO   and            POSITIVE
All numbers less than 0              NON-ZERO   and   NON-POSITIVE
Zero                                                     ZERO   and            POSITIVE

LMC does not know what a '6' is or that '2' is greater than '1'. You can't do the following:

IF Option IS GREATER THAN 10 THEN
do something here
END IF

Nor can you do this:

IF Option EQUALS 1 THEN
do this
END IF

These sort of things are commonplace in higher level languages but nowhere to be seen in assembly language.

Which poses a problem because section (4) of our program has to make a decision based on whether we input a '1' or a '2'. In a higher level language this might look something like the following:

IF Option = 1 GOTO Addition
IF Option = 2 GOTO Multiply

So, if we input 1, we jump straight to the code which does addition, and if we input 2, we jump to a different part of our code. In the two-line example above, if we input anything other than a '1' or a '2' then we just continue onto the next instruction. This could be:

IF Option = 1 GOTO Addition
IF Option = 2 GOTO Multiply
END

Which would make sense because we haven't defined what action should be taken if anything other than a '1' or a '2' is input. So, the program ends. No result is displayed (because no arithmetic was actually performed) and that's all there is to it. Simple.

But there's just one thing wrong with this design - there's no MLC equivelent instruction to test if our input is a '2', or even a '1' for that matter. We can test for a '0' though, but nothing else.

To get around this, we need to test if our option is '0' (not '1' or '2'....). We can do this by subtracting '1' from our option however to do this, we need to define a constant where our other constants and variables are located and initialise it to '1' as in:

One        DAT  1

we can then do this:

GetOption  INP
SUB One

at this stage, had we input a 1, then subtracting '1' from it we get '0' - so we know from this zero that we had input a '1' (because we know we've only subtracted our first '1'). If we didn't get a zero then we know we didn't input a '1'.

Let me explain. Whenever the result of an operation sets the value in the Accumulator equal to '0' a special 'flag' inside the LMC is set - this is the 'zero flag'. This is an automatic process and we don't need to concern ourselves with how it does this - it just does!

These are the conditions that would set LMC's zero flag:

INP                  (As it's read straight into the Accumulator) if we input a '0'

SUB addr        If the subtraction results in a zero.

Basically, whenever the Accumulator's contents is '0', LMC's zero flag is set. (It also sets the POSITIVE flag too - but we don't need to know that for now).

This is great news because LMC has a BRZ instruction which basically enables us to jump from one place to another if LMC's zero flag is set (ie, the Accumulator equals '0').

So, the following code could test if we had input a 'i':

GetOption  INP
SUB One
HLT

Had we input a '6' say, then we wouldn't have gone nowhere because 6 - 1 = 5,

What about testing for a '2' - No problem. Same technique!

GetOption  INP
SUB One
SUB One
BRZ Subtract
HLT

And there we have it! It can't get any simpler than that. If you don't believe it works, try tracing through it with different input values. Try -4, 0, 1, 2, 3 and 6.

Looking at this simple code, we note it performs some action only when we input either a '1' or a '2'. For everything else the program simply ends. This seems a little wasteful because with a small effort, we could loop back to where get our input from until a 'valid' option is entered as in:

GetOption  INP
SUB One
SUB One
BRZ Subtract
BRA GetOption

and this program ain't going anywhere unless we input a '1' or a '2'. Not even an earthquake can shake us out of this loop. It doesn't come more robust than this.

Which now brings me to a subtle suggestion. How about incorporating a simple 'exit' mechanism so that we can abort the program by inputing a '0'? We could do that with the minimum of effort. Here goes:

GetOption  INP
BRZ End
SUB One
SUB One
BRZ Subtract
BRA GetOption

And that's it. Section (4) complete. Let's put together what we've got so far filling in the missing labels as we go along. See below:

GetInput     INP
STA First
INP
STA Second

GetOption  INP
BRZ End
SUB One
SUB One
BRZ Subtract
BRA GetOption

Subtract     [SECTION 6]

[SECTION 7]

End            [SECTION 8]

First           DAT
Second      DAT
One           DAT  1

Just a final word before we leave this section. I've used BRZ and BRA in the above code. Basically, with BRZ, a branch condition occurs whenever the Accumulator's contents is zero whereas BRA performs an unconditional branch - a bit like a GOTO statement.

I hope this has been some help to you.

Don't forget the labels - especially to identify different sections of code. And don't forget the blank lines separating different sections. Let's keep it neat and snmple. Let's make it easy for your tutor to give you MAXIMUM marks!!!
0

Author Comment

t0t0>

One  DAT 1
Zero DAT 0

Start LDA One
OUT
End   HLT

No it doen´t run or compile. It says that the target LABEL 6 is not valid. That´s because you haven´t reserved space for the variable Six.
0

Author Comment

t0t0>
About blank lines... I didn´t know that it was possible to have blank lines. Every example that I have, didn´t have any blank lines. I personally prefer that, because it is so much easier to read the code if you have blank lines. So thank you!
0

Author Comment

t0t0>
It has been a BIG help for me. I´m really think that I understand your examples!

I see that we are heading for the next part of the code...addition and substraction. Should it not be addition and "multiplication". I know that the LMC doesn´t have any instructions for multiplication. But it has to be done with addition and a loop? Right?
0

LVL 16

Expert Comment

Sory... I was half asleep when I posted my last comment, by whic time my mind had turned to mush.... Yes, multiplication.....

Here's our code so far....

GetInput     INP
STA First
INP
STA Second

GetOption  INP
BRZ End
SUB One
SUB One
BRZ Multiply
BRA GetOption

Multiply      [SECTION 6]

[SECTION 7]

End            [SECTION 8]

First           DAT
Second      DAT
One           DAT  1

0

Author Comment

then I´m back in the game. :)
0

LVL 16

Expert Comment

BIG time mate!!!! Get prepared for some BIG marks.....

Oh, that comment of mine on section (4) - make sure you really understand why and how it works...

Now then, are you able to complete the next section - section (5): Addition?

Hint, This should consist of no more than 4 lines of code - the last line is a branch to section (7) instruction.

0

LVL 16

Assisted Solution

Hint for section (5)..... Just looking at it logically...

You need to load one of our numbers we input previously.
You need to add the other number to it.
You need to strore the result away so that it can be processed in a later section of the code.
You need to branch to that section of code which will process the result.

In addition to these instructions you will need to define an additional storage area for the result in our data section of our code.

Let us know when you've competed it.....
0

Author Comment

You wrote "Which now brings me to a subtle suggestion. How about incorporating a simple 'exit' mechanism so that we can abort the program by inputing a '0'?".

That would be a problem... My teacher said that the program must be able to multiply two zeros ("0") and it has to handle that the option is "0" too.
0

Author Comment

I thought I had to use "BRP" instruction for the multiplication? If my input is 4 and it substracts with one I get 3? And then its "zero or positive".

Why do I have to write "BRA GetOption"? Otherwise I think I have understood the logic.. :)

0

LVL 16

Assisted Solution

I'm quite annoyed with myself. I type an indepth reply and accidentally lost it all.

You must agree each section of code does EXACTLY what it's meant to do as we go along - and then not look back.

Section (2) enables us to input two numbers. These are then stored in FIRST and SECOND. Do you agree this works?

Section (3) enables us to input an option. This can be ANY number whether positive, negative of zero. There's no need to actually store this number because we'll be using it straight away in the next section. This section then, consists of just ONE line of code. Do you aggree it does it's job properly?

Section (4) is where we process our option. This is where we decide what to do depending on what number was entered as our option. Look at the code. If we enter a 0, the program exits. If we enter a 1, we do Addition. If we enter a 2, we do multiplication. If we enter ANY OTHER number apart from those three already mentioned (INCLUDING negative numbers) then we loop back to get another number - obviously disregarding the current number we just entered. Does this make sense? Are you able to TRACE through the code and work out how this works?

Here's the code I'm talking about:

BRZ End
SUB One
SUB One
BRZ Multiply
BRA GetOption

The reason for the BRA GetOption is because if we don't enter a valid option then the program won't know what to do next so, BRA GetOption branches back to Section (3)'s code (which is just a single line of code) consisting of: "GetOption INP" so that we can input another number - hopefully, a VALID number this time.

Does that make any sense?

Now, where's you code for the ADD two numbers routine? Have you done this yet?
0

LVL 16

Assisted Solution

Lisa....

GetOption    INP                     We're now at Section (3) of our code and this is wehre we input an
option. The number we input will go straight into the Accumulator.
At this point, we don't care what number we input as our option.

Let's say we run our program 5 times and each time we input an option
we enter one of these numbers: 0, 1, 2, 9, -4. Let's see what effect
these have on our code.

Run 1        Run 2        Run 3       Run 4       Run 5
Accumulator    [  0    ]        [  1   ]        [  2   ]        [  9   ]       [ -4   ]

BRZ  End           We're now at Section (4) of our code and this is our next instruction.
BRZ performs a jump (or a GOTO) to another part of our code IF
the contents of the Accumulator is ZERO.

In the case of our first run (Run 1), the Accumulator IS zero so
therefore, the condition for BRZ is satisfied and we jump to a part
of our code (somewhere later on) where we simply just end
the program. That section code will be Section (8) (which we haven't
defined yet) which we have labelled 'End'. At this moment in time,
we're not worried about the specifics of Section (8) - we just know we
want our program to end at that point.

So, at this point, had we input a '0', our program would have ended.

SUB One           This subtracts 'something' from the number held by the Accumulator.
In our case, it subtracts the value which is stored in memory labelled
"One". And as it happens, this value is in fact '1' (hence the
approriately named label).

So at this point, '1' is subtracted from the Accumulator therefore,
for each run, the Accumulator is now:

Run 1        Run 2        Run 3       Run 4       Run 5
Accumulator    [      ]        [  0   ]        [  1   ]        [  8   ]       [ -5   ]

NOTE: Run 1 no longer exists because that run ended.

BRZ Addition     We're now at the next instruction. A BRZ again. Looking at our
Runs, we input a '1' during Run 2, and as it happens, the
Accumulator for Run 2 is conveniently now set to '0'. So, this instruction
branches off to the section of code which adds our two numbers
together - the numbers in FIRST and SECOND. This will be Section (5)
labelled "Addition" which we haven't defined yet - but we will do later.

So, had we entered a '1' then we'd now be doing the Addition however,
let's continue by tracing through for Runs 3, 4 and 5....

SUB One           This is the next instruction. Again, a '1' is subtracted from the value
in the Accumulator. This now gives us:

Run 1        Run 2        Run 3       Run 4       Run 5
Accumulator    [      ]          [      ]         [  0   ]        [  7   ]       [ -6   ]

Both Runs 1 and 2 no longer concern us becuase they have been
completed.

BRZ Multiply     We're now at the next instruction. Once again it's a BRZ instruction.
As it so happens, Run 3's Accumulator has been set to 'zero' by the
previous SUB intruction. If you recall, we input a '2' for Run 3 and as it
so happens, because the Accumulator is now set to 'zero', the BRZ
instruction branches off to a part of the code which multiplies our
two numbers for us. That will be Section (6) which we haven't yet
defined - but we know (from our previously agreed list of sections)
that we will do this later. At this point, we just need to know that we
will be branching there by using the label "Multiply".

So now we come to the last instruction in this current section (Section 4).

BRA GetOption   If we've reached this point, then we have NOT entered 0, 1 nor 2. We
don't care what number was input, we just know it's a 'bad' one. So,
the BRA instruction branches back to the start of Section (3) for
Runs 4 and 5 - this is because the Accumulator's content is:

Run 1        Run 2        Run 3       Run 4       Run 5
Accumulator    [      ]          [      ]         [       ]        [  7   ]       [ -6   ]

So for both Runs 4 and 5, we simply go back to get another option into
the Accumulator.

This process continues until we enter valid option numbers (0, 1 and 2)
during Runs 4 and 5.

And that's all there is to it. Simple as hell. If you cannot understand this then please say so as I feel I'm wasting my time going forward with this.

If, on the other hand, you want me to clarify any point relating to LMC, LMC's instructions, or any part of our code so far, then please let me know.

Now, have you completed the section that adds our two numbers - please see my previous two comments (ID: 24311922 and ID:  24311946). If so, please let us see it.
0

Author Comment

I understand the sections... and if I have some questions about the LMC´s instruction I will let you know.

I´m doing the asignment on my free time and because I had to be at work I´m not finished with the rest of the code. Perhaps in a few hours...
0

Author Comment

this is what I have done this far... I´m now looking at section 7.
``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

OUT

BRA End

Multiply      	LDA First

STA Count

End           	HLT

First           	DAT

Second      	DAT

One           	DAT  1

Count		DAT
``````
0

LVL 16

Assisted Solution

Lisa

WOW!!, Your code looks NEAT and EASY TO READ. I LOVE the LABELS and the use of BLANK LINES. It just looks all so LOGICALLY set out.

YOU ARE HEADING FOR MAXIMUM MARKS - BE PREPARED TO EARN A DISTINCTION.

Please allow me to make some minor suggestions.

I anticipate there'll be two PATHS to the end of the program. One where the user enters a '0' during the GetOption section of the code - in which case the program jumps straight to the VERY end, and one where the program naturally works it's way to the end.

In the first case, we don't do any processing therefore there's nothing to output and the program ends immediatelt. In the second case, the program processes our data and eventually works it's way to the end however, along the way, it outputs the result of the processed data.

So, somewhere, near the end, we need to slip in section (7) OutputResult.

You've got the right code for Section (8) End. You were righ that this consists of just a single instruction: HLT. So all that leaves us to do is tidy up Section (5) Addition, design and code Section (6) Multiply and Design and code Section (7) OutputResult.

Looking at Section (5) Addition, if we're going to jump to "OutputResult", we could actually save the result of our addition to memory first (we could probably label it "Result" or something like that) and then jump to Section (7) OutputResult.

Then, the program could just naturally flow to the next section, Section (8) End, and end nicely.

Please let me fill in the bottom part of our program.... from section (5) Addition. (We don't need to worry about any of the code before this point because it does exactly what we want it to do).

I'll let you fill in the the bits to replace these -->  "<.....>".

<Save the result to memory>
BRA <Somewhere other than the "End">

Multiply             <We'll concentrate on this last section next>

DisplayResult   <Get our result from memory>
<Display it>

End                    HLT

First                    DAT
Second               DAT
<Memory>        <Directive>       <-- for storing our result.
One                    DAT  1
Count         DAT
0

LVL 16

Assisted Solution

Lisa....

Here's a simple flowchart of your program:

.
``````.

START

|

---------------

|               |

|  Get FIRST    |

|               |

---------------

|

|

---------------

|               |

|  Get SECOND   |

|               |

---------------

|

------------->|

|              |

|       ---------------

|      |               |

|      |  Get Option   |

|      |               |

|       ---------------

|              |

|              |

|             / \

|            /   \

|           /     \

|          /  Is   \  Yes

|         [ Option  ]------------------------------

|          \  '0'  /                               |

|           \     /                                |

|            \   /                                 |

|             \ /                                  |

|              | No                                |

|              |                                   |

|             / \                                  |

|            /   \                                 |

|           /     \                                |

|          /  Is   \  Yes                          |

|         [ Option  ]----------                    |

|          \  '1'  /           |                   |

|           \     /            |                   |

|            \   /             |                   |

|             \ /              |                   |

|              | No            |                   |

|              |               |                   |

|             / \              |                   |

|            /   \             |                   |

|           /     \            |                   |

|          /  Is   \  Yes      |                   |

|         [ Option  ]--------------------          |

|          \  '2'  /           |         |         |

|           \     /            |         |         |

|            \   /             |         |         |

|             \ /              |         |         |

|              |               |         |         |

--------------  No            |         |         |

|         |         |

|         |         |

|         |         |

---------------        |         |         |

|               |       |         |         |

|      |               |                 |         |

|       ---------------                  |         |

|                                        |         |

|                                        |         |

|                                        |         |

|       ---------------                  |         |

|      |               |                 |         |

|      |   Multiply    |<----------------          |

|      |               |                           |

|       ---------------                            |

|              |                                   |

------------->|                                   |

|                                   |

---------------                            |

|               |                           |

|Display Result |                           |

|               |                           |

---------------                            |

|                                   |

|<----------------------------------

|

---------------

|               |

|      End      |

|               |

---------------

.
``````
0

Author Comment

Something like this?
``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

STA Sum

BRA DisplayResult

Multiply

DisplayResult	LDA Sum

OUT

End           	HLT

First           	DAT

Second      	DAT

Sum		DAT

One           	DAT  1

Count		DAT

``````
0

LVL 16

Expert Comment

It's looking good. you must be pleased....

if i were you, i'd keep this assignment close to your chest and not share it with others as it might seem as though you copied from their work rather than them copying from yours!! - that's just the competitive side of me talking now.

just one small point.... we'll be using the same memory location to save our result to when we do our multiplication as well.... I'm just wondering whether the label "Sum" would be appropriate because 'sum' actually implies 'addition'. i am just wondering if it can be named something else - something neutral.

Right! time to press on with the Section (6) Multiply....

0

LVL 16

Expert Comment

Do you have any thoughts about how you'd like to tackle the multiply section of your code?
0

Author Comment

I´m very pleased. :) And I´m bot going to share my code with anyone!

Kan I call it Result?

About Section (6) Multiply... my thoughts is if I want to multiplu I have to use addition and a loop. For ex if my first number is 5 and my second number is 3. Then the loop has to "loop" 3 times and add the first number to itself three times (5+5+5) Right?
0

LVL 16

Assisted Solution

sounds good! it's all downhill from here.

firstly, "Result" is and EXCELLENT choice - afterall, it ties in with "DisplayResult" quite nicely! Consistency will score you good marks!!

secondly, i'm glad you're keeping this to yourself.... you paid for it!! - show them AFTERWARDS - when you've got your 100% mark and distinction.

finally, your method to multiply is good. i guess you mean something like this then....
.

``````.

Multiply

|

---------------

|               |

| Set Result=0  |

|               |

---------------

|

|

---------------

|               |

|Set Count=First|

|               |

---------------

|

------------->|

|              |

|             / \

|            /   \

|           /     \

|          /  Is   \  Yes

|         [  Count  ]-------------> DisplayResult

|          \  '0'  /

|           \     /

|            \   /

|             \ /

|              | No

|              |

|       ---------------

|      |               |

|      | Add Second to |

|      |    Result     |

|      |               |

|       ---------------

|              |

|              |

|       ---------------

|      |               |

|      |   Decrement   |

|      |     Count     |

|      |               |

|       ---------------

|              |

--------------

.
``````
0

LVL 16

Expert Comment

after you've coded that up, I'm going to discuss a method to optimise this section - this will EARN you MEGA marks!!!!
0

Author Comment

It looks good... But I have to go and do some errands. :(
I will be back in two-three hours and start coding again.
0

LVL 16

Expert Comment

okay....
0

Author Comment

"Hint for section (5)..... Just looking at it logically...

You need to load one of our numbers we input previously.
You need to add the other number to it.
You need to strore the result away so that it can be processed in a later section of the code.
You need to branch to that section of code which will process the result."

I guess I have to include this too?

I have a qustion about the "Set Result = 0". Should it be "Result Dat 0" then? I´m sorry if I´m a slow learner..

I´m going to work now but I will try to login and look at the code at work. Thank you for you patience!
0

LVL 16

Assisted Solution

no point in setting Result to '0' because this only works once in LMC's lifetime. if you run the code the first time, indeed it would have set Result to '0' however, if you run it again you'll note it retains the previous result (which theoretically is wrong - but that's how this particular LMC works).

You should have tested this out for yourself using something simple like:

LDA a
STA a
OUT
HLT

a  DAT 0
b  DAT 1

the example above first 'places' a zero in 'a'. our program then changes the contents of 'a'. it will output a '1'. if you run it a second time, it does not re-initialise (nor 'place') a '0' into 'a' - it just remembers the last result ie, '1'. so this time it will output '2'.... etc.

re: hint for section (5).... that's right, that's what i said. the flowchart shows you this... but you also need to do other stuff as well to actually make this work.
.

``````.

Multiply

|

---------------

|               |

| Set Result=0  |

|               |

---------------

|

|

---------------

|               |

|Set Count=First|

|               |

---------------

|

------------->|

|              |

|             / \                   You need to branch to that section of code...

|            /   \                  ...which will process the result.

|           /     \

|          /  Is   \  Yes

|         [  Count  ]-------------> DisplayResult

|          \  '0'  /

|           \     /

|            \   /

|             \ /

|              | No

|              |

|       ---------------

|      |               | You need to load one of our numbers we input previously.

|      | Add Second to | You need to add the other number to it.

|      |    Result     | You need to strore the result away so that...

|      |               | ...it can be processed in a later section.

|       ---------------

|              |

|              |

|       ---------------

|      |               |

|      |   Decrement   |

|      |     Count     |

|      |               |

|       ---------------

|              |

--------------

.
``````
0

LVL 16

Expert Comment

just to let you know, in your final listing, there'll be THREE parts to the multiply section.

you've already got the first TWO parts:

Pt.1: reset Result to '0'
Pt.2: ??????????????? <--- I'll leave you to stew over this one for a bit
Pt.3: do the multiplication

and this would normally be all you'll need to do to accomplish the task.... however, we're going to explorer a method to OPTIMISE this section - this will be a small bit of code slipped in between Pt.1 and Pt.3. This will a MASSIVE marks earner.

this is what's left to do:

1) write the code for Section (5) Multiply.
2) discuss and slip in Pt.2.

and that would normally be the end of it HOWEVER..... i will discuss a final RE-EDIT of the code suited to the attitude of your tutor (based on his/her own experience) and suited to the actual course you are doing this for - I will explain all this when we get to that point. so...

3) OPTIMISE code (FINAL EDIT)

and that will be all. at which point your code will be the most professional piece of work your tutor have probably EVER clapped his/her eyes on.
0

LVL 16

Expert Comment

Lisa

0

Author Comment

t0t0

I was at work yesterday evening.. but now I´m back. I´m not getting the code right.. but this is what I got now. But I´m still working on it.
``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

STA Result

BRA DisplayRes

Multiply      	LDA a

STA a

OUT

HLT

LDA a

STA Count

OUT

LOOPTOP LDA Count

SUB One

STA Count

BRZ DisplayRes

LDA First

STA Count

BRZ LOOPTOP

DisplayRes	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT 1

One           	DAT 1

Count		DAT

a		DAT 0

b		DAT 1
``````
0

LVL 16

Expert Comment

Lisa, why did you put this in your code?....

LDA a
STA a
OUT
HLT

That was maent to be put into the LMC all by itself as a mini-program. It's purpose is to demonstrate that even if you pre-initialise memory, you can't rely on it being initialised again the 2nd time you run the program.

0

LVL 16

Assisted Solution

I've tidied up the bottom half of your program and added some notes, from Section (5) Multiply, onwards. See below:
.
``````.

Multiply      <Pt.1: "Initialise Result" to go here (2 lines of code)>

<Pt.2: To discuss and to go here later,

but program will still work without it>

STA Count             | Good start

Loop          LDA Count         <---

SUB One           <------ Must be careful here because

STA Count                 Count might already be '0'

BRZ DisplayResult <------ Good intention

LDA First         <---

STA Count         <---

BRZ Loop          <------ Good intention

DisplayResult LDA Result        <------ Great!!

OUT

End           HLT               <------ Great!!

First         DAT

Second        DAT

Result        DAT

One           DAT 1             <------ Only one needing initialising.

Count         DAT

Please look at the last flowchart describing the operation of this section of code. And give it a little more thought please.

.
``````
0

LVL 16

Expert Comment

Okay, so we haven't figured out a way yet to initialise Result to '0'..

The obvious way would be to define another data constant, and use that (just as we did for "One").
0

Author Comment

is this correct? I´m still working on the rest of the loop... when you say "Good intention". What do you mean should i keep the code or change it in some way?
``````Multiply      	LDA ResultMul

STA Count

STA Count

Loop 		LDA Count

*

*

*

*

DisplayRes	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

ResultMul	         DAT 0

Count		DAT

``````
0

LVL 16

Expert Comment

``````First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero              DAT 0

Count		DAT
``````
0

Author Comment

Are you helping me too much if you explain to me in words (not finished code) how the multiplication works?

If I have understand it right.. the program has to do some type of check. Like in section (2) and depending on the result it "jumps" to a specifik part in the code. I think it is very hard to understand the logik behind the code and that´s why it´s hard to write it.
I don´t understand how the program uses the two input numbers... I understand that if the first number is 3 and the second is 5, the first number has to loop 5 times. correct? But how (!) can the program check how many times it has to loop? I guess I have to do as in section (2), test if the input i "0" and then make a choice? I thought that I was supposed to use "One" but as you said I have to be careful with that.
0

Author Comment

Zero is a much better choice.. I had a black out and could not come up with a good "name".
0

LVL 16

Assisted Solution

1) This looks good

First                 DAT
Second            DAT
Result      DAT
One                 DAT 1
Zero              DAT 0
Count      DAT

2) "Explain in words".... They say a picture paints a thousand words. Everything you need to know about the design of this final section is in the flowchart below. All you have to do is convert it to code. I'll do the first bit to show you how to do it.... then see how much more you can do....

3) Looping..... if the first number is 3 and the second number is 5, then you WILL be looping 3 times while adding '5' to itself!! But instead of counting your loop from '1' to '3', you'll be counting down from '3' to '0'.... the reason is because '0' is the only number LMC recognises (with the BRZ instruction).

4) Yep, the name "Zero" will do fine.... No need to think about it - it's just obvious and it makes perfect sense. Why complicate things?

5) I prefer "DisplayResult" not "DisplyRes". Your tutor will too. Why invent a non-existent word for the sake of 3 letters? I also prefer just "Loop" rather than "LoopTop" - It's just clearer.

Right, see if you can code this flowchart. I'll do the first box. Some boxes might have THREE instructions, some only ONE or TWO....
.
``````.

Multiply

|

---------------   (2 lines ie,)

|               |

(Label)          | Set Result=0  |  LDA Zero

Miltiply         |               |  STA Result

---------------

|

|

---------------   (2 lines)

|               |

|Set Count=First|

|               |

---------------

|

------------->|

|              |

|             / \

|            /   \        (1 line)

|           /     \

|          /  Is   \  Yes

(Label)   |         [  Count  ]-------------> DisplayResult

????      |          \  '0'  /

|           \     /

|            \   /

|             \ /

|              | No

|              |

|       ---------------   (3 lines)

|      |               |

|      | Add Second to |

|      |    Result     |

|      |               |

|       ---------------

|              |

|              |

|       ---------------   (3 lines)

|      |               |

|      |   Decrement   |

|      |     Count     |

|      |               |

|       ---------------

|              |

--------------           (1 line)
``````
0

LVL 16

Expert Comment

If you don't mind me asking, what college course are you doing this assignment for?
0

Author Comment

I´m doing a University Course. It´s a basic course in computer Science.. I don´t know why I have such a problem with this language.. the other assignments I have finished by myself. I think Java is more logical..

I have changed "DisplayResult" ti "DisplayRes" because the simulator applet that I´m using are not accepting "DisplayResult". It has to many letter... it can only be 9 letters.

I´m at work again.. but I´m looking at the code when I can. Otherwise I do it at home tonight...
0

LVL 16

Expert Comment

Lisa

Don't look upon it as a computer language. Imagine LMC's instruction are simple single instructions you might give a blind person, or a driver, to perform a task. Each instruction does ONLY just one small operation. The challenge is to use this limited set of instructions in a logical order to achieve the task.

0

Author Comment

I´m still looking att the decrement part...
``````Multiply      	LDA Zero

STA Result

STA Count

Loop 		LDA Count

SUB One

BRZ DisplayRes

LDA Result

STA Result

*

*

BRZ Loop

DisplayRes	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero		DAT 0

Count		DAT
``````
0

LVL 16

Assisted Solution

in the 3rd line of Multiply (ADD First) it's a bit confusing although it will work (but only because at this stage Result is zero) however, the "Set Count=First" flowchart instruction is basically saying "Get First, and shove it into Count"... so the use of 'ADD' would not be the best choice here....  perhaps you can think of another way of getting First into Count.

i'm trying to avoid using abbreviations for labels. I note labels can be upto 10 characters long. i'm wondering whether you would consider using "ShowResult" instead of "DisplayRes"...

I can see your label "Loop" and I can also see "BRZ DisplayRes". Looking at the flowchart, I can see the label "Loop" and I can also see the instruction "If Count is '0' then branch to DisplayResult".... but where did the two lines "LDA Count" and "SUB One" come from?... This isn't shown on the flowchart. (There's a VERY good reason why this would not work if you decremented Count at this stage).

"LDA Result", "ADD Second", "STA Result" is GREAT!!!

Oops! there's some code missing here.... I wonder where this disappeared to?......

"BRZ Loop" is good.

also....

"Zero  DAT  0" is GREAT!!!

In my next reply, i want to discuss how this multiply routine works - you MUST understand this.
0

Author Comment

Hi again,
"ADD First" should be "LDA First".

Now I have replaced "DisplayRes" with "ShowResult". Your suggestion was much better.. :)
I have also replaced "Count" with "Counter". I hope that´s ok! Now when I´m lerning more... I think Counter is more logical.

``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

STA Result

BRA ShowResult

Multiply      	LDA Zero

STA Result

LDA First

STA Counter

Loop 		BRZ ShowResult

LDA Result

STA Result

LDA Counter

SUB One

STA Counter

BRZ Loop

ShowResult	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero		DAT 0

Counter		DAT
``````
0

Author Comment

t0t0>
"I can see your label "Loop" and I can also see "BRZ DisplayRes". Looking at the flowchart, I can see the label "Loop" and I can also see the instruction "If Count is '0' then branch to DisplayResult".... but where did the two lines "LDA Count" and "SUB One" come from?... This isn't shown on the flowchart".

I don´t really know what I was thinking about. I hope my new code is better..

I guess the next step is to get the LMC to understand which of the two inputs is the greatest?

0

LVL 16

Assisted Solution

Right....

Firstly, I notice you've "Count" to "Counter"....Hmmmm.... okay...as long as you're aware of this.....

Secondly, Multiply is almost corrent. It's only let down by the last line. This is what it should look like:
.

``````                      Multiply

|

---------------

|               |  LDA Zero

| Set Result=0  |  STA Result

|               |

---------------

|

|

---------------

|               |  LDA First

|Set Count=First|  STA Count

|               |

---------------

|

(Loop)   ------------->|

|              |

|             / \

|            /   \

|           /     \             BRZ DisplayResult

|          /  Is   \  Yes

|         [  Count  ]-------------> DisplayResult

|          \  '0'  /

|           \     /

|            \   /

|             \ /

|              | No

|              |

|       ---------------

|      |               |  LDA Result

|      |    Result     |  STA Result

|      |               |

|       ---------------

|              |

|              |

|       ---------------

|      |               |  LDA Count

|      |   Decrement   |  SUB One

|      |     Count     |  STA Count

|      |               |

|       ---------------

|              |

--------------           BRA Loop

So, your code should look like the following:

Multiply      	LDA Zero

STA Result

LDA First

STA Counter

Loop 		BRZ ShowResult

LDA Result

STA Result

LDA Counter

SUB One

STA Counter

BRA Loop

.
``````
0

Author Comment

right.. because I want it to always branch to the beginning of the loop!

I changed "Counter" back to "Count" again.. :)
``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

STA Result

BRA ShowResult

Multiply      	LDA Zero

STA Result

LDA First

STA Count

Loop 		BRZ ShowResult

LDA Result

STA Result

LDA Count

SUB One

STA Count

BRA Loop

ShowResult	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero		DAT 0

Count		DAT
``````
0

Author Comment

it works! :)
0

LVL 16

Expert Comment

It looks GREAT!!!! Have you checked if it works?
0

LVL 16

Expert Comment

Sorry, you already answered ...... of course it works!!..... now for some serious stuff....
0

LVL 16

Assisted Solution

Lisa - YOU MUST READ THIS

How the multiple section works:

YOU NEED TO KNOW THIS

To multiply ANY number by another number, you simply add one of the numbers to itself as many times as the value of the other number. For example:

3 X 5

Here, '3' is the number of times we're going to add '5' to itself.

Each time we add '5' to itself, we need to keep a running total.

Also, each time we add '5' to itself, we need to keep track of how many times we do it. We can do this by simply by keeping a count.

Normally, we would count up from 1 to 3 however, we could just as easily count down from 3 to 1, and if we reach '0' then we know we're done.

Before we do any of this, we need consider how we're going to store our data value.

We input two numbers: '3' and '5'....

First    Second
[ 3 ]     [ 5 ]

'3' is the number of times we're going to add '5' to itself, so '3' is our 'count' value (so, whatever, number is in FIRST, this number will be cour 'count' value).

In our program, we move the '3' to 'Count' i.e,

First    Second    Count
[ 3 ]     [ 5 ]           [ 3 ]

We now need somewhere we can store a running total, and we need to reset this to '0'.

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 3 ]        [ 0 ]

STEP 1. Add 'Second' to 'Running Total': 0 + 5 = 5. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 3 ]        [ 5 ]

STEP 2. Subtract '1' from 'Count': 3 - 1 = 2. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 2 ]        [ 5 ]

STEP 3. If 'Count' is NOT '0', go back to STEP 1. 'Count' is not '0' so....

STEP 1. Add 'Second' to 'Running Total': 5 + 5 = 10. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 2 ]        [ 10 ]

STEP 2. Subtract '1' from 'Count': 2 - 1 = 1. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 1 ]        [ 10 ]

STEP 3. If 'Count' is NOT '0', go back to STEP 1. 'Count' is not '0' so....

STEP 1. Add 'Second' to 'Running Total': 10 + 5 = 15. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 1 ]        [ 15 ]

STEP 2. Subtract '1' from 'Count': 1 - 1 = 0. We now have....

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 0 ]        [ 15 ]

STEP 3. If 'Count' is NOT '0', go back to STEP 1. But 'Count' DOES equal '0' and we can stop at this point. The result of '3 x 5' is in our 'Running Total' - '15'.

NOTE: Instead of calling our running total "Running Total", we've decided to call it "Result".

Notice the following rules with this method.

RULE 1. The first number input is stored in 'First'
RULE 2. The second number input is stored in 'Second'
RULE 3. 'First' is ALWAYS the "number of times" we add 'Second' to itself.
RULE 4. A running total is kept in 'Result'. 'Second' is added to 'Result'
RULE 5. A count is kept of how many times we must add 'Second' to 'Result'. We do this be decrementing 'Frist' during each iteration of our loop.
RULE 6. We repeat this process until 'First' is has counted down to '0'.

NOTE 1: We must never directly change the value '5' (in 'Second') because we need to continually add this value to our running total - which is why we have a separate 'Running Total'.

NOTE 2: We can however, directly change 'First' but we chose not to by moving 'First' to 'Count' and change 'Count' instead. More on this later.

IMPORTANT NOTE: When inputing two numbers, consider what would happen if we enter '5' followed '3' (instead of '3' followed by '5'). We would of had to loop 5 times (adding '3' to itself) instead of just 3 times. This might seem a trivial concern with the numbers '3' and '5', but suppose we entered '45' and '3'? Do you see what would happen? We would loop 45 times - and this is wasteful of time and effort.

To OPTIMISE our code then, we could swap the values in 'First' and 'Second' so that the smaller number is always in 'First' (remember, 'First is the number of times we loop our program).

Remember, earlier I said there are 3 parts to the multiplication routine? This is what I wrote:

Pt.1: reset Result to '0'
Pt.2: ??????????????? <--- I'll leave you to stew over this one for a bit
Pt.3: do the multiplication

Well, you've guessed it, we need to optimise our code by swapping the order of our two numbers to suit the RULES above - in particular, RULE 3. Here are the three parts to our multiplcation routine:

Pt.1: reset Result to '0'
Pt.2: swap numbers so FIRST is the smaller number
Pt.3: do the multiplication

Please look carefully at the following flowchart snippet. All I show you here is HOW to test and swap the numbers. We'll tie this into our code in a moment. Can you think of a way of coding this? (HINT: Use the BRP instruction).
.
``````.

|

|

/ \                3 lines of code

/is \

/First\

/smaller\  Yes

[  than   ]----------

\Second?/           |

\     /            |

\   /             |

\ /              |

| No            |

|               |

Swap              ---------------        |  6 lines of code

|               |       |

|  Swap First   |       |

|  and Second   |       |

|               |       |

---------------        |

|               |

NoSwap                   |<--------------

|

.
``````
0

Author Comment

something like this?

``````Multiply      	LDA Zero

STA Result

LDA Second

SUB One

BRP NoSwap

Swap    		LDA First

STA Count

LDA Second

STA First

NoSwap		LDA Second

STA Count

LDA First

STA Count

Loop 		BRZ ShowResult

LDA Result

STA Result

LDA Count

SUB One

STA Count

BRA Loop

ShowResult	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero		DAT 0

Count		DAT
``````
0

LVL 16

Assisted Solution

Okay, I can see you've had a go at coding the 'swap' routine. I can see some mistakes....

YOU NEED TO KNOW THIS

Looking at the flowchart, there are just two operations that need to be performed:

1) Is First smaller then Second? If so, branch to NoSwap
2) Swap First and Second

Ideally, we want the smaller value to be in First. Remember from our previous discussion, First is the number of times we add the other number to itself. The value in First specifies how many times our program loops around.

There are basicall three conditions that may occur when inputing two numbers:

a) First is greater than Second                 <------- Not ideal
b) Second is greated than First                <------- Ideal
c) First and Second are equal                  <------- Ideal

So, to test which is greater, we simply take one away from the other. This sounds easy however, we must be careful to get this the right way around.

Suppose we enter '5' and '3' then this is what we get:

First    Second    Count     Running Total
[ 5 ]     [ 3 ]           [   ]        [   ]

In this case, we want to swap the '3' and the '5' around so that we end up with this:

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [   ]        [   ]

Let's see what happens if we take First away from Second, and let's do this for 3 different combinations of input:

Run 1. Input 3 and 5
Run 2. Input 5 and 3                    <--------- Not ideal
Run 3. Input 3 and 3

If we take First away from Second then this is what we get for each run:

Run 1. (First = 3, Second = 5) therefore, 5 - 3 = 2            <---------- Ideal
Run 2. (First = 5, Second = 3) therefore, 3 - 5 = -2           <---------- Not ideal (Negative result)
Run 3. (First = 3, Second = 3) therefore, 3 - 3 = 0            <---------- Ideal

In Run 2, we input '5' first and '3' second and this is not ideal. We can see that these need to be swapped around. Subtracting First from Second (3 - 5) gives us a negative result so this would indicate we need to perform a 'Swap' operation.

Therefore, if we get a non-negative (a positive) result then we  don't need to swap First with second - we can just skip the bit of code which does the swap function and go straight on to multiply our numbers.

So, all we have to do is test whether:

Second - First = Negative

to do our swap, or another way of looking at it is:

If Second - First = Positive ..... then goto NoSwap

LMC provides us with an instruction to do just this. It's the BRP instruction. This is: "Branch If Positive".

LISA - CAN YOU CODE THIS?

So, putting it all together, we need to do this test:

Get Second
Subtract First
If the result is Positive, branch past the swap function

And that's that bit over and done with. See how that relates to our flowchart below. Then I'll discuss the actual Swap function.
.

.

``````.

|

|

/ \                (3 lines of code)

/is \

/First\              Get Second

/smaller\  Yes        Subtract First

[  than   ]----------  Branch if result is positive

\Second?/           |

\     /            |

\   /             |

\ /              |

| No            |

|               |

---------------        |  6 lines of code

|               |       |

|  Swap First   |       |

|  and Second   |       |

|               |       |

---------------        |

|               |

|<--------------

|

.
``````
0

LVL 16

Assisted Solution

The Swap routine itself is straight forward - infact, you correctly predicted only 4 instructions are required. I was going to make this point later however, in the meantime, I will continue with 6 instructions as planned for reasons of clarity.

Remember from above, First is the number of times our program loops. We copy First to Count and use Count as our loop counter. The value in Second is then added to our running total each time our program loops.

If we input '3' and '5', we don't have a problem.

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [   ]        [   ]

We copy First to Count and then decrease count by '1' until Count is becomes '0'.

First    Second    Count     Running Total
[ 3 ]     [ 5 ]           [ 3 ]        [   ]
------------------- ^

You may have spotted an obvious flaw with this design: Why don't we just use First as our count. Well, we can, and this will be discussed in our last and final edit of the program however, for now, looking at the code, it becomes more apparent to spot what's going on if we use 'Count' as a counter rather than First.

Suppose then, we input the following two numbers '5' and '3' in this order. We get:

First    Second    Count     Running Total
[ 5 ]     [ 3 ]           [   ]        [   ]

And here, we need to use the '3' as our counter however, to do so, we must first swap First with Second.

Just as we used an extra memory location (Count) as a counter for our loop, we can use another memory location (Temp) to temporarily store one of our numbers while we switch them around. Here are the steps necessary to perform the swap.

1) 'Move' First to a temporary location (Temp)

First    Second    Count     Temp     Running Total
[ 5 ]     [ 3 ]           [   ]         [   ]        [   ]
-------------------------------^

Becomes:

First    Second    Count     Temp     Running Total
[   ]     [ 3 ]           [   ]         [ 5 ]        [   ]

2) 'Move' Second to First

First    Second    Count     Temp     Running Total
[   ]     [ 3 ]           [   ]         [ 5 ]        [   ]
^-------

Becomes:

First    Second    Count     Temp     Running Total
[ 3 ]     [   ]           [   ]         [ 5 ]        [   ]

3) 'Move' Temp to Second

First    Second    Count     Temp     Running Total
[ 3 ]     [   ]           [   ]         [ 5 ]        [   ]
^----------------------

Becomes:

First    Second    Count     Temp     Running Total
[ 3 ]     [ 5 ]           [   ]         [   ]        [   ]

And there we have it. First and Second have been swapped.

We define and subsequently use 'Temp' as an additional storage location. We could just as well used 'Count' as a temporary storage location, or even 'Result' for that matter although this would require us to change the order of our code - so that we do the swap (if required) first, then initialise Result and finally the loop. And it is issues like these that I want to discuss as our final solution.

In the meantime though, the Multiply section of our code should look something like this:
.
``````Multiply    LDA  Zero       <--- Initialise Result

STA  Result

LDA  Second     <--- Test First is smaller than Second

SUB  First

BRP  NoSwap     <--- Skip the Swap routine

Swap        LDA  First      <--- Entry point for Swap

STA  Temp         |

LDA  Second       |

STA  First        |

LDA  Temp         |

STA  Second     <-

NoSwap      LDA  First      <--- Initialise loop counter

STA  Count

Loop        LDA  Count      <--- Test if counter is '0'

BRZ  ShowResult <--- If it is, exit loop

LDA  Result     <--- Add Second to running total

STA  Result     <-

LDA  Count      <--- Decrement loop counter

SUB  One          |

STA  Count      <-

BRA  Loop       <--- Repeat loop

.
``````
0

Author Comment

I wrote "One" by mistake.. I´m sure that I wrote "First" but I see now that I wrote "One" by mistake. But I was thinking right anyway.. :)

I´m listening (reading) and learning.. :)
``````GetInput    	INP

STA First

INP

STA Second

GetOption  	INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

STA Result

BRA ShowResult

Multiply      	LDA Zero

STA Result

LDA Second

SUB First

BRP NoSwap

Swap    		LDA  First

STA  Temp

LDA  Second

STA  First

LDA  Temp

STA  Second

NoSwap		LDA First

STA Count

Loop 		LDA Count

BRZ ShowResult

LDA Result

STA Result

LDA Count

SUB One

STA Count

BRA Loop

ShowResult	LDA Result

OUT

End           	HLT

First           	DAT

Second      	DAT

Result		DAT

One           	DAT 1

Zero		DAT 0

Count		DAT

Temp 		DAT
``````
0

LVL 16

Assisted Solution

Hiya Lisa

The code looks good. I believe it works well too.

Notice you have a label called "Swap" but it isn't actually used (as a branch-to point). This is fine... There's no reason why you can't have labels identifying different parts of code. By including this, you demonstrate you are aware of this. Don't over-do it though by putting in more. You've just about hit the rught balance.

The layout is good. It's neat. All your labels are meaningful. All your variable and constant storage names are good and meaningful. You've shown you can initialise data on program entry as well as during run-time. You've also demonstrated insight by only initialising those variables/constants that need to be initialised at this stage. I like it.

The last discussion I'll have with you will be based on your code. As it stands, it does what it's meant to do and at this point, you could just as well submit this as your assignment however, there are areas which can be optimised and improved, and it is these areas I want to concentrate on next.

I hope to get back to you within the next 24 hours. In the mean time, grab yourself a well-deserve cup of coffee and a couple of biscuits for dunking...
0

LVL 16

Expert Comment

Will you have to do a write-up as part of this assignment?

Have you considered what test-data you might use to test the code?
0

Author Comment

I have to write a report, with the code and a explanation to it. I have not considered what test-data I might use.. I have just tested the program and checked that it works.

0

LVL 16

Expert Comment

If I'm correct, even with the best program in the world, if your write-up is crap you could very easily fail this assignment. It wouldn't surprise me if the code itself is worth only 18% of the overall marks!

Okay, I would consider the following to be essential.

1) Introduction
2) Program requirements specification
3) Overall design
4) Overall flowchart (showing only the main control of the program)
5) Refined design identifying individual sections of processes/functions
6) Refined flowcharts for the main sections showing data flow and process control
7) Full description of your data and how your program manages memory storage
8) Skeleton code showing the main control structure of your program
9) Refined code for each section of your program
10) Detailed explanation of any areas which are improved/optimised/compromised
11) Full program listing with remarks
12) Full description of test data / table showing expected and actual results
13) Description of LMC, it's limitations, it's user-friendliness
14) How you went about researching and completing the assignment
15) conclusion
16) Glossary of LMC's instructions

At the very most, half page for each (except the glossary and flowcharts)

When does your assignment need to be handed in?
Are you a mature student or a recent school-leaver?
Have you got the details of what you're supposed to do for your asignment?
Do you have to give a self appraisal?

0

Author Comment

This is a small basic asignment and non of the information that I have got shows that I have to do such a big write-up. But thank you for the information I will take it under consideration for my future asignments. :)

I have a few days left until I have to hand it in. I´m a "mature" student but it is not long ago that I studied at the university last time but then it was not Computer Science. I have all the details and I don´t have to give myself appraisal this time.

Thank you very much for all the time and effort that you have given me! If I could I would give you 1000 points... :) I will close this question as soon as I pass the asignment!
0

LVL 16

Assisted Solution

Hiya

If it's only code you want me to help you with then permit me to make some adjustments.

The most obvious improvement that can be made is in the Addition section of the code..

Addition: You currently have the following code:

STA Result
BRA ShowResult

There's no real need to store the result and branch to another section of the code to display it. We could just as well do the following (and it saves time):

OUT
HLT

The code below shows other changes which include omiting the need for Temp, Count and Zero. The code assumes First will always be the Numerator and Second will always be the denominator. Result is used instead of Temp when swapping First and Second. Also, the instruction HLT has an opcode of '0' so we can just grab that instead of declaring and using a constant 'Zero'. First now takes the place of Count:
.
``````.

GetInput    INP

STA First

INP

STA Second

GetOption   INP

BRZ End

SUB One

SUB One

BRZ Multiply

BRA GetOption

OUT

HLT

Multiply	   LDA Second

SUB First

BRP NoSwap

Swap 	   LDA Second

STA Result

LDA First

STA Second

LDA Result

STA First

NoSwap      LDA End

STA Result

Loop        LDA First

BRZ ShowResult

SUB One

STA First

LDA Result

STA Result

BRA Loop

ShowResult  LDA Result

OUT

End         HLT

First       DAT

Second      DAT

Result	   DAT

One         DAT 1

.
``````
0

LVL 16

Expert Comment

If you require an explantion of the above final code then let me know.

Don't worry about points however, please consider infinity08's initial help - i feel guilty taking this question away from him.
0

LVL 53

Expert Comment

>> please consider infinity08's initial help - i feel guilty taking this question away from him.

No worries. You handled it great, and deserve them ;)
0

Author Comment

Hi,
my teacher hasn´t checked the assignment get but I will close the question anyway. I´m sure I will pass. :) Thank you both for your help. Although t0t0 helpt me most, it was you Infinity08 who helped me in the beginning. Of course I will take both of your help in consideration when I split up the points. :)
0

Author Closing Comment

I think that you (t0t0) has guided me through my question in a good way.
I ´m very thankful for that. :)
0

LVL 16

Expert Comment

Good luck with your marks. Most of all, I hope you gained from this exercise.

Thank you for posting an interesting question and thank you for accepting my answer and awarding me points.
0

Author Comment

Hi t0t0.
I have learned a lot thanks to you! I´m not a expert... but I think I got a basic understanding for the assembler language. And that is a lot more than I thought when I started this assignment and didn´t understand anything. :)

Have a nice dag!
0

LVL 16

Expert Comment

thank you and good luck
0

LVL 16

Expert Comment

Hiya Lisa...

I was just wondering how you got on with your Littleman Computer Assignment.

Looking at the list of Top Solutions for Assembly, this question ranks the highest.

0

## Featured Post

### Suggested Solutions

Patching a Windows EXE 23 727
Swapping a Int64 var (using ASM) 19 1,090
core dump analysis 26 2,563
How to make a Bootable Program without an OS 4 516
At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
For Sennheiser, comfort, quality and security are high priority areas. This paper addresses the security of Bluetooth technology and the supplementary security that Sennheiser’s Contact Center and Office (CC&O) headsets provide.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…