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.