3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

6502アセンブル/逆アセンブル

Posted at

6502のアセンブル/逆アセンブルをしたくなって、ちょっと調べてみたらcc65が全部入りで便利だったので覚書。

cc65

6502マシン用のCコンパイラです。
今回は、Cコンパイラは使いません。

assemble

アセンブラはca65ですが、素のバイナリを吐くためにcl65(フロントエンド)を使います。

hoge.s
	.org	$800
:
	ldy		$50
	lda		skewtbl,y
	sta		$3d
	lda		sectaddr,y
	beq		:+
	sta		$27
	jsr		$005c
:
	dec		$50
	bne		:--

	rts

skewtbl:
	.byte	$00,$0d,$0b,$09,$07,$05,$03,$01
	.byte	$0e,$0c,$0a,$08,$06,$04,$02,$0f

sectaddr:
	.byte	$00,$09,$00,$00,$00,$00,$00,$00
	.byte	$30,$31,$32,$33,$34,$00,$00,$00
  • --listing リスティング生成
  • --target none ターゲット指定(none=素のバイナリ)
$ cl65 --listing hoge.lst --target none -o hoge.bin hoge.s
$ hexdump -C hoge.bin
00000000  a4 50 b9 16 08 85 3d b9  26 08 f0 05 85 27 20 5c  |.P....=.&....' \|
00000010  00 c6 50 d0 eb 60 00 0d  0b 09 07 05 03 01 0e 0c  |..P..`..........|
00000020  0a 08 06 04 02 0f 00 09  00 00 00 00 00 00 30 31  |..............01|
00000030  32 33 34 00 00 00                                 |234...|
00000036

disassemble

逆アセンブラはda65です。

  • -S スタートアドレス
  • --comments コメントレベル
$ da65 -S 0x800 --comments 4 hoge.bin
; da65 V2.17 - Git N/A
; Created:    2018-04-18 10:44:34
; Input file: hoge.bin
; Page:       1


        .setcpu "6502"

; ----------------------------------------------------------------------------
L005C           := $005C
L0861           := $0861
; ----------------------------------------------------------------------------
L0800:  ldy     $50                             ; 0800 A4 50                    .P
        lda     L0816,y                         ; 0802 B9 16 08                 ...
        sta     $3D                             ; 0805 85 3D                    .=
        lda     L0826,y                         ; 0807 B9 26 08                 .&.
        beq     L0811                           ; 080A F0 05                    ..
        sta     $27                             ; 080C 85 27                    .'
        jsr     L005C                           ; 080E 20 5C 00                  \.
L0811:  dec     $50                             ; 0811 C6 50                    .P
        bne     L0800                           ; 0813 D0 EB                    ..
        rts                                     ; 0815 60                       `

; ----------------------------------------------------------------------------
L0816:  brk                                     ; 0816 00                       .
        ora     $090B                           ; 0817 0D 0B 09                 ...
        .byte   $07                             ; 081A 07                       .
        ora     $03                             ; 081B 05 03                    ..
        ora     ($0E,x)                         ; 081D 01 0E                    ..
        .byte   $0C                             ; 081F 0C                       .
        asl     a                               ; 0820 0A                       .
        php                                     ; 0821 08                       .
        asl     $04                             ; 0822 06 04                    ..
        .byte   $02                             ; 0824 02                       .
        .byte   $0F                             ; 0825 0F                       .
L0826:  brk                                     ; 0826 00                       .
        ora     #$00                            ; 0827 09 00                    ..
        brk                                     ; 0829 00                       .
        brk                                     ; 082A 00                       .
        brk                                     ; 082B 00                       .
        brk                                     ; 082C 00                       .
        brk                                     ; 082D 00                       .
        bmi     L0861                           ; 082E 30 31                    01
        .byte   $32                             ; 0830 32                       2
        .byte   $33                             ; 0831 33                       3
        .byte   $34                             ; 0832 34                       4
        brk                                     ; 0833 00                       .
        brk                                     ; 0834 00                       .
        brk                                     ; 0835 00                       .

.org付けてくれたらいいのに‥。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?