目的
筆者は現在アセンブリ言語でゲームボーイのプログラムを書いて仕様を研究しています。
厳密には違いますがゲームボーイのCPUはZ80を改造したような仕様になっています。
ゲームボーイ研究日記
z80のアセンブリを書くのにも、少し慣れてきたので実際のz80アセンブリも書いてみたくなってきました。
どうせやるのならエミュレータでなく実際のCPUで動かしたいのですが、z80の機器を入手するのは大変です。
ちょうどNECの8080互換CPUであるV30を持っているのでこれを使用します。
親しんだld等の命令を使いたいのでz80のアセンブリを使用しますが、以下の問題があります。
- z80と8080のアセンブラでニーモニックが異なる
- z80から追加された命令は8080で動かない
よって今回はz80と8080のそれぞれのアセンブラで同じ処理を書き、生成される機械語が一致するかを調べます。
網羅は厳しいのでよく使う命令に絞ります。
方法
8080とz80のアセンブラを導入します。
sudo apt install z80asm
pip install suite8080
export PATH="$HOME/.local/bin:$PATH"
それぞれのアセンブラで機械語を生成します。
z80asm -o testz80.bin testz80.asm
asm80 -o test8080.bin test8080.asm
それぞれの機械語をのバイナリを比較して一致するかを確認する
xxd testz80.bin
xxd test8080.bin
mviとld(レジスタ←即値)
test8080.asm
mvi a, 1
mvi b, 1
mvi c, 1
mvi d, 1
mvi e, 1
mvi h, 1
mvi l, 1
testz80.asm
ld a, 1
ld b, 1
ld c, 1
ld d, 1
ld e, 1
ld h, 1
ld l, 1
上記のアセンブリの機械語は完全に一致しました。
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: 3e01 0601 0e01 1601 1e01 2601 2e01 >.........&...
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: 3e01 0601 0e01 1601 1e01 2601 2e01 >.........&...
movとld(レジスタ間移動)
test8080.asm
mov a, b
mov b, a
mov c, d
mov d, e
mov e, h
mov h, l
mov l, a
testz80.asm
ld a, b
ld b, a
ld c, d
ld d, e
ld e, h
ld h, l
ld l, a
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: 7847 4a53 5c65 6f xGJS\eo
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: 7847 4a53 5c65 6f
HLレジスタ間接
test8080.asm
lxi h, 1234h
mov a, m
testz80.asm
ld hl, 0x1234
ld a, (hl)
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: 2134 127e !4.~
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: 2134 127e !4.~
8080はBC、DEを対で使えない模様。
INC/DEC
test8080.asm
; 8bit
inr a
inr b
inr c
inr d
inr e
inr h
inr l
dcr a
dcr b
dcr c
dcr d
dcr e
dcr h
dcr l
; 16bit
inx b
inx d
inx h
dcx b
dcx d
dcx h
testz80.asm
; 8bit
inc a
inc b
inc c
inc d
inc e
inc h
inc l
dec a
dec b
dec c
dec d
dec e
dec h
dec l
; 16bit
inc bc
inc de
inc hl
dec bc
dec de
dec hl
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: 3c04 0c14 1c24 2c3d 050d 151d 252d 0313 <....$,=....%-..
00000010: 230b 1b2b #..+
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: 3c04 0c14 1c24 2c3d 050d 151d 252d 0313 <....$,=....%-..
00000010: 230b 1b2b #..+
加算と減算
test8080.asm
adi 10h
aci 20h
add b
add c
add d
add e
add h
add l
add m
sub b
sub c
sub d
sub e
sub h
sub l
sub m
testz80.asm
add a, 0x10
adc a, 0x20
add a, b
add a, c
add a, d
add a, e
add a, h
add a, l
add a, (hl)
sub b
sub c
sub d
sub e
sub h
sub l
sub (hl)
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: c610 ce20 8081 8283 8485 8690 9192 9394 ... ............
00000010: 9596 ..
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: c610 ce20 8081 8283 8485 8690 9192 9394 ... ............
00000010: 9596 ..
ジャンプ命令
test8080.asm
jmp label
call label
label:
ret
testz80.asm
jp label
call label
label:
ret
test@test-fujitsu:~/kaihatsu$ xxd testz80.bin
00000000: c306 00cd 0600 c9 .......
test@test-fujitsu:~/kaihatsu$ xxd test8080.bin
00000000: c306 00cd 0600 c9 .......
z80のjr命令は8080には無い模様