project04: Complete mult.asm

This commit is contained in:
2025-09-06 18:55:52 -04:00
parent 1e536422b8
commit b447557af8

View File

@@ -7,4 +7,36 @@
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// The algorithm is based on repetitive addition.
//// Replace this comment with your code.
// excuse excessive comments here..
// init r2 to 0
@2 // sel r2
M=0 // set it to 0
(LOOP) // begin loop
// check if r1 = 0
@1 // sel r1
D=M // load r1 to data from mem
@END // sel END
D;JEQ // jump to END if r1 = 0 (nothing left to add, we're done)
// add r0 to r2
@2 // sel r2
D=M // load r2 to data
@0 // sel r0
D=D+M // add r0 to r2 (r2 in mem, r0 in data)
@2 // sel r2
M=D // store result in r2
// r1 -= 1 (decrement)
@1 // sel r1
M=M-1 // decrement r1
// check if r1 = 0 (jump back)
@LOOP // sel LOOP
0;JMP // jump to LOOP
// end
(END) // label for jump
0;JMP // from textbook, infinite loop?