2
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?

Raspberry Piでアセンブリを触ってみよう

Last updated at Posted at 2025-09-01

Raspberry Pi 上で ARM アセンブリを使って、プログラムを動かす流れを紹介します。

対象環境

  • Raspberry Pi 4 (他モデルでも問題なし)
  • Raspberry Pi OS Bookworm(最新版)
  • OSは64bit
  • インターネット接続環境
  • ターミナル操作ができること(SSH または HDMI 接続)
  • Raspberry Piのセットアップが完了していること

手順の全体像

  1. 開発環境の準備
  2. サンプルプログラムの作成
  3. 実行ファイルの作成(アセンブル&リンク)
  4. 実行
  5. まとめ

1. 開発環境の準備

まずは開発に必要なツールをインストールし、作業ディレクトリを用意します。

sudo apt update
sudo apt install -y build-essential
mkdir -p ~/assembly_samples && cd ~/assembly_samples

2. サンプルプログラムの作成

エディタでソースコードファイルを作成します。

nano hello.s

内容は以下のとおりです。

// hello.s (AArch64)
    .section .data
msg:
    .ascii "Hello, Raspberry Pi!\n"
len = . - msg

    .section .text
    .global _start
_start:
    // write(1, msg, len)
    mov     x8, #64           // sys_write (arm64)
    mov     x0, #1            // fd = stdout
    adrp    x1, msg           // x1 ← msg のpage基底
    add     x1, x1, :lo12:msg // x1 ← msg の下位12bitオフセット
    mov     x2, #len          // count = len
    svc     #0

    // exit(0)
    mov     x8, #93           // sys_exit (arm64)
    mov     x0, #0
    svc     #0

3. 実行ファイルの作成(アセンブル&リンク)

以下のコマンドでソースを機械語に変換し、実行可能ファイルを作成します。

as -o hello.o hello.s
ld -o hello hello.o

4. 実行

作成した実行ファイルを実行します。

./hello
Hello, Raspberry Pi!

の様に表示されます。

5. まとめ

Raspberry Piでアセンブリのサンプルプログラムを動かすことができました。
よろしければ、いいね、ストック、コメントいただけますと幸いです。

2
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
2
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?