Translating a task into an assembly instruction

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Translating a task into an assembly instruction

Infidian
This post was updated on .
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'arr

A = 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.
Reply | Threaded
Open this post in threaded view
|

Re: Translating a task into an assembly instruction

WBahn
Administrator
This post was updated on .
Let's tackle the simple one and see if that gives you ideas for how to proceed with the bigger problem.

High level task:

x = 42;

In Hack assembly, we have to always be aware that we have extremely limited resources. We have the A and the D register and the A register has to do double-duty as both a second data register and as an address register. So we have to sequence things very carefully.

So how about this:

1) Get the value we want to store into the D register and then be careful not to do anything to mess this up.
2) Get the address where we want to store the value into the A register.
3) Store the contents of D into the RAM location referenced by A.

Reply | Threaded
Open this post in threaded view
|

Re: Translating a task into an assembly instruction

Infidian
This post was updated on .
To translate the high level task x = 42; I would do the following:

Step 1) Get the value we want to store into the D register
@42
D = A

Step 2) Get the address where we want to store the value into the A register
@x

Step 3) Store the contents of D into the RAM location
M = D;

Thinking about the solution in terms of steps really helped! Of course, there are some extra steps and the steps have been rearranged slightly, but here is the solution I arrived at:
------------
In the case of arr[j] = 17:

// Step 1) Get the address of arr[j] and store it in the D register

@arr //Get the base address of the array
D = A

@j
D = D + M //Add j to the base address to get the address of arr[j]

// Step 2) Store the address of arr[j] somewhere where it can be easily loaded into the A register
// at a later time. We can't keep it in D because we need D for further computations

@temp
M = D

// Step 3) Load the value we want to store at arr[j] into the D register
@17
D = A

// Step 4) Load the memory address we want into the A register by calling @temp and store D in M

@temp //In the ROM, @temp refers to some @c, where c is the memory address in the RAM where
          //the memory address of arr[j] is stored        

A = M //Load the memory address of arr[j], saved in RAM[c], into the A register

M = D //Load D into RAM[RAM[c]], which is equivalent to arr[j]

Thanks WBahn. Did you have a different solution in mind?
Reply | Threaded
Open this post in threaded view
|

Re: Translating a task into an assembly instruction

WBahn
Administrator
Nope. You nailed it. And your comments about what is happening, and why, in each step are spot on.

Great job!