MULTIPLICATION PROGRAM:

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

MULTIPLICATION PROGRAM:

Dan1984mor2017
I need help. I have no clue where to start in creating this multiplication program. Any suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

WBahn
Administrator
For this one, pretty much any valid approach, no matter how inefficient, will work.

So how would you teach a young child to multiply fairly small numbers together, such as 7 x 6?

Think of how you might explain to them what multiplication is, in terms of addition.
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

Dan1984mor2017
I think I understand what you are saying and in my way I would teach a child to add 6 to 6 a amount of seven times. Of which would give an answer. But I guess I am confused now how to get the computer to loop seven times?
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

WBahn
Administrator
So this is progress. You have taken the original program and broken it down into a couple of simpler tasks.

To multiply A by B, add A to a running total B times.

So you need to be able to "add A to a running total" and you need to be able to do this "B times".

The whole "multiplication" issue is solved; now you are focused on solving a simpler problem -- how to do something B times.

So take a step aside and try to make a simpler program that does little more than do something B times.

How about something like this:

Given a value stored in R0, write a program that initializes R2 to zero and then adds 1 to the value in R2 a number of times equal to the value in R0. In other words, if R0 had 5 in it, then the value of R0 should go 0, 1, 2, 3, 4, 5 and then the program should stop. You know it works if the final value in R2 is always equal to the initial value that was in R0.

How would you tell a child to do it if this child could only see the numbers written on a small number of index cards? What they can do with those cards is limited to the basic things that Hack hardware can do.
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

Dan1984mor2017
I was able to make a program that did a selected countdown from a certain number. Will that help?
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

WBahn
Administrator
Dan1984mor2017 wrote
I was able to make a program that did a selected countdown from a certain number. Will that help?
Yes, that program is a loop that executes a certain number of times and that number is determined by the value stored in a register.

So how could you apply the ability to do that to achieve multiplication in which you add one number to a running sum a certain number of times and that number is determined by the value stored in a register?

Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

Dan1984mor2017
I have been able to create a little program that will countdown from 10 or any input actual. But my program seems to keep adding the same numbers over and over again. It loops but does not loop the addition part. I'm confused.
Reply | Threaded
Open this post in threaded view
|

Re: MULTIPLICATION PROGRAM:

WBahn
Administrator
Dan1984mor2017 wrote
I have been able to create a little program that will countdown from 10 or any input actual. But my program seems to keep adding the same numbers over and over again. It loops but does not loop the addition part. I'm confused.
So desk check it and see what it does compared to what you want it to do.

It would be really helpful if you where to indicate what the values in the five registers you use are meant to represent.

But regardless, let's look at your final two instructions:

@R0
D=M;JGT

What will these two instructions do? Not what you think you would want them to do, but what will they actually do?

@R0 will load the A register with the value 0.

D=M;JGT will copy the value stored at RAM[0] into the D register and, if that value is strictly greater than 0, it will jump to the instruction whose address is currently stored in the A register (which is 0 due to the prior instruction).

Is this really what you want?

Even if it is, throughout your entire program there ONLY register whose value you EVER change is the value stored in R2. Hence, whatever value is initially stored in R0 will ALWAYS be the value stored in R0 when the last two instructions are reached.

Is this really what you want?

A good first step is to express what you really want to have happen using terms that a second grader could easily follow. For instance, something like:

Set up:
Name the following index cards: A, D, R0, R1
(write those names in pen in the upper right corner of the card).
On Card R0, write the value 6 (in pencil).
On Card R1, write the value 2 (in pencil).

From this point on, whenever you are told to write a value on a card, you will erase whatever value it has there and replace it (in pencil) with the new value.

Run:
Step 0: IF the value written on Card R0 is less than or equal to zero, goto to Step 5
Step 1: On Card D, write the value that is currently written on Card R0.
Step 2: On Card D, write the value obtained by subtracting the value on Card R1 from the value on Card D.
Step 3: On Card R0, write the value currently written on Card D.
Step 4: Go to Step 0:
Step 5: DONE

These steps can each then be converted to a very few lines of assembly code.







But b