# Mult.asm Classic List Threaded 7 messages Open this post in threaded view
|

## Mult.asm

 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
Open this post in threaded view
|

## Re: Mult.asm

 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.
Open this post in threaded view
|

## Re: Mult.asm

Open this post in threaded view
|

## Re: Mult.asm

 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
Open this post in threaded view
|

## Re: Mult.asm

 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.