# What's the difference between A and M? Classic List Threaded 10 messages Open this post in threaded view
|

## What's the difference between A and M?

 I'm trying to get my head around the A and M registers. As far I have understood, A can be both a data value or a memory address. And M = RAM[A]. Therefore M is a data value and therefore both A and M are data values. Is this correct? If so, when do you use A and when do you use M as a data value? Take for example 4.2 in the book which adds 1 + .. + 100: // Adds 1+...+100. @i // i refers to some mem. location. M=1 // i=1 @sum // sum refers to some mem. location. M=0 // sum=0 (LOOP) @i D=M // D=i @100D=D-A // D=i-100@END D;JGT // If (i-100)>0 goto END @i D=M // D=i @sum M=D+M // sum=sum+i @i M=M+1 // i=i+1 @LOOP 0;JMP // Goto LOOP (END) @END 0;JMP // Infinite loop Near the top of the loop, you see the expression: @100 D = D - A         // D = i - 100 In this expression I cannot explain why it's D=D-A instead of D=D-M. From what I understand, @100 points A to memory address 100. But as to what the value is stored inside of memory address 100 is not known. In that case, you can set M = 100, so RAM = 100. Therefore, to get D = i - 100, the last statement should be D=D-M. Please explain to me why this is not the case.
Open this post in threaded view
|

## Re: What's the difference between A and M?

Open this post in threaded view
|

## Re: What's the difference between A and M?

 Hey Ivant, In your example, you alluded to the fact that A is both a data value and a memory address. So when you declare @100, is A storing the value 100 or is 100 the address that A is pointing to, or both? I think this is what is confusing the hell out of me. Everything else like D and M is quite clear to me. It's just A that I can't get my head around!
Open this post in threaded view
|

## Re: What's the difference between A and M?

 Administrator This post was updated on . The value of A is just a bit pattern. The interpretation of what that bit pattern represents depends on how the A register is used. ```@100 // Put the bit pattern 0000 0000 0110 0100 (decimal 100) into the A register. D=A // D = 100 using the A register's value as data D=D+A // D = 200 using the A register's value as data M=D // RAM = 200 using the A register's value as a RAM memory address 0;JMP // Set the PC to address 100. The next instruction will be read from ROM ```Normally, the various usages are not mixed together as in the above example so you see common sequences like ```@123 // D = 123 D=A (CONTINUE_LOOP) ... @CONTINUE_LOOP // goto top of loop 0;JMP @16 // counter = 16 D=A @counter M=D ... @counter // decrement counter M=M-1 ```In that last example, "counter" is not mentioned in any () code label, so it is automatically assigned a RAM location. Check out Introduction to Hack Assembly Language. It may help. If you have any suggestions for improving it, please let me know. --Mark
Open this post in threaded view
|

## Re: What's the difference between A and M?

 cadet1620 wrote The value of A is just a bit pattern. The interpretation of what that bit pattern represents depends on how the A register is used. ```@100 // Put the bit pattern 0000 0000 0110 0100 (decimal 100) into the A register. D=A // D = 100 using the A register's value as data D=D+A // D = 200 using the A register's value as data M=D // RAM = 200 using the A register's value as a RAM memory address 0;JMP // Set the PC to address 100. The next instruction will be read from ROM ```Normally, the various usages are not mixed together as in the above example so you see common sequences like ```@123 // D = 123 D=A (CONTINUE_LOOP) ... @CONTINUE_LOOP // goto top of loop 0;JMP @16 // counter = 16 D=A @counter M=D ... @counter // decrement counter M=M-1 ```In that last example, "counter" is not mentioned in any () code label, so it is automatically assigned a RAM location. Check out Introduction to Hack Assembly Language. It may help. If you have any suggestions for improving it, please let me know. --Mark Going back to your example, if you declare @100, then the value of A = 100 , i.e. the A register reads 0000 0000 0110 0100. Is this right? And if M is a register that is pointing to a memory address of A, i.e. M = RAM[A]. Then in this case, M is pointing to slot in memory with the address 100 right? For example, if RAM has 1000 slots, then slot 100 corresponds to the value of M. If so, what is the value of M in that address of 100 then?
Open this post in threaded view
|

## Re: What's the difference between A and M?

 Administrator riverfish wrote Going back to your example, if you declare @100, then the value of A = 100 , i.e. the A register reads 0000 0000 0110 0100. Is this right? Correct. And if M is a register that is pointing to a memory address of A, i.e. M = RAM[A]. Then in this case, M is pointing to slot in memory with the address 100 right? For example, if RAM has 1000 slots, then slot 100 corresponds to the value of M. Correct.  If so, what is the value of M in that address of 100 then? It might help if you discard the idea of M being a "register". Think of M as a gateway to RAM. Every M in Hack assembly language means RAM[A]. RAM keeps the last value that was written to it. Once you write something to RAM, that value will stay there until a different value is written to RAM. In the real world, if you read a RAM location that has never been written to, you will get an undefined value.  It is important to initialize RAM variables before you use them. (In the n2t tools, RAM is initialized to 0 when the tool starts. Some of the tools reset the RAM to 0 when you restart your program, some of the tools do not. Don't rely on RAM being zeroed.) --Mark
Open this post in threaded view
|

## Re: What's the difference between A and M?

Open this post in threaded view
|

## Re: What's the difference between A and M?

 Administrator I think that you have the concepts, but the nomenclature is a bit off. Variable "i"s address is 16. That is where the assembler placed (allocated) it in the RAM. @i loads the address of "i" into the A register. The A register now contains a pointer to "i". A variable can be a pointer if its purpose is to hold the address of another variable or an address of somewhere defined in memory. For instance, for the Fill program you will be writing to the Screen which is RAM starting at 16384. You might want to use a pointer variable to operate on screen memory. ```@SCREEN // built in symbol for Screen starting address D=A @scrPtr // A = scrPtr's address M=D // scrPtr's value = 16384 ... @scrPtr // A = scrPtr's address A=M // A = scrPtr's value -- the pointer to Screen memory M=1 // RAM[scrPtr] = 1 ... @scrPtr // A = scrPtr's address M=M+1 // scrPtr = scrPtr+1 -- now points to the next word of Screen memory ```--Mark
Open this post in threaded view
|

## Re: What's the difference between A and M?

 Makes sense now. I think the key point that made me understand all this is that the data value of the A-register is an address. Thanks 