ca65-5.html

来自「cc65 的编译器文档」· HTML 代码 · 共 202 行

HTML
202
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.20"> <TITLE>ca65 Users Guide: Symbols and labels</TITLE> <LINK HREF="ca65-6.html" REL=next> <LINK HREF="ca65-4.html" REL=previous> <LINK HREF="ca65.html#toc5" REL=contents></HEAD><BODY><A HREF="ca65-6.html">Next</A><A HREF="ca65-4.html">Previous</A><A HREF="ca65.html#toc5">Contents</A><HR><H2><A NAME="s5">5.</A> <A HREF="ca65.html#toc5">Symbols and labels</A></H2><P>The assembler allows you to use symbols instead of naked values to makethe source more readable. There are a lot of different ways to define anduse symbols and labels, giving a lot of flexibility.</P><H2><A NAME="ss5.1">5.1</A> <A HREF="ca65.html#toc5.1">Numeric constants</A></H2><P>Numeric constants are defined using the equal sign or the label assignmentoperator. After doing</P><P><BLOCKQUOTE><CODE><PRE>      two = 2</PRE></CODE></BLOCKQUOTE></P><P>may use the symbol "two" in every place where a number is expected, and it isevaluated to the value 2 in this context. The label assignment operator causesthe same, but causes the symbol to be marked as a label, which may cause adifferent handling in the debugger:</P><P><BLOCKQUOTE><CODE><PRE>      io := $d000</PRE></CODE></BLOCKQUOTE></P><P>The right side can of course be an expression:</P><P><BLOCKQUOTE><CODE><PRE>      four = two * two</PRE></CODE></BLOCKQUOTE></P><H2><A NAME="ss5.2">5.2</A> <A HREF="ca65.html#toc5.2">Standard labels</A></H2><P>A label is defined by writing the name of the label at the start of the line(before any instruction mnemonic, macro or pseudo directive), followed by acolon. This will declare a symbol with the given name and the value of thecurrent program counter.</P><H2><A NAME="ss5.3">5.3</A> <A HREF="ca65.html#toc5.3">Local labels and symbols</A></H2><P>Using the <CODE><A HREF="ca65-10.html#.PROC">.PROC</A></CODE> directive, it is possible tocreate regions of code where the names of labels and symbols are local to thisregion. They are not known outside of this region and cannot be accessed fromthere. Such regions may be nested like PROCEDUREs in Pascal.</P><P>See the description of the <CODE><A HREF="ca65-10.html#.PROC">.PROC</A></CODE>directive for more information.</P><H2><A NAME="ss5.4">5.4</A> <A HREF="ca65.html#toc5.4">Cheap local labels</A></H2><P>Cheap local labels are defined like standard labels, but the name of thelabel must begin with a special symbol (usually '@', but this can bechanged by the <CODE><A HREF="ca65-10.html#.LOCALCHAR">.LOCALCHAR</A></CODE>directive).</P><P>Cheap local labels are visible only between two non cheap labels. As soon as astandard symbol is encountered (this may also be a local symbol if inside aregion defined with the <CODE><A HREF="ca65-10.html#.PROC">.PROC</A></CODE> directive), thecheap local symbol goes out of scope.</P><P>You may use cheap local labels as an easy way to reuse common labelnames like "Loop". Here is an example:</P><P><BLOCKQUOTE><CODE><PRE>        Clear:  lda    #$00             ; Global label                ldy    #$20        @Loop:  sta    Mem,y            ; Local label                dey                bne    @Loop            ; Ok                rts        Sub:    ...                     ; New global label                bne    @Loop            ; ERROR: Unknown identifier!</PRE></CODE></BLOCKQUOTE></P><H2><A NAME="ss5.5">5.5</A> <A HREF="ca65.html#toc5.5">Unnamed labels</A></H2><P>If you really want to write messy code, there are also unnamedlabels. These labels do not have a name (you guessed that already,didn't you?). A colon is used to mark the absence of the name.</P><P>Unnamed labels may be accessed by using the colon plus several minusor plus characters as a label designator. Using the '-' characterswill create a back reference (use the n'th label backwards), using'+' will create a forward reference (use the n'th label in forwarddirection). An example will help to understand this:</P><P><BLOCKQUOTE><CODE><PRE>        :       lda     (ptr1),y        ; #1                cmp     (ptr2),y                bne     :+              ; -> #2                tax                beq     :+++            ; -> #4                iny                bne     :-              ; -> #1                inc     ptr1+1                inc     ptr2+1                bne     :-              ; -> #1        :       bcs     :+              ; #2 -> #3                ldx     #$FF                rts        :       ldx     #$01            ; #3        :       rts                     ; #4</PRE></CODE></BLOCKQUOTE></P><P>As you can see from the example, unnamed labels will make even shortsections of code hard to understand, because you have to count labelsto find branch targets (this is the reason why I for my part doprefer the "cheap" local labels). Nevertheless, unnamed labels areconvenient in some situations, so it's your decision.</P><H2><A NAME="ss5.6">5.6</A> <A HREF="ca65.html#toc5.6">Using macros to define labels and constants</A></H2><P>While there are drawbacks with this approach, it may be handy in somesituations. Using <CODE><A HREF="ca65-10.html#.DEFINE">.DEFINE</A></CODE>, it ispossible to define symbols or constants that may be used elsewhere. Sincethe macro facility works on a very low level, there is no scoping. On theother side, you may also define string constants this way (this is notpossible with the other symbol types).</P><P>Example:</P><P><BLOCKQUOTE><CODE><PRE>        .DEFINE two     2        .DEFINE version "SOS V2.3"        four = two * two        ; Ok        .byte   version         ; Ok        .PROC                   ; Start local scope        two = 3                 ; Will give "2 = 3" - invalid!        .ENDPROC</PRE></CODE></BLOCKQUOTE></P><H2><A NAME="ss5.7">5.7</A> <A HREF="ca65.html#toc5.7">Symbols and <CODE>.DEBUGINFO</CODE></A></H2><P>If <CODE><A HREF="ca65-10.html#.DEBUGINFO">.DEBUGINFO</A></CODE> is enabled (or <A HREF="ca65-2.html#option-g">-g</A> is given on the command line), global, local andcheap local labels are written to the object file and will be available in thesymbol file via the linker. Unnamed labels are not written to the object file,because they don't have a name which would allow to access them.</P><HR><A HREF="ca65-6.html">Next</A><A HREF="ca65-4.html">Previous</A><A HREF="ca65.html#toc5">Contents</A></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?