Question: Why is my HACK ASM wrong? //addn.asm //This asm computes the sum of n numbers //Assuming RAM[0] stores the value n (how many numbers to
Why is my HACK ASM wrong?
//addn.asm
//This asm computes the sum of n numbers
//Assuming RAM[0] stores the value n (how many numbers to sum)
//and the numbers are stored in consecutive addresses starting at address 1.
//The result will be stored in RAM[0], overwriting the original value of n.
//It may be useful to use variables when solving this problem.
//Write your code below.
@R0
D=M
@N
M=D
@R1
D=M
@Y
M=D
// //while (i > 0){
// A = A+1
// D = D + M
// i --
// }
@i
M = 0
@sum
M=0
(loop)
//If i < 16 or if i >= 0 break the loop
@i
D=M
@C
D=D-M //i-n
@cont
D; JGE
//sum = Ram[0]
@i
D=M
@Y
A = D+M
D=M
@sum
M=D+M
@i
M=M+1
@loop
0; JMP
(cont)
@sum
D=M
@R0
M=D
(end)
@end
0;JMP
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
