Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile arm assembly on an m1 macbook

I am completely new to assembly and would like to learn arm assembly (simply because I own an m1 mac). I can't really find many ressources online, that's why I'm here. My code is as follows (file is called 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"

When I use

as first.asm -o hello.o 

I get the following errors:

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

I have a couple of questions:

  1. Is "as" a built-in mac compiler for assembly code?
  2. Do I need a different compiler?
  3. Does it make sense for me to learn arm assembly since I'm on an m1 mac or could I write x86 assembly without issues?
like image 311
cachedcashew Avatar asked Sep 13 '25 13:09

cachedcashew


1 Answers

The following tutorial from Stephen Smith details some of the differences between Linux ARM64 assembly language and MacOS ARM64 assembly language:

Here is a sample HelloWorld.s source assembler program for MacOS ARM64:

// Assembler program to print "Hello World!"
// to stdout.
//
// X0-X2 - parameters to linux function services
// X16 - linux function number
//
.global _start             // Provide program starting address to linker
.p2align 3 // Feedback from Peter

// Setup the parameters to print hello world
// and then call Linux to do it.

_start: mov X0, #1     // 1 = StdOut
adr X1, helloworld // string to print
mov X2, #13     // length of our string
mov X16, #4     // MacOS write system call
svc 0     // Call linux to output the string

// Setup the parameters to exit the program
// and then call Linux to do it.

mov     X0, #0      // Use 0 return code
mov     X16, #1     // Service command code 1 terminates this program
svc     0           // Call MacOS to terminate the program

helloworld:      .ascii  "Hello World!\n"

The commands to assemble and link it are the following:

as -o HelloWorld.o HelloWorld.s
ld -macosx_version_min 13.0.0 -o HelloWorld HelloWorld.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64

IMO, the ideal way to approach the challenge of learning ARM64 assembler on MacOS ARM64 is to use Xcode with its built in source code management and debugger.

The Stephen Smith book and his source code examples is for Linux ARM64 and I believe iOS ARM64. The HelloSilicon GIT repository referenced by Albert is Stephen's source code updated to support native macOS ARM64. Stephen's Chapter 3 source code includes a Xcode.project to run ARM64 on an iOS device with all the view controller junk. The HelloSilicon Chapter 03 includes a much simpler Xcode project for native macOS - Invaluable to you (and me) since you will also develop skills around source code management in Xcode together with using in the Xcode debugger.

Assembler languages are very different between CPU architectures. Whatever one you decide to learn, stick with it.

like image 110
Dwayne Avatar answered Sep 15 '25 14:09

Dwayne