Tatara

Chapter 11
Data

A program needs data as well as instructions: messages to print, tables of numbers, addresses. This chapter describes the directives that put data into a program: DB for bytes and strings, DW for 16-bit words, and DC for strings that mark their own end. DS, which reserves space without filling it, is in chapter 10.

11.1 DB: bytes

DB takes a list of expressions separated by commas, and stores each one as a byte:

table:          db      1,2,3           ; 01 02 03 
                db      'A'+1           ; 42 
                db      -1              ; FF
 

Each expression can be anything chapter 8 describes. As with instructions (chapter 9), TATARA stores the low byte of the value and does not check that it fits: db 300 stores 2Ch.

One byte cannot hold an address that TANREN still has to finish, so the value must be absolute. A label or an external name in DB stops TATARA with the relocation error:

DBREL.AS(3): ERROR: relocation error - a segment-relative value is not allowed here.
 

11.2 Strings

A string in DB is text between quotation marks, and is stored as one byte for each character. Strings and numbers can be mixed in one list:

msg:            db      'Hello',13,10,'$'
 

These are the bytes 48 65 6C 6C 6F 0D 0A 24: the five letters, then 13 and 10, which move to the start of the next line, then the $ that the MSX-DOS2 function _STROUT stops at (chapter 4).

A string can be enclosed in apostrophes or in quotation marks. Inside it, the enclosing mark is written twice to stand for itself, or the other mark can be used instead. These two lines store the same four bytes:

                db      "It's" 
                db      'It''s'
 

An empty string, ”, stores nothing.

Note.  A string of one or two characters looks the same as a character constant (chapter 8), and TATARA tells them apart as M80 does, by what follows the closing quotation mark. If a comma or the end of the line follows, the item is a string; otherwise it is a character constant in an expression. db ’AB’ is therefore two bytes, 41 42, but db ’AB’ and 0ffh is one: the value 4142h with its high byte removed, 42h.

11.3 DW: words

DW stores each expression as a word: two bytes, low byte first, which is the order in which the Z80 stores every 16-bit value (chapter 2). dw 1234h is 34 12.

Two bytes can hold an address that TANREN finishes, so a label, an expression built on one, or an external name is allowed here (section 8.6). This makes DW the way to build a table of addresses, such as a list of routines to jump to:

jumps:          dw      start, again, finish
 

A string of one or two characters in DW is a character constant: dw ’AB’ is 42 41, the value 4142h stored low byte first. A longer string stops TATARA with bad DB, DW or DC operand.

11.4 DC: strings that mark their own end

DC takes one string, and stores it like DB except that bit 7 of the last character is set. dc ’Stop’ is 53 74 6F F0: the last byte is the p, 70h, plus 80h.

The characters of ordinary text all have bit 7 clear, so a program can recognise the last character of such a string by that bit, and the string needs no terminator such as $. This routine prints a string made with DC, whose address is in HL. It uses the MSX-DOS2 function _CONOUT, which prints the character in E, through the system macro of MSXDOS.INC (chapter 14):

print:          ld      a,(hl)          ; the next character 
                inc     hl 
                push    af              ; keep it, and its bit 7 
                and     7fh             ; the character without bit 7 
                ld      e,a 
                push    hl              ; MSX-DOS2 may change HL 
                system  _CONOUT 
                pop     hl 
                pop     af 
                bit     7,a             ; was it the last one? 
                jr      z,print         ; no: go round again 
                ret
 

If AND, BIT and the stack are new to you, the book you learn Z80 programming from explains them; what matters here is that the loop ends by itself, on the byte whose bit 7 is set.

DC must be given exactly one string, and not an empty one, because an empty string has no last character to mark. dc ” and dc 5 both stop TATARA with bad DB, DW or DC operand.

11.5 The other names

Some directives have a second name, which TATARA accepts so that sources written for other assemblers can be assembled unchanged. Table 11.1 lists them.

Other name Same as
DEFB, DEFM DB
DEFW DW
DEFS DS
Table 11.1: Other names of the data directives.

11.6 Messages

Table 11.2 lists the messages about data, each with a line that causes it.

Line

Message

db 1„2

bad DB, DW or DC operand.

dw ’ABC’

bad DB, DW or DC operand.

dc ”, dc 5

bad DB, DW or DC operand.

db label, db far (external)

relocation error - a segment-relative value is not allowed here.

Table 11.2: Messages about data.