NAND2Tetris / CPU Emulator ROM @o 1 @o 2 @o 3 @o J @o @o 6 7 2 2 2 Screen CL Addr ว asm RAM CL Addr 2 dec▾ 0 0 1 0 2 0 3 0 4 0 5 0 คด Test: Default Edit ► Slow Test Script Compare File Output File Diff Table 1 repeat { 2 ticktock; 3 } 9 7 8 о 0 K Slow fx Fast x0 x1 x2 0 0 Enable Keyboard Key: Char code: 0 A Fast
I cant figure out how to run the following code on CPU Emulator of Nand2Tetris, please help
this is the code I want to run and see the output:
// R0: dividend
// R1: divisor
// R2: quotient
// R3: remainder
// Initialize registers
@0
D=M // D = dividend (R0)
@R2
M=0 // R2 = quotient = 0
@R3
M=D // R3 = remainder = dividend (R0)
// Check if divisor is 0 (to avoid division by zero)
@R1
D=M // D = divisor (R1)
@DIVIDE
D;JEQ // If divisor is 0, jump to END
@LOOP
@R1
D=M // D = divisor (R1)
@R3
D=D-M // D = remainder - divisor
@CHECK // Check if remainder is still non-negative
D;JGE // If D >= 0, continue with subtraction
// If remainder < divisor, we've found the quotient
@END
0;JMP // Jump to END
@R2
M=M+1 // Increment quotient (R2)
@R3
D=M // D = remainder
@R1
D=D-M // D = remainder - divisor
@R3
M=D // Update remainder (R3)
@LOOP // Repeat the loop
0;JMP
// End of program
(END)
@END
0;JMP // Infinite loop to end program (or you can implement a HALT)
Unlock instant AI solutions
Tap the button
to generate a solution