0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

z80とrgbasmのアセンブリで生成される機械語を比較

0
Last updated at Posted at 2026-09-19

目的

これまでゲームボーイ研究にはrgbasmのゲームボーイ専用アセンブラを使って来ましたが、今後はz80asmを使うことにしました。
しかしz80とゲームボーイのCPUは全く同じではありません。
一部相互に使えない命令があります。
そこでよく使う命令の機械語が一致するか、無い場合は代替策を確認しておきます。

以下の記事で8080z80に対して同じ比較をしています。
8080とz80で機械語を比較

比較

z80用のアセンブラz80asmとゲームボーイ専用アセンブラrgbasmで生成される機械語を比較します。

それぞれのアセンブラで機械語を生成します。

z80asm -o z80.gb z80.asm

rgbasm -o gameboy.o gameboy.asm
rgblink -o gameboy.gb gameboy.o

それぞれの機械語をのバイナリを比較して一致するかを確認する
rgblinkは先頭と末尾に余計なバイナリが生成されるので、表示する範囲を絞ります。

xxd z80.gb
xxd -l 50 gameboy.gb

ld(即値)

z80.asm
org 0x0000
ld a, 1
ld b, 1
ld c, 1
ld d, 1
ld e, 1
ld h, 1
ld l, 1
gameboy.asm
SECTION "test", ROM0[$0000]
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 z80.gb
00000000: 3e01 0601 0e01 1601 1e01 2601 2e01       >.........&...
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: 3e01 0601 0e01 1601 1e01 2601 2e01 0000  >.........&.....

ld(レジスタ間移動)

z80.asm
org 0x0000
ld a, b
ld b, a
ld c, d
ld d, e
ld e, h
ld h, l
ld l, a
gameboy.asm
SECTION "test", ROM0[$0000]
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 z80.gb
00000000: 7847 4a53 5c65 6f                        xGJS\eo
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: 7847 4a53 5c65 6f00 0000 0000 0000 0000  xGJS\eo.........

対レジスタ間接

z80.asm
org 0x0000

ld hl, 0x1234
ld a, (hl)
ld bc, 0x1234
ld a, (bc)
ld de, 0x1234
ld a, (de)
gameboy.asm
SECTION "test", ROM0[$0000]

ld hl, $1234
ld a, [hl]
ld bc, $1234
ld a, [bc]
ld de, $1234
ld a, [de]
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: 2134 127e 0134 120a 1134 121a            !4.~.4...4..
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: 2134 127e 0134 120a 1134 121a 0000 0000  !4.~.4...4......

INC/DEC

z80.asm
org 0x0000

; 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
gameboy.asm
SECTION "test", ROM0[$0000]

; 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 z80.gb
00000000: 3c04 0c14 1c24 2c3d 050d 151d 252d 0313  <....$,=....%-..
00000010: 230b 1b2b                                #..+
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: 3c04 0c14 1c24 2c3d 050d 151d 252d 0313  <....$,=....%-..
00000010: 230b 1b2b 0000 0000 0000 0000 0000 0000  #..+............

加算と減算

z80.asm
org 0x0000

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)
gameboy.asm
SECTION "test", ROM0[$0000]

add a, $10
adc a, $20

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 z80.gb
00000000: c610 ce20 8081 8283 8485 8690 9192 9394  ... ............
00000010: 9596                                     ..
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: c610 ce20 8081 8283 8485 8690 9192 9394  ... ............
00000010: 9596 0000 0000 0000 0000 0000 0000 0000  ................

ジャンプ命令

z80.asm
org 0x0000

jp label
jr label
jr nz, label
call label
label:
ret
gameboy.asm
SECTION "test", ROM0[$0000]

jp label
jr label
jr nz, label
call label
label:
ret
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: c30a 0018 0520 03cd 0a00 c9              ..... .....
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: c30a 0018 0520 03cd 0a00 c900 0000 0000  ..... ..........

割り込み

z80.asm
org 0x0000

di
ei
gameboy.asm
SECTION "test", ROM0[$0000]

ei
di
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: f3fb                                     ..
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: fbf3 0000 0000 0000 0000 0000 0000 0000  ................

xor

z80.asm
org 0x0000

    xor a
gameboy.asm
SECTION "test", ROM0[$0000]

    xor a
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: af                                       .
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: af00 0000 0000 0000 0000 0000 0000 0000  ................

GBとz80で異なる命令

ldh

z80.asm
org 0x0000

    xor a
    ldh($FF40), a
gameboy.asm
SECTION "test", ROM0[$0000]

    xor a
    ldh[$FF40], a

z80には存在しない。

test@test-fujitsu:~/kaihatsu$ rgbasm -o gameboy.o gameboy.asm
test@test-fujitsu:~/kaihatsu$ z80asm -o z80.gb z80.asm
z80.asm:4: error: command or comment expected (was ldh($FF40), a )
*** 1 error found ***

reti

アセンブル自体には成功したが、別々の機械語が生成された。
Z80でゲームボーイの機械語を生成する場合はdb命令で直接機械語のバイナリを書き込む必要がある。

z80.asm
org 0x0000

    reti
gameboy.asm
SECTION "test", ROM0[$0000]

    reti
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: ed4d                                     .M
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: d900 0000 0000 0000 0000 0000 0000 0000  ................

ld(0xFF40), a

z80.asm
org 0x0000

    ld(0xFF40), a
gameboy.asm
SECTION "test", ROM0[$0000]

    ld[$FF40], a
test@test-fujitsu:~/kaihatsu$ xxd z80.gb
00000000: 3240 ff                                  2@.
test@test-fujitsu:~/kaihatsu$ xxd -l 50 gameboy.gb
00000000: ea40 ff00 0000 0000 0000 0000 0000 0000  .@..............
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?