Search code examples
assemblyz80

Understanding Z80 dollar signs and labels


I was researching Brandon W's "fake" application for the ti-84 to see how it worked. (http://brandonw.net/calculators/fake/) While looking through the code I noticed many labels and jump instructions that I didn't understand. I would like to learn how he is using these dollar signs and jumps. My questions are asked in the following code snippet: (All code is from Brandon W's open source fake application!)

    resetKeys:
        xor a
        ld (de),a
$$:     ld a,(MenuCurrent) ;How is this label two dollar signs? What does this mean?
        cp 02h
        jr nz,$F           ;Are we jumping to the instruction at 0xF or to one of these dollar sign labels?
        ld hl,sFakeAppVar
        rst 20h
        B_CALL ChkFindSym
        jr c,$F            ;If we are jumping to 0xF, what are the dollar signs used for?
        ld a,b
        or a
        jr nz,$F
        inc de
        inc de
        ld a,(de)
        cp 25h
        jr z,ignoreAppsKeys
        cp 26h
        jr z,ignoreAppsKeys
        cp 27h
        jr z,ignoreAppsKeys
$$:     pop af              ;Here's another
        ld b,a
        ld a,(cxCurApp)
        cp 45h
        jr nz,$F
        ld a,b
        cp kCapS
        jr nz,$F

From what I have researched, the dollar sign is used when signifying hex or the current location counter. Please correct me if I'm wrong. Any help would be much appreciated!


Solution

  • The $ sign has several uses. Most Z80 assemblers use $ as prefix for hexadecimal numbers, like in this case:

    jr z, $2A
    

    These assemblers also use $ to represent the location were present line of code will be in the binary. This is useful to create constants to represent code locations, like in this case:

    value_to_jump = $+1
    jr $10
    

    In this case, 'value_to_jump' is a constant that will hold the address where $10 is stored (jr $10 is 2-bytes long, so $+1 points to the second of these bytes).

    Finally, some assemblers let you create anonymuos labels, like $$ or @@. This is the case you are seeing here. In this code:

    jr $f
    ....
    $$:
    ....
    jr $b
    

    Jump instructions mean "closest forward" or "closest backwards", referring to the anonymous labels.