A and D register

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

A and D register

Juan Camilo Valencia Espitia
In Chapter 4, in the overview, i was reading the part "Registers". I'm sure i've read that topic 10 times or more, including the example in Figure 4.2. It is not clear to me what does the A,D and M register

There have to be a simple way to understand this... How do they work?
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

ybakos
Did you complete Project 3? If so, you built a Register.
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

Juan Camilo Valencia Espitia
Yes, i already finished the project 3
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

cadet1620
Administrator
This post was updated on .
In reply to this post by Juan Camilo Valencia Espitia
I've started writing an introduction to Hack programming.  This should help you understand what the A, D and M registers are and how they are used.

Introduction to Hack Assembly Language

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

Juan Camilo Valencia Espitia
I'm gonna read that, thanks Mark
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

ybakos
In reply to this post by Juan Camilo Valencia Espitia
First, there is no M register in hardware. But the A mnemonic does refer to the A Register, which is an actual hardware register; the same goes for D.

All registers just hold 16-bit binary values. The Hack platform specifies that the A register and D register can only be manipulated using certain instructions.

To store a value in the A register, you must use an A instruction, or a C instruction where A is the dest. For example:

@42 // stores 42 in the A register
A=1 // stores 1 in the A register

The D register can only be manipulated using a C instruction. For example:
D=1 // stores 1 in the D register
D=A // copies the value from A and stores it in D

The M symbol can be tricky at first. M refers to a register in memory, but since memory consists of many registers, we have to specify which address in memory we are referring to. To do this, we must first store the address of the register in A, and then we can use M correctly. For example:

@42 // Stores 42 in the A register
D=A // Stores 42 in the D register
@0 // Stores 0 in the A register
M=D // Stores 42 (from D) into Memory address 0
@2 // Stores 2 in the A register
M=D // Stores 42 (from D) into Memory address 2

The M mnemonic always means "the register in memory whose address is currently stored in A."

Reply | Threaded
Open this post in threaded view
|

Re: A and D register

Juan Camilo Valencia Espitia
Thanks ybakos :)
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

enea
In reply to this post by cadet1620
I'm sorry or my bad english, but I don't understand this:

Why

pseudocode n=1

ASM
@n
M=1

and

pseudocode n=17

ASM
@17
D=A
@n
M=D

Can I do this?

pseudocode n=17

ASM
@n
M=17


Reply | Threaded
Open this post in threaded view
|

Re: A and D register

ivant
Because M=1 is a valid command for the CPU (it can "calculate" the value 1 directly in the ALU), while M=17 isn't.

M=0 and M=1 are special cases, which can be handled more effectively by the CPU and are common enough for the VM to produce special code for them. But for the other cases longer code sequence is needed.

Also note that the sequence has to be different for negative numbers.
Reply | Threaded
Open this post in threaded view
|

Re: A and D register

enea
Thank you ivant, i have another two questions:

first:

in
Pseudocode a[123]= 0

ASM
@123
D=A
@a
A=D+A   / why this and not A=D?
M=0

second

Pseudocode a[123]= 17

ASM
@123
D=A
@a
A=D+A  
@17
D=A
@a
M=D

is it correct?

Reply | Threaded
Open this post in threaded view
|

Re: A and D register

cadet1620
Administrator
enea wrote
in
Pseudocode a[123]= 0

ASM
@123
D=A
@a
A=D+A   / why this and not A=D?
M=0
A=D would set A = 123, so the code would set RAM[123] = 0 instead of a[123] = 0.
    @123
    D=A     // D = 123
    @a      // A = address of array 'a', element 0
    A=D+A   // Update A to address of 'a' element 123
    M=0     // Set a[123] to 0
enea wrote
Pseudocode a[123]= 17

ASM
@123
D=A
@a
A=D+A  
@17
D=A
@a
M=D

is it correct?
    A=D+A   // Update A to address of 'a' element 123
    @17     // Sets A = 17, overwriting the A just set.
    D=A
    @a
    M=D     // Sets a[0] = 17

Setting a[123] to an arbitrary value is a bit tricky.

You need to compute the address of a[123] and save it in a temporary variable in RAM, for instance R15.
Then load 17 into D.
Reload the temporary variable into A, and
finally set a[123] to D.

--Mark