# Mult.asm problem

11 messages
Open this post in threaded view
|

## Mult.asm problem

 This post was updated on . I ran a mult.asm with a CPU emulator. I have 3 comparison failures at lines 3, 6, and 7. ```@R1 // creates a copy of R1 D=M @memory M=D // end @R2 // zeros R2 M=0 // end // Calculate! (LOOP) @LOOP @R0 // Calculate R0 times R1 D=M @R2 M=M+D @memory D=M D=D-1 M=D-1 @LOOP D;JGT // end of loop R0 times R1 (END) @END 0;JMP "Buggy list" |R0 |R1 |R2 | |0 |0 |0 | |1 |0 |1 | |0 |2 |0 | |3 |1 |3 | |2 |4 |6 | |6 |7 |24 | ``` Thanks in advance, Wm
Open this post in threaded view
|

## Re: Mult.asm problem

 Administrator I see two problems with this code. The first is that the loop always executes at least one time, so when the program tries to multiply by 0, it ends up multiplying by 1. The second problem is that the loop decrements 'number' by 2, so the program doesn't go through the loop enough times. --Mark
Open this post in threaded view
|

## Re: Mult.asm problem

 I smashed a bug related to the second problem. Please rephrase your first problem comment or give me another hint.  Comparison failure at line 3. Thanks, Wm
Open this post in threaded view
|

## Re: Mult.asm problem

 Administrator Your current code computes prod = a * b as: ```prod = 0 do { prod = prod + a b = b - 1 } while b > 0 ```So when it computes 1 * 0, it does: ```prod = 0 prod = 0 + 1 = 1 b = 0 - 1 = -1 ```and exits the loop with prod = -1, instead of 0. --Mark
Open this post in threaded view
|

## Re: Mult.asm problem

 This post was updated on . I am very sorry.  I did not understand the recent comment.  Please give me another hint.  I haven't take any courses for a Java, a C, a C++, nor a C#.  Wm
Open this post in threaded view
|

## Re: Mult.asm problem

Basically, there are two types of loop structures: the pretest loop and the posttest loop.

The pretest loop is used when the instructions in the body of the loop are to be executed zero or more times>. The posttest loop executes its body one or more times.

 Pretest Loop if/gotoimplementation ```LOOP: if (condition) goto EXIT ... goto LOOP EXIT: ``` while implementation ```while (condition) { ... } ``` Posttest Loop if/goto implementation ```LOOP: ... if (condition) goto LOOP ``` while implementation ```do { ... } while (condition) ```

The Mult.asm program needs to use the pretest loop structure because the multiplier can be zero, in which case the loop must exit before it does the addition.

```(LOOP)
@memory
D=M
@END
D;JLE

@R0     // Calculate R0 times R1
...
@memory
M=M-1
@LOOP
D;JMP
(END)
```
--Mark
Open this post in threaded view
|

## Re: Mult.asm problem

 This post was updated on . To reduce my confusion on my own side, I better upload my file here or somewhere else.  I think I got it.  But I changed my code to clarify the program flow.  Now, the message, which is within the code, doesn't match with the email message.  It was my own mistake. ``` Multiplier X Multiplicand ___________ Product```Sorry, file removed from here.Thanks, Wm
Open this post in threaded view
|

## Re: Mult.asm problem

 Administrator Congratulations on getting your Mult.asm working. Please edit your post to remove the working code. We want students to develop their own solutions. --Mark
Open this post in threaded view
|

## Re: Mult.asm problem

 Administrator In reply to this post by William DesLauriers My misunderstanding this time...there's still a bug.  I'll take a look at it. --Mark
 Administrator In reply to this post by William DesLauriers You have multiplier and multiplicand swapped in the body of the loop (the test at the top of the loop is correct.) multiplicand is what is being summed to make the product; multiplier is the loop counter. I like the optimization in your loop to break at the end with the ``` D;JMP ```instead of the normal unconditional jump ``` 0;JMP ```That's a nice insight! --Mark