gnupic: Documentation for $ shortcut for line number
Subject:
Re: Documentation for $ shortcut for line number
From:
"Eric Smith" ####@####.####
Date:
15 Jan 2003 02:58:28 -0000
Message-Id: <4372.4.20.168.156.1042598991.squirrel@ruckus.brouhaha.com>
Colin Kuskie wrote:
> I've borrowed a few books in order to try to learn PIC programming, and
> in one of them I noticed this construct:
>
> GOTO $ - 1
>
> where it explains that $ is expanded to the current line number. I
> tried this in gpasm and was pleased to see that it's supported, but
> appears to be undocumented in the manual.
$ does not expand to a "line number", which would be all but worthless.
It expands to the address of the instruction currently being assembled.
If it's used in a context other than an instruction, such as a
conditional, it expands to the address the next instruction would occupy,
since the assembler's idea of current address is incremented after an
instruction is assembled.
So a two-cycle NOP can be written as:
GOTO $+1
If you want to force alignment to a 256-word page, you coud write:
ORG ($+0xff)&0xff00
Or if you wanted to make sure that a lookup table fit entirely on a
page:
TABLE: ADDWF PCL
RETLW 37
RETLW 62
[...]
RETLW 196
IF ($ & 0xff00) != ((TABLE + 1) & 0xff00)
ERROR "Table crossed page boundary"
ENDIF
[Note that these examples are based on techniques I commonly use, but
the specific code snippets were hastily written for this email and may
contain errors.]
For this latter purpose, I normally use macros I call TableStart and
TableEnd to encapsulate the whole mess:
TableStart MACRO
TableBaseAddr SET $
ENDM
TableEnd MACRO
IF ($>>8) != (TableBaseAddr>>8)
ERROR "Table crosses a 256-byte boundary"
ENDIF
ENDM
MPASM has the annoying property that an error in a macro expansion is
reported at the line number within the macro definition, thus if the
error occurs you have to look at the listing file to find the actual
location of the offending table. Sigh.
Eric