How to interpreate M and A?

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

How to interpreate M and A?

moonwalk
i get confused about the use of M and A.
in the example,
D= Memory[516]-1;
A=516;
D=M-1;
In my understanding, the M is like the content of Memory[516], just like *p in the C language, and p equals A here.

However, in the example:
// 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
       @100
       D=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
1. in the D=D-A, why don't we use M instead of A?

2. by @100 we get A=100,
    by @i do we get A=i?
then how does the system distinguish which one to use? or it's only used once and dropped?

3. can we understand in the way that @value means we instruct the location to operate and then the next line, on what to operate?
if it's ok, how to understand the logic of the code?
in my understanding,
1. the lines above the LOOP, are to initialize the i and sum;
2. in the lower part, @sum is to save the result and @i is to increment the i and let the loop go.
but, how to interpret the @i , @100, in the middle part:
      @i
       D=M    // D=i
       @100
       D=D-A  // D=i-100
       @END
       D;JGT  // If (i-100)>0 goto END
       @i
       D=M    // D=i
why we need two @i here?

I think i must have understood sth incorrectly somewhere. pls give me some hints.
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to interpreate M and A?

cadet1620
Administrator
Just time for a quick note.  I've got to go to work.  Post back if this doesn't make sense.

You are correct, M is exactly C's "*A".

1.  @100
    D=D-A  // D=i-100
    @END
    D;JGT  // If (i-100)>0 goto END
This is subtracting the constant 100 from  D so that the D;JGT will compare i-100 against 0.

2.  @i is "A = &i" in C.

In summary, the value in A is an integer constant or a variable address
    @123     A = 123
    @symbol  A = &symbol
   
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: How to interpreate M and A?

moonwalk
Wow! Your reply is always so simple and beautiful!!
I have got it!
Thanks so much!!
Reply | Threaded
Open this post in threaded view
|

Re: How to interpreate M and A?

moonwalk
In reply to this post by cadet1620
hi, Mark,
two more things.
1.
Can you please explain to me the difference between the code below?
//n=1
@n
M=1

and
//n=17
@17
D=A
@n
M=D

Can i just write
@n
M=17
?

2. in a post, you replied that
"The first time the Assembler or CPUEmulator encounters a symbol that is not a code symbol, it automatically assigns a RAM address to that symbol, starting with address 16. "
What is the exact location of address 16? is that the 16th register? why is 16? and where is it?

Thanks always!
Reply | Threaded
Open this post in threaded view
|

Re: How to interpreate M and A?

cadet1620
Administrator
You can not write M=17 because 17 is not one of the things that the ALU can compute and therefore is not available in C-instructions.
See figure 4.3 for the computations that can be done.

The only constants that the ALU can compute are 0, 1, and -1, so those are the only values that can be directly set using M=

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

Re: How to interpreate M and A?

moonwalk
Thanks, i got it!
another example in your article:
Here's the pseudo-code for the program:

n = 0
addr = 100
do {
    RAM[addr] = n
    addr = addr+1
    n = n+11
} while n <= 99
Hack program
1:      @n      // n = 0
2:      M=0
3:
4:      @100    // addr = 100
5:      D=A
6:      @addr
7:      M=D
8:
9:  (Loop)      // do {
10:     @n      //     RAM[addr] = n
11:     D=M
12:     @addr
13:     A=M
14:     M=D
15:
16:     @addr   //     addr = addr+1
17:     M=M+1
18:
19:     @11     //     n = n+11
20:     D=A
21:     @n
22:     MD=M+D  // (tricky: also leaves new 'n' value in D)
23:
24:     @99     // } while n <= 99
25:     D=D-A
26:     @Loop
27:     D;JLE
28:
29:     @Halt   // loop forever
30: (Halt)
31:     0;JMP

from line 12 to 14,
@addr
A=M  // if M equals *A, How to understand the assignment between two different things?
M=D
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: How to interpreate M and A?

cadet1620
Administrator
moonwalk wrote
from line 12 to 14,
@addr
A=M  // if M equals *A, How to understand the assignment between two different things?
M=D
Thanks
ASM is untyped. Think of A as a C union.  It can be used as either an int or a *int.
int D;      // CPU registers
union {
    int num;
    int *ptr;
} A;

int addr;   // RAM variable

A.ptr = &addr;  // @addr
A.num = *A.ptr; // A=M
*A.ptr = D      // M=D

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

Re: How to interpreate M and A?

moonwalk
Thanks so much for your patient help! i got it!