Jumps and labels when you wont be able to know PC line number?

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

Jumps and labels when you wont be able to know PC line number?

moobles
I'm wondering about getting jumps to go to right place in situations where your code wont know what line number it is on.  (I know that sounds vague...)  I'm writing the code for chap 7.  Since I'm calling functions to write assembly I wont know what line number to tell the jump to go to because I dont know how much assembly I will have already done.  And I cant use specific labels {eg @GOHERE  with (GOHERE)} because then I will wind up with multiple occurrences of places for jumps to go...

So I was wondering. Can I access PC (program counter) or something to get the line number?  
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Jumps and labels when you wont be able to know PC line number?

cadet1620
Administrator
moobles wrote
I'm wondering about getting jumps to go to right place in situations where your code wont know what line number it is on.  (I know that sounds vague...)  I'm writing the code for chap 7.  Since I'm calling functions to write assembly I wont know what line number to tell the jump to go to because I dont know how much assembly I will have already done.  And I cant use specific labels {eg @GOHERE  with (GOHERE)} because then I will wind up with multiple occurrences of places for jumps to go...

So I was wondering. Can I access PC (program counter) or something to get the line number?
There is no way to get to the assembly details like the current output ROM address. You must use symbolic labels.

The trick to avoiding duplicate label names is to "decorate" them with a combination of the current file name, function name, or unique number. Note that VM file names will be the name of a Jack class, so they won't have weird characters in them.

If you look at the allowed characters in ASM symbol names you will find some that are not allowed in VM and Jack symbol names. I like to use '$'.

So for implementing "if-goto target", you can make symbol names that won't conflict by using file.function$target. This is similar to the way the "static" segment is handled (see 7.3.1).

For labels that your VM needs for it's own use, for instance in implementing the compare commands, you can use something like $eqNNNN where NNNN is a unique number. My code writer has a makeUniqueLabel(name) function that does this for me; the NNN value gets incremented every time it's called.

There's more info about this in chapter 8.

--Mark
Loading...