assembly 如何在m1 macbook上编译arm汇编

mkh04yzy  于 2022-11-24  发布在  Mac
关注(0)|答案(2)|浏览(229)

我是一个组装新手,想学习arm组装(仅仅因为我有一台m1 mac)。我在网上找不到很多资源,这就是我来这里的原因。我的代码如下(文件名为first.asm):

global _main
section .text

_main: 
    mov r7, 0x4
    mov r0, 1
    ldr r1, message
    mov r2, 13
    swi 0

.section .data
    message: .ascii "Hello, World\n"

当我使用

as first.asm -o hello.o

出现以下错误:

first.asm:2:1: error: invalid instruction mnemonic 'global'
global _main
^~~~~~
first.asm:3:1: error: invalid instruction mnemonic 'section'
section .text
^~~~~~~
first.asm:6:2: error: unknown use of instruction mnemonic without a size suffix
        mov r7, 0x4
        ^
first.asm:7:2: error: unknown use of instruction mnemonic without a size suffix
        mov r0, 1
        ^
first.asm:8:2: error: invalid instruction mnemonic 'ldr'
        ldr r1, message
        ^~~
first.asm:9:2: error: unknown use of instruction mnemonic without a size suffix
        mov r2, 13
        ^
first.asm:10:2: error: invalid instruction mnemonic 'swi'
        swi 0
        ^~~
first.asm:12:15: error: unexpected token in '.section' directive
.section .data

我有几个问题:
1.“as”是一个用于汇编代码的内置mac编译器吗?
1.我是否需要不同的编译器?
1.既然我使用的是m1 mac,学习arm汇编对我来说有意义吗?或者我可以毫无问题地编写x86汇编吗?

sdnqo3pr

sdnqo3pr1#

我想你会在这里找到开始在Apple M1上编写ARM64汇编代码所需要知道的一切:https://github.com/below/HelloSilicon

z6psavjg

z6psavjg2#

global start
section .text

start:
    mov     eax, 0x2000004 ; write system call
    mov     edi, 1         ; stdout = 1
    mov     rsi, msg       ; address of the message to print
    ;lea     rsi, [rel msg]; Alternative way using RIP relative addressing
    mov     edx, msg.len   ; length of message
    syscall                ; Use syscall, NOT int 0x80

    mov     eax, 0x2000001 ; exit system call
    mov     edi, 42        ; return 42 when exiting
    syscall

section .rodata

msg:    db      "Hello, world!", 10
.len:   equ     $ - msg

使用nasm(brew安装nasm)

nasm -f macho64 hello.asm

和链接:

ld -macosx_version_min 10.7.0 -o hello hello.o

我在配备M1芯片的MacbookPro上成功测试了这一点
See this link

相关问题