10.3. Segments

In Kick Assembler, a segment is a list of memory blocks, so let's look at these first.

A memory block is generated each time you use the *= directive. It has a start, an optional name and might be marked as virtual. If you add code without defining a memory block first, a default block is created for you. Here are examples of memory blocks.

        inc $d020         // This create a default memory block 
        jmp *-3           

        *=$1000           // Start of memoryblock 2 (unnamed)
        lda #1
        sta $d020
        rts
     
        *=$4000 "block3"  // Start of memoryblock 3     
        lda #2
        sta $d021
        rts
     

A segment is a list of memory blocks. Since you haven't selected any segment in the above code, they are all placed on the 'Default' segment.

A segment is defined by the .segmentdef directive and you use the .segment directive to decide which segment to add code to:

        // Define two segments
        .segmentdef MySegment1
        .segmentdef MySegment2 [start=$1000]

        // Add code to segment1 
        .segment MySegment1
        *=$4000
        ldx #30
 l1:    inc $d021 
        dex
        bne l1

        // Add code to segment2 (Using default block starting in $1000)
        .segment MySegment2
        inc $d021
        jmp *-3

        // Switch back to segment1 and add more code.
        .segment MySegment1
        inc $d020
        jmp *-3

In the above code MySegment1 is defined used the default parameters for a segment. While MySegment2 is defined setting the start address for the default memory block to $1000. A complete list of parameters is given in the end of this chapter.

Notice that you can switch back to a segment at any time and continue adding code to its current memory block.

Sometimes, it's convenient to define a memory block and switch to it with the same command. This is done by adding a parameters block ([...]) to the segment directive.

        // This:
        .segment MySegment [start=$1000]        

        // Is a shorthand notations for this:
        .segmentdef MySegment [start=$1000]
        .segment MySegment

A segment can only by be defined once so the above will give produce an error saying that 'MySegment' is double defined.