Confused On Assembly Language

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Confused On Assembly Language

elauzel
I have made it thus far without having a strong grasp of how the assembly language works. This is now a big problem, and I can't get clarification from the book, either due to my own lack of understanding, or the regrettably fact that I can't ask it questions. I'd like to walk through what I *do* understand using arbitrary code (so it won't need to be removed,) and pose questions about what I don't grasp. I'm looking for feedback and help from all of you.

@SP       // @0, M = RAM[0] = 256
D = M      // D = RAM[0] = 256
A = D - 1   // A = 256 - 1 = 255
AM = M - 1  // would this be A = RAM[0] = RAM[0] - 1, or A = RAM[255] = RAM[255] - 1 ?

I read that M refers to the word held in the address of the A register, which is set by '@', but is it also set by an equation such as A = D - 1 ? I am often confused at the changing role of M, it seems to refer to something different depending on which side of the equation it falls on. Can anyone help me out with a good guide for this, as well as A? I didn't find the book explicit enough, or organized for my learning style.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Confused On Assembly Language

ivant
Think of each step instruction independently and see what it changes:
elauzel wrote
@SP       // @0, M = RAM[0] = 256
This stores 0 in register A. Nothing more, nothing less.

elauzel wrote
D = M      // D = RAM[0] = 256
Correct, D is assigned the value in RAM[0]. Assuming that RAM[0] contains 256, D becomes 256.

elauzel wrote
A = D - 1   // A = 256 - 1 = 255
Correct again.

elauzel wrote
AM = M - 1  // would this be A = RAM[0] = RAM[0] - 1, or A = RAM[255] = RAM[255] - 1 ?
This is the harder one. Let's first find out the M - 1 part. M here means take the value of RAM[A], that is RAM[255]. Let's say its value is x. M - 1 is evaluated to x-1. Think of it as if this is stored in some temporary register TMP.

Next it is the assignment. And it's ambiguous. It could mean

RAM[A] = x-1
A = x-1

or it could mean

A = x-1
RAM[x-1] = x-1

The second interpretation isn't very meaningful, as it would mean that AM=x will always make M[x] become x. This isn't very helpful, so it's better to implement the first interpretation.

You can test this, by entering your program in a asm file (but remove the spaces: it's D=M, not D = M, etc.). Then load it in the CPU emulator, store some meaningful values in RAM and step over it. I chose to store 20 in RAM[0] and 13 in RAM[19]. This way I can see the relevant RAM will be visible without scrolling.

The meaning of "M" doesn't change (or changes just a little). It always denotes RAM[A]. The only difference is, that M in the DEST part of the instruction means storing the COMP value in that RAM register, while M in the COMP part means fetching the value from this RAM register.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Confused On Assembly Language

cadet1620
Administrator
In reply to this post by elauzel
It looks like you have made a good start at learning the assembly language. I'm writing an intro to programming that might help clarify things.
    Introduction to Hack Assembly Language

To understand AM=M-1, think about how synchronous hardware works. The A-register and RAM change simultaneously when the clock ticks. The source and destination M both use the pre-tick A-register value as the RAM address.

Taking Ivan's TMP variable idea a bit further:
    tempA = A
    temp = RAM[A] - 1
    <clock tick>
    A = temp
    RAM[tempA] = temp

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Confused On Assembly Language

elauzel
I think I get it now, but I want to be sure. I have some code that is relevant to the chapter for this project. Specifically, pushing a constant to the stack. Who can I send this to, since we're not supposed to post such code directly here?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Confused On Assembly Language

cadet1620
Administrator
Feel free to email me your code.  I'll be happy to look at it for you and offer guidance.

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Confused On Assembly Language

rgravina
In reply to this post by cadet1620
Thanks Mark for the great intro to Hack Assembly Language page! It was very helpful in helping me understand the assembly language better.

I found that even after completing the assembler (chapter 6) I didn't really understand how to use the instructions to achieve something practical, like adding two values or incrementing a value in RAM. I suppose that's not too surprising, as up until this chapter we haven't done anything with the instructions :)
Loading...