Advertisement
DanFloyd

My solution to "mult problem" of nand2tetris course

Feb 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. // Multiplies R0 and R1 and stores the result in R2.
  2. // (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
  3.  
  4. // Put your code here.
  5.  
  6. @2 // load result
  7. M=0 // zeroing result
  8. @3 // R[3] is the counter
  9. M=1 // set the counter to 1
  10. (LOOP)
  11. @3 // load counter
  12. D=M // store the counter in the data reg
  13. @1 // load 2nd factor
  14. D=D-M // data = data - counter
  15. @END // load end location
  16. D;JGT // if data - counter > 0 goto END
  17. @0 // load 1st factor
  18. D=M // store 1st factor in data reg
  19. @2 // load result
  20. M=M+D // store result
  21. @3
  22. M=M+1 // increase the counter
  23. @LOOP
  24. 0;JMP // unconditionate jump to LOOP
  25. (END)
  26. @END
  27. 0;JMP // infinte loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement