-
「Z80 Assembler Reference Manual」の版間の差分
(→追加条件コード) (タグ: モバイル編集、モバイルウェブ編集) |
(→アセンブラディレクティブ) (タグ: モバイル編集、モバイルウェブ編集) |
||
685行目: | 685行目: | ||
|} | |} | ||
− | == | + | == アセンブラディレクティブ== |
− | + | アセンブラディレクティブはソースファイルにおける行で、これはコードを生成せず、むしろアセンブラの振る舞いを変えます。 | |
+ | |||
+ | どのディレクティブも行の初めのカラムにアスタリスクがつくのでわかります。 | ||
+ | これは単語のすぐあとにつき、最初の文字のみを見ます。 | ||
+ | |||
+ | ディレクティブ自体を含む行はリスト化されません。ディレクティブは以下の通りです | ||
;<nowiki>*Title</nowiki> | ;<nowiki>*Title</nowiki> | ||
− | : | + | :このディレクティブ後続する行をリスティングのタイトルとして使います。 |
;<nowiki>*Heading</nowiki> | ;<nowiki>*Heading</nowiki> | ||
− | : | + | :このディレクティブに続く行をリスティングのサブタイトルとして使います。*Ejectにも影響します。 |
;<nowiki>*List</nowiki> | ;<nowiki>*List</nowiki> | ||
− | : | + | :ONやOFFを後に伴い、リストをそれぞれオンオフします。このディレクティブはマクロの内部や、そのマクロやインクルードファイルをコントロールするインクルードファイルでも使われる可能性があります。 |
− | + | :以前のリスティング状態はマクロやインクルードファイルから戻った時に回復されます。 | |
;<nowiki>*Include</nowiki> | ;<nowiki>*Include</nowiki> | ||
− | : | + | :ディレクティブに後続するファイル名が、この時点でアセンブラにインクルードされます。 |
;<nowiki>*Eject</nowiki> | ;<nowiki>*Eject</nowiki> | ||
− | : | + | :新しいページをこの時点で開始します。ソースのフォームフィールド文字も同じ効果を持ちます。 |
− | + | :これらのディレクティブのいくつかの例を挙げます。 | |
+ | *Title Widget Control Program | ||
− | |||
*Heading Initialization Phase | *Heading Initialization Phase | ||
− | + | ||
*Include widget.i | *Include widget.i | ||
− | |||
− | |||
== Diagnostics == | == Diagnostics == |
2017年8月16日 (水) 20:58時点における版
目次
Introduction
The assembler incorporated in the HI-TECH C compiler system is a full-featured relocating macro assembler 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 implements certain extensions to the operands allowed, and certain additional pseudo-ops, which are described here. The assembler also accepts the additional opcodes for the Hitachi 64180 and Z180 processors.
Usage
The assembler is named zas, and is invoked as follows:
ZAS options files ...
The files are one or more assembler source files which
will be assembled, but note that all the files are assembled
as one, not as separate files. To assemble separate files,
the assembler must be invoked on each file separately. The
options are zero or more options from the following list:
- -N
- Ignore arithmetic overflow in expressions. The -N option suppresses the normal check for arithmetic overflow. The assembler follows the "Z80 Assembly Language Handbook" in its treatment of overflow, and in certain instances this can lead to an error where in fact the expression does evaluate to what the user intended. This option may be used to override the overflow checking.
- -J
- Attempt to optimize jumps to branches. The -J option will request the assembler to attempt to assemble jumps and conditional jumps as relative branches where possible. Only those conditional jumps with branch equivalents will be optimized, and jumps will only be optimized to branches where the target is in branch range. Note that the use of this option slows the assembly down, due to the necessity for the assembler to make an additional pass over the input code.
- -U
- Treat undefined symbols as external. The -U option will suppress error messages relating to undefined symbols. Such symbols are treated as externals in any case. The use of this option will not alter the object code generated, but merely serves to suppress the error messages.
- -Ofile
- Place the object code in file. The default object file name is constructed from the name of the first source file. Any suffix or file type (i.e. anything following the rightmost dot ('.') in the name is stripped, and the suffix .obj appended. Thus the command
ZAS file1.as file2.z80
- will produce an object file called file1.obj. The use of the -O option will override this default convention, allowing the object file to be arbitrarily named. For example:
ZAS -ox.obj file1.obj
- will place the object code in x.obj.
- -Llist
- Place an assembly listing in the file list, or on standard output if list is null A listfile may be produced with the -L option. If a file name is supplied to the option, the list file will be created with that name, otherwise the listing will be written to standard output (i.e. the console). List file names such as CON: and LST: are acceptable.
- -Wwidth
- The listing is to be formatted for a printer of given width The -W option specifies the width to which the listing is to be formatted. E.g.
ZAS -Llst: -W80 x.as
- will output a listing formatted for an 80 column printer to the list device.
- -C
- This options requests ZAS to produce cross reference information in a file. The file will be called xxx.crf where xxx is the base part of the first source file name. It will then be necessary to run the CREF utility to turn this information into a formatted listing.
The Assembly Language
As mentioned above, the assembly language accepted by zas is based on the Zilog mnemonics. You should have some reference book such as the "Z80 Assembly Language Handbook". Described below are those areas in which zas differs, or has extensions, compared to the standard Zilog assembly language.
Symbols
The symbols (labels) accepted by the assembler may be of any length, and all characters are significant. The 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 lower case are distinct. The following are all legal and distinct symbols.
An_identifier an_identifier an_identifier1 $$$ ?$_123455
Note that the symbol $ is special (representing the current location) and may not be used as a label. Nor may any opcode or pseudo-op mnemonic, register name or condition code name. You should note the additional condition code names described later.
Temporary Labels
The assembler implements a system of temporary labels, useful for use within a localized section of code. These help eliminate the need to generate names for labels which are referenced only in the immediate vicinity of their definition, for example where a loop is implemented.
A temporary label takes the form of a digit string. A reference to such a label requires the same digit string, plus an appended b or f to signify a backward or forward reference respectively. Here is an example of the use of 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
The digit string may be any positive decimal number 0
to 65535. A temporary label value may be re-used any number
of times. Where a reference to e.g. 1b is made, this will
reference the closest label 1: found by looking backwards
from the current point in the file. Similarly 23f will
reference the first label 23: found by looking forwards from
the current point in the file.
Constants
Constants may be entered in one of the radices 2, 8, 10 or 16. The default is 10. Constants in the other radices may be denoted by a trailing character drawn from the following 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, for example LD A,0x21. Note that a lower case b may not be used to indicate a binary number, since 1b is a backward reference to a temporary label 1:.
Character Constants
A character constant is a single character enclosed in single quotes ('). Multi character constants may be used only as an operand to a DEFM pseudo-op.
Floating Constants
A floating constant in the usual notation (e.g. 1.234 or 1234e-3) may be used as the operand to a DEFF pseudo-op.
Opcode Constants
Any z80 opcode may be used as a constant in an expres- 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 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
Expressions
Expressions are constructed largely as described in the "Z80 Assembly Language Handbook".
Operators
The following operators may be used in expressions:
Operator | Meaning |
---|---|
& | Bitwise AND |
* | Multiplication |
+ | Addition |
- | Subtraction |
.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 |
Operators starting with a dot "." should be delimited
by spaces, thus label .and. 1 is valid but label.and.1 is
not.
Relocatability
Zas produces object code which is relocatable; this means that it is not necessary to specify assembly time where the code is to be located in memory. It is possible to do so, by use of the ORG pseudo-op, however the preferred approach is to use program sections or psects. A psect is a named section of the program, in which code or data may be defined at assembly time. All parts of a psect will be loaded contiguously into memory, even if they were defined in separate files, or in the same file but separated by code for another psect. For example, the following code will load some executable instructions into the psect named text, and some data bytes into the data psect.
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
Note that even though the two blocks of code in the
text psect are separated by a block in the data psect, the
two text psect blocks will be contiguous when loaded by the
linker. The instruction "ld hl,anotherstring" will fall
through to the label "putit:" during execution. The actual
location in memory of the two psects will be determined by
the linker. See the linker manual for information on how
psect addresses are determined.
A label defined in a psect is said to be relocatable, that is, its actual memory address is not determined at assembly time. Note that this does not apply if the label is in the default (unnamed) psect, or in a psect declared abso- lute (see the PSECT pseudo-op description below). Any labels declared in an absolute psect will be absolute, that is their address will be determined by the assembler.
With the version of ZAS supplied with version 7 or later of HI-TECH C, relocatable expressions may be combined freely in expressions. Older versions of ZAS allowed only limited arithmetic on relocatable expressions.
擬似命令
擬似命令は、"Z80アセンブリ言語ハンドブック"の記述に依拠し、いくつかの追加があります。
DEFB, DB
この擬似命令はエクスプレッションのコンマ区切りリストの後に置かれます。
- 一続きのバイトロケーションにアセンブルされます。
どのエクスプレッションも-128から255の値を内包する必要があります。 DBはDBFBのシノニムとして使うことができます。
例:
DEFB 10, 20, 'a', 0FFH DB 'hello world',13,10,0
DEFF
- この擬似命令は浮動小数点数コンスタントを32bitのHI-TECH Cフォーマットの浮動小数点数コンスタントにアセンブルします。
例
pi: DEFF 3.14159
DEFW
- エクスプレッションを値に制限のないワードにアセンブルすること以外はDEFBと似ています。
例:
DEFW -1, 3664H, 'A', 3777Q
DEFS
- DEFSは初期化することなしにメモリロケーションをリザーブします。
- このオペランドは絶対的なエクスプレッションで、リザーブされるされるバイト数を表現します。
- このエクスプレッションは現在のロケーションカウンターに追加されます。
- しかしながら、DEFSによってリザーブされたロケーションがプログラムの中間の場合、リンカによってゼロに初期化される可能性があります。
例:
DEFS 20h ;メモリの32バイトをリザーブ
EQU
- EquはEQUの左に、シンボルの値を
右にエクスプレッションを設定します。
- 既にていぎされているシンボルの値を設定するのは違反です。
- 例:
SIZE equ 46
DEFL
- 既に存在するシンボルを再定義できることを除いて、EQUと同じです。
例:
SIZE defl 48
DEFM
- DEFMはシングルクォートで囲まれた、文字列の後に続かなければいけません。
- これら文字のASCII コードは連続したメモリにアセンブルされます。
例:
DEFM 'A string of funny *@$ characters'
END
- ソースファイルの終わり、もしくは擬似命令ENDがアセンブルの終了を意味します。
- 擬似命令ENDはオプション的に、プログラムの開始アドレスを定義するエクスプレッションに続いて現れることがあります。
- これはCP/Mでは本当のところ、便利ではありません。ただ1つの開始アドレスだけがプログラムごとに定義されます。それ以上ある場合はリンカが文句をいいます。
例:
END somelabel
COND, IF, ELSE, ENDC
- 条件的アセンブルはCOND擬似命令によって、提供されます。
- CONDのオペランドは絶対的なエクスプレッションでなければいけません。
- もし、この値が偽(ゼロ)の場合、CONDから続き、対応するENDC擬似命令の部分まではアセンブルされません。
- COND/ENDCのペアはネストが可能です。
- IFがCONDのシノニムとして使用可能です。
- ELSE擬似命令をCOND/ENDCブロックの中に含むことができます。
例:
IF CPM call 5 ELSE call os_func ENDC
ELSE
- COND参照
ENDC
- COND参照
ENDM
- MACRO参照
PSECT
- この擬似命令は再配置可能なプログラムセクションの定義を許可します。
- この引数はオプション的にpsectフラッグの後に着く、psect nameです。
- psect名はラベルと同じルールに従って構築されるシンボルですが、psectは、衝突することなくラベルと同じ名前を持つことができます。
- psect名はPSECT擬似命令の後で、のみ認識されます。psectフラグは以下の通りです。
- ABS
- Psectは絶対値です。
- GLOBAL
- Psectはグローバルです。
- LOCAL
- Psectはグローバルではありません。
- OVRLD
- Psectはリンカによってオーバーラップ可能です
- PURE
- Psectはリードオンリーでなければいけません
- Psectがもしグローバルな場合、リンカはこれを、他モジュールで同名の他のグローバルpsectとマージします。
- ローカルのpsectは他のモジュールからの、他のpsectとは区別されます。Psectはデフォルトでグローバルです。
- デフォルトでリンカはいろいろなモジュールからのpsect内で、コードを連結します。
- psectがOVRLDとして指定されると、リンカがそれぞれのモジュールを、そのpsectにオーバーラップしていきます。
- 初期化する時や、ベクター割り込みする時など、モジュールをリンクをする特定の場合に便利です。
- PUREフラグは、psectが実行時にのみリードオンリーになり得るとリンカに伝えます。
- このフラグの有用性はリンカが要求を強制する能力に依存します。CP/Mは悲しいことに、ここに配慮されていません?
- ABSフラグはpsectを絶対化します。
- psectはゼロに読み込まれます。
- これは静的にベクター割り込みを初期化し、テーブルにジャンプする時に有用です。
例:
PSECT text, global, pure PSECT data, global PSECT vectors, ovrld
GLOBAL
- GLOBAL はもう1つのシンボルに続いて(カンマ区切りで)使われなければいけません。これはinternalでもexternalでも、現在のモジュールの内部で依存していてもいなくても、アセンブラにグローバルとして取り扱われます。
- 例:
GLOBAL label1, putchar, _printf
ORG
- 擬似命令ORGは現在のpsectをデフォルト(絶対)psectに設定し、絶対表現としなければならないロケーションカウンタをオペランドとします。
例:
ORG 100H
MACRO
- この擬似命令はマクロを定義します。マクロ名に前置する要素も後続する要素もあります。
カンマ区切りのフォーマルパラメータがオプション的に後続します。
- MACRO擬似命令に後続し、次のENDMまでのコードの行がマクロ本体として格納されます。
- マクロ名は後続するアセンブラステートメントのオプコード部分でも使われ、実際のパラメータが後続します。
- マクロの本体は、その部分で、対応する実際のパラメータを持つフォーマルパラメータと置き換えられます??
- 例:
print MACRO string psect data 999: db string,'$' psect text ld de,999b ld c,9 call 5 ENDM
- 使用されると、このマクロはマクロの本体で、実際のパラメータの代用である関数と引数の3つのインストラクションに展開されます??
- 従って、print hello world' は次のように展開されます。
psect data 999: db 'hello world','$' psect text ld de,999b ld c,9 call 5
- マクロの引数はかぎ括弧('<' と '>')で閉じてコンマなどのデリミタキャラクタを含む任意のテキストを渡すために使うことが可能です。
- 例えば、キャリッジリターンとラインフィードを含む文字列を先ほど定義したprintマクロで使いたいとしましょう。
その場合のマクロを以下のように発動させたとします。
print 'hello world',13,10
- しかしこれは失敗します。なぜなら13と10が追加の引数として扱われて、無視されるからです。コンマを含む文字列を単体の引数として渡すためには、次のように書きます。
print <'hello world',13,10>
- これで 'hello world',13,10というテキストを単体の引数として渡すことができるでしょう。これは次のようなコードに展開されます。
psect data 999: db 'hello world',13,10,'$' psect text ld de,999b ld c,9 call 5
- ZASは古いバージョンのZASや他のZ80アセンブラとの互換性のため、2種類のマクロ定義をサポートしています。
マクロ名はMACRO擬似命令の前のラベルフィールドでも、MACRO擬似命令の後のオペランドフィールドでも定義できます。
- そのため、この2つのマクロ定義は等価です。
bdos MACRO func,arg ld de,arg ld c,func call 5 ENDM
MACRO bdos,func,arg ld de,arg ld c,func call 5 ENDM
LOCAL
- LOCAL擬似命令はマクロの拡張のため、ユニークなラベルを定義できます。
- LOCALディレクティブの後にシンボルがリスト化されている場合、マクロが展開される時に、これらは代わりの
ユニークなアセンブラ生成シンボルとなります。
- 例
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
- nocopyの位置に展開されたコードがユニークなアセンブラ生成ラベルを含む場合には、例えば、copy(recptr),buf,(recsize)は次のように展開されます。
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
- 2回目に発動する際には、nocopyラベルは??0002に展開されます。
REPT
REPT擬似命令は引数によってていぎされただけ、複数回展開されるテンポラリマクロを定義します。
例:
REPT 3 ld (hl),0 inc hl ENDM
これは次のように展開されます。
ld (hl),0 inc hl ld (hl),0 inc hl ld (hl),0 inc hl
IRP と IRPC
IRPとIRPCディレクティブはREPTに似ていますが、ブロックを決まった回数繰り返すのではなく、引数リスト化にある回数それぞれを一回だけ実行します。
IRPの場合はリストはコンベンショナルマクロの引数リスト、IRPCの場合は文字列からの連続する文字です。
例:
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
これは次のように展開されます。
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
コンベンショナルマクロを使用する際と同様に、LOCALラベルとかぎ括弧の使用に注意してください。
IRPCを最もよく表す例は以下のものです。
IRPC char,ABC ld c,2 ld e,'char' call 5 ENDM
これは次のように展開されます。
ld c,2 ld e,'A' call 5 ld c,2 ld e,'B' call 5 ld c,2 ld e,'C' call 5
追加条件コード
このアセンブラはいくつかの追加条件コードを認識します。 これらは次の通りです。
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 | ld a,iの後に使う | |
ei | 割り込み可能フラグの | |
評価です。それぞれ、 | ||
可能、不可能。 |
アセンブラディレクティブ
アセンブラディレクティブはソースファイルにおける行で、これはコードを生成せず、むしろアセンブラの振る舞いを変えます。
どのディレクティブも行の初めのカラムにアスタリスクがつくのでわかります。 これは単語のすぐあとにつき、最初の文字のみを見ます。
ディレクティブ自体を含む行はリスト化されません。ディレクティブは以下の通りです
- *Title
- このディレクティブ後続する行をリスティングのタイトルとして使います。
- *Heading
- このディレクティブに続く行をリスティングのサブタイトルとして使います。*Ejectにも影響します。
- *List
- ONやOFFを後に伴い、リストをそれぞれオンオフします。このディレクティブはマクロの内部や、そのマクロやインクルードファイルをコントロールするインクルードファイルでも使われる可能性があります。
- 以前のリスティング状態はマクロやインクルードファイルから戻った時に回復されます。
- *Include
- ディレクティブに後続するファイル名が、この時点でアセンブラにインクルードされます。
- *Eject
- 新しいページをこの時点で開始します。ソースのフォームフィールド文字も同じ効果を持ちます。
- これらのディレクティブのいくつかの例を挙げます。
*Title Widget Control Program
*Heading Initialization Phase
*Include widget.i
Diagnostics
An error message will be written on the standard error stream for each error encountered in the assembly. This 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 indicate the error. The characters and the corresponding messages are:
- 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
Z80/Z180/64180 Instruction Set
The remainder of this chapter is devoted to a complete instruction set listing for the Z80, Z180, 64180 and NSC800 processors. The Z180 and 64180 will execute all Z80 instructions, although the timing is different.