# Mult.asm

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

## Re: Mult.asm

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

## Re: Mult.asm

Open this post in threaded view
|

## Re: Mult.asm

 Thanks for the detailed explanation. Although I am very familiar with the loop concept you mentioned but refreshing ones' mind doesn't harm in anyway. Actually, my determination was that anytime I am going to reply to this message, it will be to give you a glad tiding that I did it. But I've been on it all this while and I couldn't seem to be getting it right. Although progress has been made. It appears that the line: @R2 M=M+D is not adding things up. D is holding the value contained in R0 and I intend to add it to R2, but I keep getting comparison failure at line 5 and .out file is producing R2=0 where R0=3 and R1=1. Here is the new version of my program. THANKS. @i //variable i refers to some memory location D=M // initialize i to the value of RAM[0] set in the test file @R0 M=D // R0 = i @j //variable j refers to some memory location D=M // initialize j to the value of RAM[1] set in the test file @R1 M=D // R1 = j @R2 M=0 // R2 = 0   (LOOP) // LABEL DECLARATION @R0 D=M // D = 3 @R2 M=M+D // R2=R2+3 @R1 M=M-1 // R1=R1-1 D=M   // D = R1 @LOOP //Jump to (LOOP) D;JGT // IF R1 >0 GOTO (LOOP) (LOOP) @i D=M @R0 M=D D=M @R2 M=D+M @j D=M @R1 M=D M=M-1 D=M @LOOP D;JGT (LOOP) @i D=M @R0 M=D D=M @R2 M=D+M @j D=M @R1 M=D M=M-1 @LOOP D;JGT (LOOP) @i D=M @R0 M=D D=M @R2 M=D+M @j D=M @R1 M=D M=M-1 @LOOP D;JGT (LOOP) @i D=M @R0 M=D D=M @R2 M=D+M @j D=M @R1 M=D M=M-1 @LOOP D;JGT (LOOP) @i D=M @R0 M=D D=M @R2 M=D+M @j D=M @R1 M=D M=M-1 @LOOP D;JGT  (END) @END 0;JMP
Open this post in threaded view
|

## Re: Mult.asm

 Administrator You've got the (LOOP) label declared in multiple places. You can jump to a label from many places, but you can only declare a label once.