-

Changes

Jump to: navigation, search

Z80 Assembler Reference Manual

240 bytes added, 20:00, 30 July 2017
no edit summary
The assembler incorporated in the HI-TECH C compiler
system is a full-featured relocating macro assembler accept-ing accepting Zilog mnemonics. These mnemonics and the syntax of the
Z80 assembly language are described in the "Z80 Assembly
Language Handbook" published by Zilog and are included at
the end of this manual as a reference. The assembler imple-ments implements certain extensions to the operands allowed, and cer-tain certain additional pseudo-ops, which are described here. Theassembler also accepts the additional opcodes for the Hita-chi Hitachi 64180 and Z180 processors.
== Usage ==
options are zero or more options from the following list:
;-N :Ignore arithmetic overflow in expressions. The -Noption suppresses the normal check for arithmetic over-flowoverflow. The assembler follows the "Z80 Assembly LanguageHandbook" in its treatment of overflow, and in certaininstances this can lead to an error where in fact theexpression does evaluate to what the user intended.This option may be used to override the overflow check-ingchecking.
;-J :Attempt to optimize jumps to branches. The -J optionwill request the assembler to attempt to assemble jumpsand conditional jumps as relative branches where possi-blepossible. Only those conditional jumps with branchequivalents will be optimized, and jumps will only beoptimized to branches where the target is in branchrange. Note that the use of this option slows theassembly down, due to the necessity for the assemblerto make an additional pass over the input code.
;-U :Treat undefined symbols as external. The -U optionwill suppress error messages relating to undefined sym-bolssymbols. Such symbols are treated as externals in anycase. The use of this option will not alter the objectcode generated, but merely serves to suppress the errormessages.
;-O_�f_�i_�l_�eO<strong>file</strong>:Place the object code in _�f_�i_�l_�e<strong>file</strong>. The default object filename is constructed from the name of the first sourcefile. Any suffix or file type (i.e. anything followingthe rightmost dot ('.') in the name is stripped, andthe suffix .obj appended. Thus the command
ZAS file1.as file2.z80
:will produce an object file called file1.obj. The useof the -O option will override this default convention,allowing the object file to be arbitrarily named. Forexample:
ZAS -ox.obj file1.obj
:will place the object code in x.obj.
;-L_�l_�i_�s_�tL<strong>list</strong>:Place an assembly listing in the file _�l_�i_�s_�t<strong>list</strong>, or on stan-dard standard output if _�l_�i_�s_�t <strong>list</strong> is null A listfile may be producedwith the -L option. If a file name is supplied to theoption, the list file will be created with that name,otherwise the listing will be written to standard out-put output (i.e. the console). List file names such as CON:and LST: are acceptable.
;-W_�w_�i_�d_�t_�hW<strong>width</strong>:The listing is to be formatted for a printer of given_�w_�i_�d_�t_�h <strong>width</strong> The -W option specifies the width to which thelisting is to be formatted. E.g.
ZAS -Llst: -W80 x.as
:will output a listing formatted for an 80 columnprinter to the list device.
;-C :This options requests ZAS to produce cross referenceinformation in a file. The file will be called _�x_�x_�x<strong>xxx</strong>.crfwhere _�x_�x_�x <strong>xxx</strong> is the base part of the first source filename. It will then be necessary to run the CREF utilityto turn this information into a formatted listing.
== The Assembly Language==
As mentioned above, the assembly language accepted byzas is based on the Zilog mnemonics. You should have somereference book such as the "Z80 Assembly Language Handbook".Described below are those areas in which zas differs, or hasextensions, compared to the standard Zilog assemblylanguage.
=== Symbols ===
The symbols (labels) accepted by the assembler may be
of any length, and all characters are significant. The char-acters characters used to form a symbol may be chosen from the upper
and lower case alphabetics, the digits 0-9, and the special
distinct symbols.
An_identifier an_identifier an_identifier1 $$$ ?$_123455 
Note that the symbol $ is special (representing the
such labels.
entry_point: ;This is referenced from far away ld b,10 1: dec c jr nz,2f ;if zero, branch forward to 2: ld c,8 djnz 1b ;decrement and branch back to 1: jr 1f ;this does not branch to the ;same label as the djnz 2: call fred ;get here from the jr nz,2f 1: ret ;get here from the jr 1f
set:
{||+!Character || Radix || Name|-!B || 2 || binary|-!O || 8 || octal|-!Q || 8 || octal|-!o || 8 || octal|-!q || 8 || octal|-!H || 16 || hexadecimal|-!h || 16 || hexadecimal|}
Hexadecimal constants may also be specified in C style,
sion. The value of the opcode in this context will be the
byte that the opcode would have assembled to if used in the
normal way. If the opcode is a 2-byte opcode (CB or ED pre-fix prefix byte) only the second byte of the opcode will be used.
This is particularly useful when setting up jump vectors.
For example:
ld a,jp ;a jump instruction ld (0),a ;0 is jump to warm boot ld hl,boot ;done here ld (1),hl
The following operators may be used in expressions:
{||+!Operator || Meaning|-!& || Bitwise AND|-!* || Multiplication|-!+ || Addition|-!- || Subtraction      HI-TECH C USER'S MANUAL Page 39 .and. || Bitwise AND|-!.eq. || Equality test|-!.gt. || Signed greater than|-!.high. || Hi byte of operand|-!.low. || Low byte of operand|-!.lt. || Signed less than|-!.mod. || Modulus|-!.not. || Bitwise complement|-!.or. || Bitwise or|-!.shl. || Shift left|-!.shr. || Shift right|-!.ult. || Unsigned less than|-!.ugt. || Unsigned greater than|-!.xor. || Exclusive or|-!/ || Divison|-!< || Signed less than|-!= || Equality|-!> || Signed greater than|-!^ || Bitwise or|}
psect text, global alabel: ld hl,astring call putit ld hl,anotherstring psect data, global astring: defm 'A string of chars' defb 0 anotherstring: defm 'Another string' defb 0 psect text putit: ld a,(hl) or a ret z call outchar inc hl jr putit
DEFB. Example:
DEFB 10, 20, 'a', 0FFH DB 'hello world',13,10,0
ple:
pi: DEFF 3.14159
This operates in a similar fashion to DEFB, except that
it assembles expressions into words, without the value res-trictionrestriction. Example:
DEFW -1, 3664H, 'A', 3777Q
program. Example:
DEFS 20h ;reserve 32 bytes of memory
of a symbol which is already defined. Example:
SIZE equ 46
SIZE defl 48
Example:
DEFM 'A string of funny *@$ characters'
ple:
END somelabel
a COND/ENDC block, for example:
IF CPM call 5 ELSE call os_func ENDC
pseudo-op. The psect flags are as follows:
;ABS :Psect is absolute
;GLOBAL :Psect is global
;LOCAL :Psect is not global
;OVRLD :Psect is to be overlapped by linker
;PURE :Psect is to be read-only
The PURE flag instructs the linker that the psect is to
be made read-only at run time. The usefulness of this flag
depends on the ability of the linker to enforce the require-mentrequirement. CP/M fails miserably in this regard.
The ABS flag makes a psect absolute. The psect will be
PSECT text, global, pure PSECT data, global PSECT vectors, ovrld
GLOBAL label1, putchar, _printf
which must be an absolute expression. Example:
ORG 100H
corresponding actual parameter. For example:
print MACRO string psect data 999: db string,'$' psect text ld de,999b ld c,9 call 5 ENDM
When used, this macro will expand to the 3 instructions
in the body of the macro, with the actual parameters substi-tuted substituted for func and arg. Thus
print 'hello world'
expands to
psect data 999: db 'hello world','$' psect text ld de,999b ld c,9 call 5
Macro arguments can be enclosed in angle brackets ('<'
and '>') to pass arbitrary text including delimiter charac-ters characters like commas as a single argument. For example, suppose
you wanted to use the print macro defined above to print a
string which includes the carriage return and linefeed char-acterscharacters. The macro invocation:
print 'hello world',13,10
as a single argument, you could write:
print <'hello world',13,10>
which would cause the text 'hello world',13,10 to be passed
through as a single argument. This would expand to the fol-lowing following code:
psect data 999: db 'hello world',13,10,'$' psect text ld de,999b ld c,9 call 5
ZAS supports two forms of macro declaration for compa-tibility compatibility with older versions of ZAS and other Z80 assem-blersassemblers. The macro name may be declared either in the label
field before the MACRO pseudo-op, or in the operand field
after the MACRO pseudo-op. Thus these two MACRO declara-tions declarations are equivalent:
bdos MACRO func,arg ld de,arg ld c,func call 5 ENDM
and
MACRO bdos,func,arg ld de,arg ld c,func call 5 ENDM
The LOCAL pseudo-op allows unique labels to be defined
for each expansion of a macro. Any symbols listed after the
LOCAL directive will have a unique assembler-generated sym-bol symbol substituted for them when the macro is expanded. For
example:
copy MACRO source,dest,count LOCAL nocopy push af push bc ld bc,source ld a,b or c jr z,nocopy push de push hl ld de,dest ld hl,source ldir pop hl pop de nocopy: pop bc pop af ENDM
(recptr),buf,(recsize) will expand to:
push af push bc ld bc,(recsize) ld a,b or c jr z,??0001 push de push hl ld de,buf ld hl,(recptr) ldir pop hl pop de ??0001: pop bc pop af
if invoked a second time, the label nocopy would expand to
ment. For example:
REPT 3 ld (hl),0 inc hl ENDM
ld (hl),0 inc hl ld (hl),0 inc hl ld (hl),0 inc hl
=== IRP and IRPC ===
The IRP and IRPC directives are similar to REPT, how-ever however instead of repeating the block a fixed number of times
it is repeated once for each member of an argument list. In
the case of IRP the list is a conventional macro argument
string. For example:
IRP string,<'hello world',13,10>,'arg2' LOCAL str psect data str: db string,'$' psect text ld c,9 ld de,str call 5 ENDM
would expand to
psect data ??0001: db 'hello world',13,10,'$' psect text ld c,9 ld de,??0001 call 5 psect data ??0002: db 'arg2','$' psect text ld c,9 ld de,??0002 call 5
IRPC is best demonstrated using the following example:
IRPC char,ABC ld c,2 ld e,'char' call 5 ENDM
will expand to:
ld c,2 ld e,'A' call 5 ld c,2 ld e,'B' call 5 ld c,2 ld e,'C' call 5
codes. These are:
_________________________________________________{|| +Code|| Equivalent|| Meaning || -alt || m || Arithmetic less than || -llt || c || Logical less than || -age || p || Arithmetic greater or equal|| -lge || nc || Logical greater or equal || -di || || Use after ld a,i for test-|| -ei || || ing state of interrupt|| - || || enable flag - enabled or||- |______�| |_____________�|�|__d�_i�_s�_a�_b�_l�_e�_d�__r�_e�_s�_p�_e�_c�_t�_i�_v�_e�_l�_y�_ disabled respectively.�_______�|�|}
12.4. == Assembler Directives==
An assembler directive is a line in the source filewhich produces no code, but rather which modifies thebehaviour of the assembler. Each directive is recognized bythe presence of an asterisk in the first column of the line,followed immediately by a word, only the first character ofwhich is looked at. the line containing the directive itselfis never listed. The directives are:
;<nowiki>*Title</nowiki>:Use the text following the directive as a title for thelisting.
;<nowiki>*Heading</nowiki>:Use the text following the directive as a subtitle forthe listing; also causes an *Eject.
;<nowiki>*List</nowiki>:May be followed by ON or OFF to turn listing on or offrespectively. Note that this directive may be usedinside a macro or include file to control listing ofthat macro or include file. The previous listing statewill be restored on exit from the macro or includefile.
;<nowiki>*Include</nowiki>:The file named following the directive will be includedin the assembly at that point.
;<nowiki>*Eject</nowiki>:A new page will be started in the listing at thatpoint. A form feed character in the source will havethe same effect.
Some examples of the use of these directives:
*Title Widget Control Program *Heading Initialization Phase *Include widget.i
An error message will be written on the standard error
stream for each error encountered in the assembly. This mes-sage message identifies the file name and line number and describes
the error. In addition the line in the listing where the
error occurred will be flagged with a single character to
;A: :Absolute expression required
;B: :Bad arg to *L:Bad arg to IM:Bad bit number:Bad character constant:Bad jump condition
;D: :Directive not recognized:Digit out of range
;E: :EOF inside conditional:Expression error
;G: :Garbage after operands:Garbage on end of line
;I: :Index offset too large
;J: :Jump target out of range
;L: :Lexical error
;M: :Multiply defined symbol
;O: :Operand error
;P: :Phase error:Psect may not be local and global
;R: :Relocation error
;S: :Size error:Syntax error
;U: :Undefined symbol:Undefined temporary label:Unterminated string
12.6. == Z80/Z180/64180 Instruction Set==
The remainder of this chapter is devoted to a complete

Navigation menu