Next Previous Contents

6. シンボルとラベル(Symbols and labels)

シンボル・ラベルは、文字列の最初の識別及び、文字列と数字で追従されます。
Depending on some features enabled (see at_in_identifiers, dollar_in_identifiers and leading_dot_in_identifiers) other characters may be present.
Use of identifiers consisting of a single character will not work in all cases, because some of these identifiers are reserved keywords (for example "A" is not a valid identifier for a label,because it is the keyword for the accumulator).

The assembler allows you to use symbols instead of naked values to make the source more readable.
There are a lot of different ways to define and use symbols and labels, giving a lot of flexibility.

6.1 定数(Numeric constants)

定数は、"="サインか、割り当て演算子を使う事で定義されます。

      two = 2

上述の定義後は、数値が予期されるあらゆる場所でシンボル"two"を使う事ができ、 そのコンテクストの中でそれは値2として評価されます。
ラベル割り当て演算子は同じ事をもたらすが、シンボルはデバッガーで異なる取り扱いを生じさせるラベルとして作られる:

      io := $d000

The right side can of course be an expression:

      four = two * two

6.2 標準ラベル(Standard labels)

行の先頭(命令ニモニック、マクロ、もしくは疑似ディレクティブの前)に ラベルの名前を記述することでラベルの定義となり、コロン":"を後ろに追加する。
これは、現在のプログラムカウンターの値と与えられた名前でシンボルを定義します。

6.3 局所的なラベルとシンボル(Local labels and symbols)

.PROCディレクティブの使用は、 ラベルとシンボルの名前がこの領域内で局所的となるコードを生成する事を可能とする。
そのため、この領域外からはアクセスできないラベル、シンボルとなる。
このような境界は、PascalのPROCEDUREのような重ね合わせを可能にする。

詳細は、.PROCディレクティブを参照。

6.4 簡易局所ラベル(Cheap local labels)

簡易局所ラベルは標準ラベルのように定義する。
ただし、ラベル名は特別なシンボルで始まらなければならない (通常'@'だが、.LOCALCHARディレクティブにより変更することができる)。

簡易局所ラベルは、2つの簡易局所ラベルでないラベルの間でのみ有効となります。
標準ラベルが現れると直ぐに、 (this may also be a local symbol if inside a region defined with the .PROC directive), 簡易局所ラベルはスコープから外れます。

"Loop"の様な共通的なラベル名を再利用するための簡単な方法として、簡易局所ラベルを使う事ができます。
例:

        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!

6.5 Unnamed labels

このコードの書き方は、バグの原因になって好きじゃないから翻訳しない。

If you really want to write messy code, there are also unnamed labels. 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.

Unnamed labels may be accessed by using the colon plus several minus or plus characters as a label designator. Using the '-' characters will create a back reference (use the n'th label backwards), using '+' will create a forward reference (use the n'th label in forward direction). An example will help to understand this:

        :       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

As you can see from the example, unnamed labels will make even short sections of code hard to understand, because you have to count labels to find branch targets (this is the reason why I for my part do prefer the "cheap" local labels). Nevertheless, unnamed labels are convenient in some situations, so it's your decision.

6.6 Using macros to define labels and constants

While there are drawbacks with this approach, it may be handy in some situations. Using .DEFINE, it is possible to define symbols or constants that may be used elsewhere. Since the macro facility works on a very low level, there is no scoping. On the other side, you may also define string constants this way (this is not possible with the other symbol types).

Example:

        .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

6.7 Symbols and .DEBUGINFO

If .DEBUGINFO is enabled (or -g is given on the command line), global, local and cheap local labels are written to the object file and will be available in the symbol 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.


Next Previous Contents