-

Changes

Jump to: navigation, search

Z80 Assembler Reference Manual

631 bytes added, 10:32, 31 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 ==
The assembler is named zas, and is invoked as follows:
ZAS options files ...
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
 
symbols underscore ('_'), dollar ('$') and question mark
('?'). The first character may not be numeric. Upper and
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|-|<nowiki>+ </nowiki>|| Addition|-|<nowiki>- </nowiki>|| 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, DB ====
:This pseudo-op should be followed by a comma-separatedlist of expressions, which will be assembled into sequentialbyte locations. Each expression must have a value between -128 and 255 inclusive. DB can be used as a synonym for DEFB. Example:
 byte locations. Each expression must have a value between-128 and 255 inclusive. DB can be used as a synonym forDEFB. Example: DEFB 10, 20, 'a', 0FFH DB 'hello world',13,10,0
==== DEFF ====
:This pseudo-op assembles floating point constants into32 bit HI-TECH C format floating point constants. For exam-pleexample:
pi: DEFF 3.14159
==== DEFW ====
:This operates in a similar fashion to DEFB, except thatit assembles expressions into words, without the value res-trictionrestriction. Example:
DEFW -1, 3664H, 'A', 3777Q
==== DEFS ====
:Defs reserves memory locations without initializingthem. Its operand is an absolute expression, representingthe number of bytes to be reserved. This expression isadded to the current location counter. Note however thatlocations reserved by DEFS may be initialized to zero by thelinker if the reserved locations are in the middle of theprogram. Example:
DEFS 20h ;reserve 32 bytes of memory
==== EQU ====
:Equ sets the value of a symbol on the left of EQU tothe expression on the right. It is illegal to set the valueof a symbol which is already defined. Example:
SIZE equ 46
==== DEFL ====
:This is identical to EQU except that it may redefineexisting symbols. Example:
SIZE defl 48
==== DEFM ====
:Defm should be followed by a string of characters,enclosed in single quotes. The ASCII values of these char-acters characters are assembled into successive memory locations.Example:
DEFM 'A string of funny *@$ characters'
==== END ====
:The end of an assembly is signified by the end of thesource file, or the END pseudo-op. The END pseudo-op mayoptionally be followed by an expression which will definethe start address of the program. This is not actually use-ful useful for CP/M. Only one start address may be defined per pro-gramprogram, and the linker will complain if there are more. Exam-pleExample:
END somelabel
==== COND, IF, ELSE, ENDC ====
:Conditional assembly is introduced by the COND pseudo-op. The operand to COND must be an absolute expression. Ifits value is false (zero) the code following the COND up tothe corresponding ENDC pseudo-op will not be assembled.COND/ENDC pairs may be nested. IF may be used as a synonymfor COND. The ELSE pseudo operation may be included withina COND/ENDC block, for example:
IF CPM call 5 ELSE call os_func ENDC
==== ELSE ====
:See COND.
==== ENDC ====
:See COND. 
==== ENDM ====
:See MACRO.
==== PSECT ====
:This pseudo-op allows specification of relocatable pro-gram program sections. Its arguments are a psect name, optionallyfollowed by a list of psect flags. The psect name is a sym-bol symbol constructed according to the same rules as for labels,however a psect may have the same name as a label withoutconflict. Psect names are recognized only after a PSECTpseudo-op. The psect flags are as follows: ABS Psect is absolute
GLOBAL :;ABS::Psect is globalabsolute
LOCAL :;GLOBAL::Psect is not global
OVRLD :;LOCAL::Psect is to be overlapped by linkernot global
PURE :;OVRLD::Psect is to be read-onlyoverlapped by linker
:;PURE
::Psect is to be read-only
:If a psect is global, the linker will merge it with anyother global psects of the same name from other modules.Local psects will be treated as distinct from any otherpsect from another module. Psects are global by default.
:By default the linker concatenates code within a psectfrom various modules. If a psect is specified as OVRLD, thelinker will overlap each module's contribution to thatpsect. This is particularly useful when linking moduleswhich initialize e.g. interrupt vectors.
:The PURE flag instructs the linker that the psect is tobe made read-only at run time. The usefulness of this flagdepends 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 beloaded at zero. This is useful for statically initializinginterrupt vectors and jump tables. Examples:
PSECT text, global, pure PSECT data, global PSECT vectors, ovrld
==== GLOBAL ====
:Global should be followed by one more symbols (commaseparated) which will be treated by the assembler as globalsymbols, either internal or external depending on whetherthey are defined within the current module or not. Example:
GLOBAL label1, putchar, _printf
==== ORG ====
:An ORG pseudo-op sets the current psect to the default(absolute) psect, and the location counter to its operand,which must be an absolute expression. Example:
ORG 100H
==== MACRO ====
:This pseudo-op defines a macro. It should be eitherpreceded or followed by the macro name, then optionally fol-lowed followed by a comma-separated list of formal parameters. Thelines of code following the MACRO pseudo-op up to the nextENDM pseudo-op will be stored as the body of the macro. Themacro name may subsequently be used in the opcode part of anassembler statement, followed by actual parameters. The textof the body of the macro will be substituted at that point,with any use of the formal parameters substituted with thecorresponding 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 labelfield before the MACRO pseudo-op, or in the operand fieldafter 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
The REPT pseudo-op defines a temporary macro which is
then expanded a number of times, as determined by its argu-mentargument. 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.�_______�|�|}
== Assembler Directives ==
12An assembler directive is a line in the source file which produces no code, but rather which modifies the behaviour of the assembler.4Each directive is recognized by the presence of an asterisk in the first column of the line, followed immediately by a word, only the first character of which is looked at. Assembler Directivesthe line containing the directive itself is never listed. The directives are:
An assembler directive is a line in the source file;<nowiki>*Title</nowiki>which produces no code, but rather which modifies :Use thebehaviour of text following the assembler. Each directive is recognized bythe presence of an asterisk in the first column of the line,followed immediately by as a word, only title for the first character ofwhich is looked atlisting. the line containing the directive itselfis never listed. The directives are:
;<nowiki>*TitleHeading</nowiki>:Use the text following the directive as a title subtitle for thelisting; also causes an *Eject.
;<nowiki>*HeadingList</nowiki>Use the text following the :May be followed by ON or OFF to turn listing on or off respectively. Note that this directive as may be used inside a subtitle macro or include file to control listing of that macro or include file. The previous listing state will be restored on exit from forthe listing; also causes an *Eject macro or include file.
*List
May be followed by ON or OFF to turn listing on or off
respectively. Note that this directive may be used
inside a macro or include file to control listing of
that macro or include file. The previous listing state
will be restored on exit from the macro or include
file.
;<nowiki>*Include</nowiki>
:The file named following the directive will be included in the assembly at that point.
*IncludeThe 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