Mult.asm

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

Mult.asm

Idrisadeniyi
Good day,

I'm trying to fix multiplication problem but I keep getting a comparison failure at line 2. I know I'm not getting it right. But I needed to be shown the right direction.

@R0
D=M
@6
M=A
D=M

@R1
D=M
@2
M=A
D=M
 
(LOOP)
@END  
D;JLE // IF R0 <=0 END PROGRAM
@R0
D=M

@R1  
D=D-A

@R0
M=D
@LOOP
0;JMP
(END)
0;JMP
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

rleininger
I'm assuming your solving the multiplication problem in Lesson 4.  The problem states that the registers R0 and R1 are to hold the multiplier and multiplicand and the product is to be saved in R2.  In your program, R2 is not used.  In fact, the product (the sum of R0 copies of R1) is not saved anywhere.

I entered your program into the CPU Emulator.  It assembled without error and executed without error.  There is a slight problem with the way you terminate execution using an END label.  It's not quite right.

A good way to proceed on problems of this type is to write a high-level language version of the algorithm you are going to use and translate it into Hack assembly.  For simple programs (without function calls) there is generally a straightforward translation.  Suppose you wanted to multiply 8 times 3.  In Python, for example:

R0 = 8      # multiplicand
R1 = 3      # multiplier
R2 = 0      # product accumulator

while R1 > 0:
    R2 = R2 + R0   # add the multiplicand to product
    R1 = R1 - 1      # decrement the multiplier

The first line in Hack assembly could be written as:

@8
D=A
@R1
M=D

and so on.  Hope this helps.
 
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

WBahn
Administrator
In reply to this post by Idrisadeniyi
In addition to the good advice already given, here's some more general steps to use.

What did your program produce for Line 2 in the .out file and what does the .cmp file say it should produce?

Do you agree that the .cmp file is correct and that the .out file is wrong? Often this is enough to steer you in the right direction.

Walk through your program step by step, tracking what you think the values should be if what you had in mind was happening and look for where they start deviating. Then focus on why the actual values are what they are instead of what you intended.

In looking over your code briefly, I see that you have @6 and @2. Why? Are these meant to be register locations? In which case, use @R6 and @R2 to make your code more readable. If they are simply values (i.e., the number six and the number two), what is there purpose in your approach?
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

Idrisadeniyi
Thanks for the reply. It clarifies some of my confusion. And sorry my late response, sometimes life gets in the way.

I have been able correct some of the errors in my program and I could see the progress as my .cmp file no longer conflicting with my .out file at line 2 but at line 5 now. My initial understanding was that I am suppose to declare the values and put them in the right memory for the multiplication operation, but I realized that the values are set in the script file. At line 5, .cmp file expects 3*1=3 but my .out file is producing 3*1=0.

I think I need to do a bit of modification but I am not sure what to do next.

@3  //
D=A
@R0
M=D

@1
D=A
@R1
M=D

@R2
M=0
 
(LOOP)
@R2
D=M

@R0
D=D+M

@R1
M=M-1

@2
D=A
@R0
M=D

@4
D=A
@R1
M=D

@R2
D=M

@R0
D=D+M

@R1
M=M-1
@LOOP
D;JGT // IF R0 >0 RUN THE ABOVE PROGRAMS
(END)
@END
0;JMP
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

rleininger
This post was updated on .
Completely understand "life gets in the way."  Substantial progress has been made.  I note that with the statement D=D+M you add the multiplicand (R0 = 3) to the current value of the product accumulator (stored in R2).  As I mentioned in my first reply, you don't seem to be saving this "running product" result anywhere.  I would start fixing there.

I had no looked at the rest script until now.  The script initializes the multiplicand and multiplier, so, contrary to my pervious advice, your program should initialize them.  As the script comments indicate, your program must still handle initialization of the product accumulator.

After you correctly decrement the multiplier (stored in R1) with the statement M=M-1, what it the intent of the following code down to @LOOP?

Still a problem with the loop condition.  What's in D when you execute "D;JGT"?  What happens in the case where the multiplier (R1) starts at zero?  There may be a logic problem here: "while" loop versus "do...while" loop (in C language).

Your termination "infinite loop" now looks good.

Hope this helps.  You are really close to a correct solution.
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

Idrisadeniyi
I think I need more clarification. I have modified the program several times, but I'm still stuck in the same place. For instance, I thought by the statement "D=D+M" I'm saving the result of the computation in the D register.

Concerning the loop, I only intend to iterate until the "M=M-1" condition is met.

According to the lesson, I think D;JGT is suppose to jump to execute @LOOP which is a reference to (LOOP). My understanding may be deficient.    
Reply | Threaded
Open this post in threaded view
|

Re: Mult.asm

rleininger
I am looking at the last version of your program that was posted.  It would be really helpful if you would include a comment with each line to explain what you intend the statement to accomplish.

You are right about the statement D=D+M.  However, remember the problem specification requires that the product end up in R2.  Also, registers D and A are used for temporary operands or temporary storage of results.  In the apparent algorithm you are using to compute the product, which employs a loop construct, the D register does not contain the product until the very last iteration.  You must do something with the "partial" product during each iteration of the loop.

I'm not exactly sure how to respond to your comment concerning the loop.  Not knowing the level of your programming experience, I apologize in advance if the following is below that level.

High-level languages generally provide a syntax for three main kinds of looping constructs.  A pre-condition conditional loop (while<condition==True>{...}), a post condition conditional loop (do{...}while<condition==True>), and a counting loop (for<N times>{...})  Which kind do you have?  

In the Hack assembly language, there is only one kind of looping statement, an arithmetic conditional loop with 6 different condition variations.  Fortunately, any of the types of loops available in high-level languages can be constructed with this kind of looping statement. Placement of the loop statement in the program and the arithmetic condition evaluated determines this.

The hardest thing in constructing assembly language loops, for me anyway, is insuring that the loop is iterated the correct number of times for every possible starting condition.  I will call the block of statements in the loop that are executed in each iteration the body of the loop.  One thing you need to decide at the start is whether there are conditions for which the loop should not execute the body even one time or whether the loop should always execute the body at least once.  Which case do you have for this problem?  Is there a starting condition for which the loop body should never execute or must we always execute the body at least once?  Your program appears to currently be using a post-condition type of conditional loop, so the loop body will always execute at least one time.

In reading your code, it seems you are using R0 as the multiplicand and R1 as the multiplier; that is, the product (R2) is R0 + R0 + ... + R0, where the number of terms is equal to R1.  You need to look at what is in the D register when the conditional statement executes.  Is it what you want?

The test script sets various values for R0 and R1.  You don't need to set values for R0 and R1 in your program.  Look at the values that will be tested by the script.  This will help you determine which kind of loop you need to use.