dealing with (i)

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

dealing with (i)

sn1012
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: dealing with (i)

ivant
sn1012 wrote
I've managed to write an assembly code that works for most of my memory segments but the issue is @i and @addr.  They get mapped to r16 and r17 but to my understanding, this is wrong since those registers are reserved for static variables.
The assembler doesn't "know" anything about static variables. It translates assembly language programs to machine language. The HACK assembler has this convention, that unknown labels will be treated as variables and will be assigned to addresses from 16 onward. That's why i and addr are mapped to mem[16] and mem[17].

Then there is the VM translator, which generates HACK assembly from a bunch of .vm files. One of its features is handling class-level or static fields. It does that by using the free variable mechanism of the HACK assembler. That is, it just uses the ClassName.fieldName in the assembly code and lets the HACK assembly handle this.

A different compiler may use the same mechanism differently. Or it may just skip it outright and just handle the address assignments by itself.
Reply | Threaded
Open this post in threaded view
|

Re: dealing with (i)

sn1012
So is my Hack assembley code correct for push LCL (i )function ? I wasn't talking about my assembler ,edited my post to make it more clear now.
Reply | Threaded
Open this post in threaded view
|

Re: dealing with (i)

ivant
I'm not entirely sure what you're trying to achieve as a whole but I can see some of the problems. Cadet1620 wrote an excellent Introduction to Hack Assembly Language. Read it and try again with a simpler task. For example, the code fragment
@i
D=M
@LCL
D=D+M
seems to try to put in the register D the address of the ith local variable, but it's not doing that correctly. Try to fix it on your own.

(Hint: do you know the value of i at compile time?)