Sections

The simplest programs will consist of two sections. The data section where data is stored and the code section where the executable program is stored. The programmer is not limitied to just two sections. He is free to create and use as many data and code sections as he wants. An example of creating a simple program that uses just two sections would be:
        .sect   data_section
        .data
count   .db     0

        .sect   code_section
        .code
        LD      A,(count)
a slightly more complex example involves interleaving sections
        .sect   data_section    ;; lexically grouped sequence of
        .data                   ;; statments
count   .db     0               ;;
                                ;; GROUP A
        .sect   code_section    ;;
        .code                   ;;
        LD      A,count         ;;


        .sect   data_section    ;; lexically grouped sequence of
delay   .db     0               ;; statments
                                ;;
        .sect   code_section    ;; GROUP B
        LD      A,delay         ;;
Note the absence of the ".data" section type statement after the second use of ".sect data_section" and the absence of the ".code" statement after the second use of ".sect code_section". This is because the first use of these section type statements causes the associated sections in which they occure to be tagged with the specified type.

Interleaving the data_section and code_section allows the programmer the freedom to lexically group parts of a program even though those parts will reside in distant parts of a system. Although the above example consists of two groups of statements the assembler will collect all the parts bellonging to each section and emit code as if they had in fact been sorted by the programmer.

The above example is equivalent to

        .sect   data_section
        .data
count   .db     0
delay   .db     0

        .sect   code_section
        .code
        LD      A,count
        LD      A,delay
The ability to use sections to lexically group lines of source together is very usefull when you need to keep local data definitions near to the point of use (e.g. local variables used by a subroutine). However the ability to use sections is vital when using macros to generate self contained sequences of statements that use dedicated locals.
e.g.

foo     .macro

        .sect   data_section
var     .set    LABEL("local_"+MACINV)
var     .db     0

        .sect   code_section
        LD      (var),A
var2    .set    LABEL("delay_"+MACINV)
var2    DEC     var
        BNZ     var2

        .endm
This example reserves a byte of storage from the data section, then generates code that sets it to the value in the accumulator, then decrements the local repeatedly until it reaches zero.

Note the use of the assembler variables to generate the local labels used during macro expansion

A macro could be used to split up a record into several arrays

e.g.
foo     .macro  arg1, arg2, arg3

        .sect   tbl1_data
        .db     arg1

        .sect   tbl2_data
        .db     arg2

        .sect   tbl3_data
        .db     arg3

        .endm
useing the above macro so:
        .sect   tbl1_data
        .data
tbl1

        .sect   tbl2_data
        .data
tbl2

        .sect   tbl3_data
        .data
tbl3

        foo     11,12,13
        foo     21,22,23
        foo     31,32,33
        foo     41,42,43
would generate the equivalent of
tbl1    .db     11
        .db     21
        .db     31
        .db     41

tbl2    .db     12
        .db     22
        .db     32
        .db     42

tbl3    .db     13
        .db     23
        .db     33
        .db     43
Section Order (grouping)
The order in which sections are arranged in memory is defined by the group to which it is assigned and the order in which it apears in that group.
Section Limits
Sections also have the property of limits. Limits can be set by the programmer for a particualr section so that the programmer is warned in the event that more than a pre-determined amount of space is used in that section. Limits can also be used to ensure that an address for a given section is not outside a given range. This is usefull if the section is actually mapped onto some phisical region of the processors memory space such as dedicated memory mapped hardware or a register file.