/4/SumTo100.asm

http://github.com/happy4crazy/elements_of_computing_systems · Assembly · 23 lines · 23 code · 0 blank · 0 comment · 0 complexity · 7e05a592900633e1d11dc85bea163ce6 MD5 · raw file

  1. // Adds 1+...100.
  2. @i
  3. M=1 // i = 1
  4. @sum
  5. M=0 // sum = 0
  6. (LOOP)
  7. @i
  8. D=M // set the D register to be i
  9. @100
  10. D=D-A // set D to be i - 100
  11. @END
  12. D;JGT // If D = (i-100) > 0 goto END, you've been through the loop 100 times
  13. @i
  14. D=M // set D to be i
  15. @sum
  16. M=D+M // set sum to be sum + i
  17. @i
  18. M=M+1 // increment i by 1
  19. @LOOP
  20. 0;JMP // unconditionally jump back up to (LOOP)
  21. (END)
  22. @END
  23. 0;JMP // Infinite loop: jump to end, which jumps to end, which...