Hi all. Or probably WBahn
I'm trying to translate the following task as you might do in a higher-level language into assembly instruction:
arr[j] = 17;
I was able to find a solution to a similar problem where I have to translate
arr[j] = 0
or arr[j] = -1
I cannot assume to know anything about
arr except that its ROM instruction references some memory location in the RAM, which is the 0 index of the array. j assumes M[j] has a value stored, i.e. M[j] = some integer
My translation for arr[j] = 0 is as follows:
@arr // arr is a reference to some ROM location containing the instruction @n, so that A = n
D = A // D = n, where n is the memory location representing index 0 of the 'array'
arr@j // j is a reference to some ROM location containing the instruction @c, so that A = c
D = D + M // D = n + M[c], so that D is the memory location representing index j of the 'array'
arrA = D // We want to change the value stored at RAM[D], but Hack only allows calls to RAM[A],
// so A must equal D so that, at least symbolically, RAM[A] == RAM[D]
M = 0 //As per the c instruction, dest = M[A], and comp = 0
The problem is that this only works because the C instruction allows direct assignment of M to either 0, 1, or -1.
If I want to translate arr[j] = 17,
I can't simply call M = 17.
I also can't store 17 in D, because the following happens:
@17
D = A
// I now need to get the instructions located at ROM[arr] and ROM[j]
@arr
// A now equals the memory location where the array begins, so now I only need to add j
@j
// I've now lost the memory location where the array begins and traded it for the memory location containing the index j. Which is a problem.
I've been thinking about this problem for a long time but I can't figure it out!!!!
Any help is greatly appreciated.