UP | HOME

构建 Arm64 汇编

Table of Contents

1. 安装交叉编译的工具链

# 交叉编译器
sudo apt install gcc-aarch64-linux-gnu
# Qemu 用户态模拟器
sudo apt install qemu-user

2. 编写 hello world 代码

新建 hello.S 并写入以下代码

.data

/* Data segment: define our message string and calculate its length. */
msg:
    .ascii        "Hello, ARM64!\n"
len = . - msg

.text

/* Our application's entry point. */
.globl _start
_start:
    /* syscall write(int fd, const void *buf, size_t count) */
    mov     x0, #1      /* fd := STDOUT_FILENO */
    ldr     x1, =msg    /* buf := msg */
    ldr     x2, =len    /* count := len */
    mov     w8, #64     /* write is syscall #64 */
    svc     #0          /* invoke syscall */

    /* syscall exit(int status) */
    mov     x0, #0      /* status := 0 */
    mov     w8, #93     /* exit is syscall #93 */
    svc     #0          /* invoke syscall */

3. 编译代码

aarch64-linux-gnu-as -o hello.o hello.S
aarch64-linux-gnu-ld -s -o hello hello.o

4. 运行代码

$ qemu-aarch64 ./hello
Hello, ARM64!
$

Last Updated 2024-06-25 Tue 15:03. Created by Jinghui Hu at 2024-06-25 Tue 00:00.