Help with Mult.asm

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

Help with Mult.asm

asdffoo
Sorry, I'm sure this kind of things gets posted a lot.

Mult.asm

// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/04/Mult.asm

// Multiplies R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)

// 2, 3
// Put your code here.

// bail early
@R2
M = 0
@R0
D = M
@END
D;JEQ
@R1
D = M
@END
D;JEQ

@R1
D = M
@i
M = D // set up counter - count down from R1

(LOOP)
@R0
D = M
@R2
M = M + D

@i
M = M - 1

@i
D = M

@loop
D;JEQ

@end
D;JMP

(END)
  @end
  0;JMP

OUTPUT
|  RAM[0]  |  RAM[1]  |  RAM[2]  |
|       0  |       0  |       0  |
|       1  |       0  |       0  |
|       0  |       2  |       0  |
|       3  |       1  |       3  |
|       2  |       4  |       2  |
|       6  |       7  |       6  |

It looks like it's setting @R2 to @R0 no matter what.
Reply | Threaded
Open this post in threaded view
|

Re: Help with Mult.asm

WBahn
Administrator
Pick one of the cases that is not producing the correct result and work it by hand one instruction at a time. You should find your mistake very quickly.