10.6. Naming memory blocks while switching segment

One use of segments is to place code/data that is tied together but should be located different places in memory, close together in the source code. This leads to a coding style where you may want to name a new block of code every time you switch segment. You could do this by adding a memblock directive right after the segment directive. But as a convenient shorthand you can just place a text string after the segment switch:

        // This
        .segment Code "My Code"

        // Is the same as this
        .segment Code
        .memblock "My Code"

To demonstrate this style is here given a larger example. Some of the features are first covered later. :

.segmentdef Code [start=$0900]
.segmentdef Data [start=$8000]
.file [name="segments.prg", segments="Code,Data", modify="BasicUpstart", marg1=$0900]

// Main
        .segment Code "Main"
        jsr colorSetup
        jsr textSetup 

// Color
        .segment Code "Color Setup"
        lda colors
        sta $d020
        lda colors+1
        sta $d021
        .segment Data "Colors"
colors: .byte LIGHT_GRAY, DARK_GRAY

// Text
        .segment Code "Text Setup"
textSetup: {
        ldx #0
loop:   lda text,x
        cmp #$ff
        beq out
        sta $0400,x
        jmp loop
        .segment Data "Static Text"
text:   .text "hello world!"
        .byte $ff

You will now get a memory map like this, when you use the -showmem’ option:

  $0900-$0906 Main
  $0907-$0913 Color Setup
  $0914-$0924 Text Setup

  $8000-$8001 Colors
  $8002-$800e Static Text

The code and data are now separated in memory, but close together in the source code.

Note that scoping and segments don't affect each other so you can switch segments within a scope. In the above its used so the 'text' label is local. It can be seen from textSetup code but not from other routines. If you want to have a scroll text routine it could have its own 'text' label and they wouldn't collide.